使用 Google 张 API (PHP) 更改单元格颜色

Changing Cell Colour With Google Sheets API (PHP)

我正在尝试通过 PHP 中的 Google 工作表 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' => ''
]

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

如有任何建议,我们将不胜感激!

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

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

修改点:

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

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

修改后的脚本:

在使用之前,请设置 sheet 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'
        ]
    ])
];
  • 当上述请求body用于SheetsAPI的batchUpdate方法时,Spreadsheet的标题和单元格的背景颜色发生变化“A1:B3”变为红色。

  • 如果你想使用UpdateCellsRequest,你可以使用下面的请求body。在以下请求 body 中,单元格“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'
              ]
          ])
      ];
    

参考文献: