在Google工作表(PHP)中格式化单元格背景和粗体

时间:2019-07-25 17:12:44

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

我正在尝试更改电子表格中几个单元格的背景颜色和粗体值。我知道我有一项有效的服务,因为我正在做一个电子表格值更新,并且可以正常工作。

更改值是可行的,所以我猜测它一定与我的配置有关。我没有收到任何错误,$ result变量仍然为空。任何想法将不胜感激,谢谢。

// known to work from changing the values
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'MY SPREADSHEET ID';

// new code
$r = $g = $b = 0.5;
$a = 1;

$myRange = [
    //'sheetId' => 0, // can be omitted because I'm working on the first sheet
    'startRowIndex' => 5,
    'endRowIndex' => 10,
    //'startColumnIndex' => 0, // can be omitted because default is 0
    'endColumnIndex' => 5,
];
$format = [
    'backgroundColor' => [
        'red' => $r,
        'green' => $g,
        'blue' => $b,
        'alpha' => $a,
    ],
    'textFormat' => [
      'bold' => true
    ]
];


$requests = [
    new Google_Service_Sheets_Request([
        'repeatCell' => [
            'fields' => 'userEnteredFormat.backgroundColor, userEnteredFormat.textFormat.bold',
            'range' => $myRange,
            'cell' => [
                'userEnteredFormat' => $format,
            ],
        ],
    ])
];


$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
  'requests' => $requests
]);
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);

1 个答案:

答案 0 :(得分:0)

解决方案:

我在这里犯了2个错误:

1)我已经思考报告错误,但是没有。 @greybeard让我寻找该错误,此后出现以下错误(这可能对其他人有所帮助):

Uncaught Google_Service_Exception: { "error": { "code": 400, "message": "Invalid requests[0].repeatCell: No grid with id: 1"

2)如您所见,我的网格ID上面的错误被拒绝。这是由于我将sheetId与工作表索引混淆了。谷歌表实际上有一个“真实” ID,您可以通过以下请求获得

$sheetId = $service->spreadsheets->get($spreadsheetId);

这将返回一个包含对象的数组,该对象包含sheetIds。对于索引为0的工作表,将为

$sheetId = $sheetId->sheets[0]->properties->sheetId;

要仅直接访问某些工作表而不是整个电子表格,可以添加“范围”属性

$sheetId = $service->spreadsheets->get($spreadsheetId, ['ranges' => 'NAME_OF_SHEET']);

因此完整的脚本应该是:

$client = getClient(); // this calls my custom function that does the authentication
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'MY SPREADSHEET ID';

// get sheetId of sheet with index 0
$sheetId = $service->spreadsheets->get($spreadsheetId);
$sheetId = $sheetId->sheets[0]->properties->sheetId;

// set colour to a medium gray
$r = $g = $b = 0.5;
$a = 1;

// define range
$myRange = [
    'sheetId' => $sheetId, // IMPORTANT: sheetId IS NOT the sheets index but its actual ID
    'startRowIndex' => 5,
    'endRowIndex' => 10,
    //'startColumnIndex' => 0, // can be omitted because default is 0
    'endColumnIndex' => 5,
];

// define the formatting, change background colour and bold text
$format = [
    'backgroundColor' => [
        'red' => $r,
        'green' => $g,
        'blue' => $b,
        'alpha' => $a,
    ],
    'textFormat' => [
      'bold' => true
    ]
];

// build request
$requests = [
    new Google_Service_Sheets_Request([
        'repeatCell' => [
            'fields' => 'userEnteredFormat.backgroundColor, userEnteredFormat.textFormat.bold',
            'range' => $myRange,
            'cell' => [
                'userEnteredFormat' => $format,
            ],
        ],
    ])
];

// add request to batchUpdate    
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
  'requests' => $requests
]);

// run batchUpdate
$result = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);