【问题标题】:Changing Cell Colour With Google Sheets API (PHP)使用 Google Sheets API (PHP) 更改单元格颜色
【发布时间】:2020-11-16 04:50:22
【问题描述】:

我正在尝试通过 PHP 中的 Google Sheets API 更改范围颜色。

我做了大约一个小时的研究。下面的代码是我所掌握的。

$requests = [
      // Change the spreadsheet's title.
      new Google_Service_Sheets_Request([
          'updateSpreadsheetProperties' => [
              'properties' => [
                  'title' => "The Title"
              ],
              'fields' => 'title'
          ],
          'UpdateCellsRequest' => [
              'properties' => [
                  'range' => "Sheet1!A1",
                  'backgroundColor' => "#000"
              ],
              'fields' => ''
          ]
      ])
    ];

    // Add additional requests (operations) ...
    $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
        'requests' => $requests
    ]);

    $response = $GoogleSheetsAPIHandler->sheets->spreadsheets->batchUpdate("SHEETID", $batchUpdateRequest);

    print_r($response);

如果我拿出这个:

'UpdateCellsRequest' => [
  'properties' => [
      'range' => "Sheet1!A1",
      'backgroundColor' => "#000"
  ],
  'fields' => ''
]

然后代码可以更新工作表标题。但是,我似乎无法更新范围颜色。

任何建议将不胜感激!

【问题讨论】:

    标签: php google-sheets-api


    【解决方案1】:

    我相信你的目标和情况如下。

    • 您想使用 googleapis for php 更改单元格的背景颜色。
    • 您已经能够使用 Sheets API 为 Google 电子表格获取和输入值。

    修改点:

    • 当你想使用Sheets API的batchUpdate方法时,请将每个请求放到requests数组的每个元素上。
    • 我认为您脚本中 UpdateCellsRequest 的请求正文不正确。
      • 从你I'm trying to change a ranges colour via the Google Sheets API in PHP.的问题来看,当你想用一种颜色改变多个单元格的背景颜色时,我认为RepeatCellRequest可能是合适的。

    在这个答案中,我想提出一个修改后的脚本,用于使用一种颜色更改多个单元格。当你的脚本被修改后,变成如下。

    修改脚本:

    使用前,请设置工作表ID。

    $requests = [
        new Google_Service_Sheets_Request([
            'updateSpreadsheetProperties' => [
                'properties' => [
                    'title' => "The Title"
                ],
                'fields' => 'title'
            ]
        ]),
        new Google_Service_Sheets_Request([
            'repeatCell' => [
                'cell' => [
                    'userEnteredFormat' => [
                        'backgroundColor' => [
                            'red' => 1,
                            'green' => 0,
                            'blue' => 0
                        ]
                    ]
                ],
                'range' => [
                    'sheetId' => $sheetId,  // <--- Please set the sheet ID.
                    'startRowIndex' => 0,
                    'endRowIndex' => 3,
                    'startColumnIndex' => 0,
                    'endColumnIndex' => 2
                ],
                'fields' => 'userEnteredFormat'
            ]
        ])
    ];
    
    • 当上述请求正文用于 Sheets API 的 batchUpdate 方法时,Spreadsheet 的标题将更改,并且单元格“A1:B3”的背景颜色更改为红色。

    • 如果您想使用 UpdateCellsRequest,您可以使用以下请求正文。在以下请求正文中,单元格“A1:B1”的背景颜色分别更改为红色和绿色。使用 UpdateCellsRequest 时,可以更新每个单元格。关于UpdateCellsRequest的详细信息,请查看官方文档。 Ref

        $requests = [
            new Google_Service_Sheets_Request([
                'updateCells' => [
                    'rows' => array([
                        'values' => array(
                            ['userEnteredFormat' => [
                                'backgroundColor' => [
                                    'red' => 1,
                                    'green' => 0,
                                    'blue' => 0
                                ]
                            ]],
                            ['userEnteredFormat' => [
                                'backgroundColor' => [
                                    'red' => 0,
                                    'green' => 1,
                                    'blue' => 0
                                ]
                            ]]
                        )
                    ]),
                    'range' => [
                        'sheetId' => $sheetId,  // <--- Please set the sheet ID.
                        'startRowIndex' => 0,
                        'startColumnIndex' => 0,
                    ],
                    'fields' => 'userEnteredFormat'
                ]
            ])
        ];
      

    参考资料:

    【讨论】:

    • 您好,谢谢您的回答! :) 我试过修改后的脚本代码。我收到此错误:pastebin.com/whDRYqw4
    • @Jack Trowbridge 感谢您的回复。我带来的不便表示歉意。尽管我不确定您当前的整个脚本,但从您的错误消息来看,我认为您可能使用了电子表格 ID 作为$sheetId。工作表 ID 与电子表格 ID 不同。如果我的猜测是正确的,请修改它。您可以在 this official document 看到工作表 ID。如果这不能解决您当前的问题,您能否提供整个脚本来复制您的问题。借此,我想确认一下。
    • 嘿,耶耶!是的,它现在可以工作了:) 索引是否从 0 开始?当我在 PHP 中更改行索引为 4 的单元格时。然后在我的 Google 表格中,它位于第 5 行...它还会重置该单元格中的所有其他格式。比如大胆、大小、对齐等……有没有办法阻止它?或者我将如何更改该格式?它会在“userEnteredFormat”下
    • @Jack Trowbridge 感谢您的回复。我很高兴你的问题得到了解决。关于您在 Google 电子表格上的第二个问题 Do the indexes start at 0?,当创建新的电子表格时,第一个选项卡的工作表 ID 为 0。看来这是目前的规范。
    • 您好,谢谢您的回复。我在这里问了另一个问题:stackoverflow.com/questions/64886023/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2011-11-22
    相关资源
    最近更新 更多