Update format of more than one cell with updateCel

2019-08-06 09:06发布

问题:

I'm trying to center align a range of cells, but only the first cell in the range is updated with the specified format.

Here is my code:

align = 'CENTER'
data={
      "requests": [
        {
          "updateCells": {
            "rows": [
              {
                "values": [
                  {
                    "userEnteredFormat": {
                      "horizontalAlignment": align,
                      "textFormat":  { 
                        "fontFamily": fontFamily,
                        "fontSize":  fontSize
                      }
                    }
                  }
                ]
              }
            ],
            "range": {
              "sheetId": sheetId,
              "startRowIndex": startRowIndex,
              "endRowIndex": endRowIndex,
              "startColumnIndex": startColumnIndex,
              "endColumnIndex": endColumnIndex
            },
            "fields": "userEnteredFormat"
          }
        }
      ]
    }

If I log the values - i.e. print (startRowIndex, endRowIndex, startColumnIndex, endColumnIndex) - they are correct (e.g. 0 1 27 30), yet only the first cell is updated to the format - not the whole range.

What's going on here? How can I apply the specified format to the whole range?

回答1:

You want to update "AB1:AD1" ({startRowIndex: 0, endRowIndex: 1, startColumnIndex: 27, endColumnIndex: 30}). If my understanding is correct, how about this modification?

Modified points :

  • When 3 columns are updated, the values are also required to be updated. In your case, you want to update 3 columns. So it is required to create like {values: [{userEnteredFormat: ###}, {userEnteredFormat: ###}, {userEnteredFormat: ###}]}.

Modified request :

{
  "requests": 
  [
    {
      "updateCells": 
      {
        "rows": 
        [
          {
            "values": 
            [
              {
                "userEnteredFormat": 
                {
                  "horizontalAlignment": align ,   #'CENTER','LEFT','RIGHT',
                  "textFormat": 
                  {
                    "fontFamily": fontFamily,
                    "fontSize": fontSize
                  }
                }
              },
              {
                "userEnteredFormat": 
                {
                  "horizontalAlignment": align ,   #'CENTER','LEFT','RIGHT',
                  "textFormat": 
                  {
                    "fontFamily": fontFamily,
                    "fontSize": fontSize
                  }
                }
              },
              {
                "userEnteredFormat": 
                {
                  "horizontalAlignment": align ,   #'CENTER','LEFT','RIGHT',
                  "textFormat": 
                  {
                    "fontFamily": fontFamily,
                    "fontSize": fontSize
                  }
                }
              }
            ]
          }
        ],
        "range": 
        {
          "sheetId": sheetId,
          "startRowIndex": startRowIndex,
          "endRowIndex": endRowIndex,
          "startColumnIndex": startColumnIndex,
          "endColumnIndex": endColumnIndex
        },
        "fields": "userEnteredFormat",
      }
    }
  ]
}

If I misunderstand your question, please tell me. I would like to modify it.

Edit :

When you want to reflect the format for a lot of cells, you can use repeatCell. The request body is as follows. In this sample, all cells in the range are modified .

Modified request :

{
  "requests": 
  [
    {
      "repeatCell": 
      {
        "cell": 
        {
          "userEnteredFormat": 
          {
            "horizontalAlignment": align ,   #'CENTER','LEFT','RIGHT',
            "textFormat":
             {
               "fontFamily": fontFamily,
               "fontSize": fontSize
             }
          }
        },
        "range": 
        {
          "sheetId": sheetId,
          "startRowIndex": startRowIndex,
          "endRowIndex": endRowIndex,
          "startColumnIndex": startColumnIndex,
          "endColumnIndex": endColumnIndex
        },
        "fields": "userEnteredFormat"
      }
    }
  ]
}

Reference :

  • RepeatCellRequest