简体   繁体   中英

How to update Spreadsheet Properties with Google Sheets API and Python?

I'm trying to update fontSize from here:

sheets_api.spreadsheets().get(spreadsheetId=ssId).execute()

{'properties': {'autoRecalc': 'ON_CHANGE',
  'defaultFormat': {'backgroundColor': {'blue': 1, 'green': 1, 'red': 1},
   'backgroundColorStyle': {'rgbColor': {'blue': 1, 'green': 1, 'red': 1}},
   'padding': {'bottom': 2, 'left': 3, 'right': 3, 'top': 2},
   'textFormat': {'bold': False,
    'fontFamily': 'arial,sans,sans-serif',
    'fontSize': 10,

With this command:

requests = [
{
  "updateSpreadsheetProperties": {
    "properties": {"textFormat": {"fontSize": 12}},
    "fields": "textFormat(fontSize)"
  }
}
]
response = sheets_api.spreadsheets().batchUpdate(
    spreadsheetId=ssId, body={'requests': requests}).execute()

And get:

Unknown name "textFormat"

With this command:

{
  "updateSpreadsheetProperties": {
    "properties": {"fontSize": 12},
    "fields": "fontSize"
  }
}

I get:

Unknown name "fontSize"

Update:

This command returned no errors, but the font didn't change:

{
  "updateSpreadsheetProperties": {
    "properties": {"defaultFormat": {"textFormat": {"fontSize": 12}}},
    "fields": "defaultFormat(textFormat(fontSize))"
  }
}

Some considerations:

Unfortunately you can't update the defaultFormat with an updateSpreadsheetProperties request. As you can see in the documentation the defaultFormat is read-only: that means you can't edit it with any update request.

Solution:

You will need to use the repeatCell request instead: This request is composed by two parts: 1. The range within we want to apply our format update (in this case it will be the whole Sheet) 2. The cell format we want to apply

Code:

So let's build a new request object like this:

 requests = [ { "repeatCell": { "range": { "sheetId": 0 //This is the index for the first sheet in your spreadsheet }, "cell": { "userEnteredFormat": { "textFormat": { "fontSize": 12, } } }, "fields": "userEnteredFormat(textFormat)" } } ]

Now, if you want to apply this format to the whole spreadsheet you could easily loop through your sheets and update the "sheetId" index for every request.

Resources:

Repeat Cell Request

Cell Data

Cell Format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM