使用updateCells API请求更新多个单元格的格式 [英] Update format of more than one cell with updateCells API request

查看:129
本文介绍了使用updateCells API请求更新多个单元格的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的代码:

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),但是只有第一个单元格被更新为格式-而不是整个范围.

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?

推荐答案

您要更新"AB1:AD1"({startRowIndex: 0, endRowIndex: 1, startColumnIndex: 27, endColumnIndex: 30}).如果我的理解是正确的,那么该修改如何?

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

  • 更新3列时,还需要更新值.您要更新3列.因此,需要像{values: [{userEnteredFormat: ###}, {userEnteredFormat: ###}, {userEnteredFormat: ###}]}一样创建.
  • 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: ###}]}.
{
  "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.

如果要反映很多单元格的格式,可以使用repeatCell.请求正文如下.在此示例中,该范围内的所有单元格均被修改.

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 .

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

参考:

  • RepeatCellRequest
  • Reference :

    • RepeatCellRequest
    • 这篇关于使用updateCells API请求更新多个单元格的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆