Skip to content

Instantly share code, notes, and snippets.

@svamja
Last active October 9, 2023 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svamja/1357650b02f3ebcb4d631b24222ccb6a to your computer and use it in GitHub Desktop.
Save svamja/1357650b02f3ebcb4d631b24222ccb6a to your computer and use it in GitHub Desktop.
PHP Google Sheets API v4 - Working example to append rows with formatting
<?php
function append_rows_with_formatting($service, $sheet_id) {
$cell = [
'userEnteredFormat' => [
'numberFormat' => [
'type' => 'NUMBER',
'pattern' => '[Color22]0.00', // '[Blue]#.##'
],
"backgroundColor" => [
"red" => 0.2,
"green" => 1.0,
"blue" => 0.5,
],
],
'userEnteredValue' => [
'numberValue' => 23
],
'note' => 'previous value: 44'
];
$cells = [ $cell, $cell ];
$row = [
'values' => $cells
];
$rows[] = $row;
$appendCellsRequest = [
'fields' => '*',
'rows' => $rows
];
$request = [
'appendCells' => $appendCellsRequest
];
$batchUpdate = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => [ $request ]
]);
$service->spreadsheets->batchUpdate($sheet_id, $batchUpdate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment