Google表格API v4更新单元格编号格式,而不更改任何其他内容

时间:2016-07-30 19:51:49

标签: google-api-php-client google-sheets-api

我想更新Google表格API v4中的单元格数字格式,而不更改其他任何内容,例如背景颜色。

下面的代码更新了单元格的数字格式,但也重置了背景颜色等内容。我想设置$fields = 'userEnteredFormat.numberFormat'; 会留下其他设置。有没有办法得到我想要的东西而不必指定所有其他格式设置?

<?php

require_once __DIR__ . '/../vendor/autoload.php';

$auth = __DIR__ . '/path-to-auth.json';
$creds = __DIR__ . '/path-to-creds.json';
$spreadsheetId = 'spreadsheet-id';
$sheetId = 0;
$format = '0.0%';

$client = new Google_Client();
$client->setAuthConfig($auth);
$token = file_get_contents($creds);
$client->setAccessToken($token);

$service = new Google_Service_Sheets($client);

$numberFormat = new Google_Service_Sheets_NumberFormat();
$numberFormat->setType('NUMBER');
$numberFormat->setPattern($format);
$cellFormat = new Google_Service_Sheets_CellFormat();
$cellFormat->setNumberFormat($numberFormat);
$cellData = new Google_Service_Sheets_CellData();
$cellData->setUserEnteredFormat($cellFormat);
$rowData = new Google_Service_Sheets_RowData();
$rowData->setValues([$cellData]);
$rows[] = $rowData;

$gridRange = new Google_Service_Sheets_GridRange();
$gridRange->setSheetId($sheetId);
$gridRange->setStartRowIndex(0);
$gridRange->setEndRowIndex(1);
$gridRange->setStartColumnIndex(0);
$gridRange->setEndColumnIndex(1);

$fields = 'userEnteredFormat.numberFormat';

$updateCellsRequest = new Google_Service_Sheets_UpdateCellsRequest();
$updateCellsRequest->setFields($fields);
$updateCellsRequest->setRows($rows);
$updateCellsRequest->setRange($gridRange);

$request = new Google_Service_Sheets_Request();
$request->setUpdateCells($updateCellsRequest);
$batchUpdate = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdate->setRequests([$request]);

$service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdate);

?>

1 个答案:

答案 0 :(得分:0)

在处理该特定请求时,这是服务中的错误。它应该在下周的某个时候修复。抱歉,麻烦。