代码之家  ›  专栏  ›  技术社区  ›  jason

使用updatecells api请求更新多个单元格的格式

  •  1
  • jason  · 技术社区  · 6 年前

    我正在尝试将单元格区域居中对齐,但只有该区域中的第一个单元格使用指定格式更新。

    这是我的代码:

    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"
              }
            }
          ]
        }
    

    如果我记录这些值-即。 print (startRowIndex, endRowIndex, startColumnIndex, endColumnIndex) -它们是正确的(例如 0 1 27 30 ),但只有第一个单元格更新为格式,而不是整个范围。

    怎么回事?如何将指定的格式应用于整个范围?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Tanaike    6 年前

    你想更新“ab1:ad1”( {startRowIndex: 0, endRowIndex: 1, startColumnIndex: 27, endColumnIndex: 30} )如果我的理解是正确的,那么这次修改怎么样?

    修改点:

    • 更新3列时,还需要更新值。在您的情况下,需要更新3列。所以需要创造 {values: [{userEnteredFormat: ###}, {userEnteredFormat: ###}, {userEnteredFormat: ###}]} .

    修改请求:

    {
      "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",
          }
        }
      ]
    }
    

    如果我误解了你的问题,请告诉我。我想修改一下。

    编辑:

    如果要反映许多单元格的格式,可以使用 repeatCell . 请求主体如下。在此示例中,该范围内的所有单元格都将被修改。

    修改请求:

    {
      "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"
          }
        }
      ]
    }
    

    参考文献: