diff --git a/sheets/snippets/src/composer.json b/sheets/snippets/src/composer.json new file mode 100644 index 0000000..8cb5960 --- /dev/null +++ b/sheets/snippets/src/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "google/apiclient": "^2.2" + } +} \ No newline at end of file diff --git a/sheets/snippets/src/sheets_append_values.php b/sheets/snippets/src/sheets_append_values.php new file mode 100644 index 0000000..4b37db2 --- /dev/null +++ b/sheets/snippets/src/sheets_append_values.php @@ -0,0 +1,55 @@ +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + try { + + $values = [ + [ + ], + ]; + // [START_EXCLUDE silent] + $values = $_values; + // [END_EXCLUDE] + $body = new Google_Service_Sheets_ValueRange([ + 'values' => $values + ]); + $params = [ + 'valueInputOption' => $valueInputOption + ]; + $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params); + printf("%d cells appended.", $result->getUpdates()->getUpdatedCells()); + return $result; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } +} +// [END sheets_append_values] +appendValues('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8', 'Sheet1!A1:B2', "RAW", []); + + ?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_batch_get_values.php b/sheets/snippets/src/sheets_batch_get_values.php new file mode 100644 index 0000000..1b436fa --- /dev/null +++ b/sheets/snippets/src/sheets_batch_get_values.php @@ -0,0 +1,45 @@ +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + try { + + $ranges = []; + $ranges = $_ranges; + $params = array( + 'ranges' => $ranges + ); + $result = $service->spreadsheets_values->batchGet($spreadsheetId, $params); + printf("%d ranges retrieved.", count($result->getValueRanges())); + return $result; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } + +} +// [END sheets_batch_get_values] + batchGetValues('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8', 'Sheet1!A1:B2'); + + ?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_batch_update.php b/sheets/snippets/src/sheets_batch_update.php new file mode 100644 index 0000000..5a8b95d --- /dev/null +++ b/sheets/snippets/src/sheets_batch_update.php @@ -0,0 +1,60 @@ +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + try { + + $requests = [ + new Google_Service_Sheets_Request([ + 'updateSpreadsheetProperties' => [ + 'properties' => [ + 'title' => $title + ], + 'fields' => 'title' + ] + ]), + new Google_Service_Sheets_Request([ + 'findReplace' => [ + 'find' => $find, + 'replacement' => $replacement, + 'allSheets' => true + ] + ]) + ]; + $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ + 'requests' => $requests + ]); + $response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest); + $findReplaceResponse = $response->getReplies()[1]->getFindReplace(); + printf("%s replacements made.\n", + $findReplaceResponse->getOccurrencesChanged()); + return $response; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } +} +// [END sheets_batch_update] + batchUpdate('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8','title', 'abc', 'def'); + + ?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_batch_update_values.php b/sheets/snippets/src/sheets_batch_update_values.php new file mode 100644 index 0000000..372cb75 --- /dev/null +++ b/sheets/snippets/src/sheets_batch_update_values.php @@ -0,0 +1,54 @@ + +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + $values = [ + [ + ], + ]; + try { + + $values = $_values; + $data = []; + $data[] = new Google_Service_Sheets_ValueRange([ + 'range' => $range, + 'values' => $values + ]); + $body = new Google_Service_Sheets_BatchUpdateValuesRequest([ + 'valueInputOption' => $valueInputOption, + 'data' => $data + ]); + $result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body); + printf("%d cells updated.", $result->getTotalUpdatedCells()); + return $result; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } +} +// [END sheets_batch_update_values] + batchUpdateValues('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8','Sheet1!A1:B2',"RAW", []); + + ?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_conditional_formatting.php b/sheets/snippets/src/sheets_conditional_formatting.php new file mode 100644 index 0000000..68fcef9 --- /dev/null +++ b/sheets/snippets/src/sheets_conditional_formatting.php @@ -0,0 +1,87 @@ + +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + + try { + $myRange = [ + 'sheetId' => 0, + 'startRowIndex' => 1, + 'endRowIndex' => 11, + 'startColumnIndex' => 0, + 'endColumnIndex' => 4, + ]; + + $requests = [ + new Google_Service_Sheets_Request([ + 'addConditionalFormatRule' => [ + 'rule' => [ + 'ranges' => [$myRange], + 'booleanRule' => [ + 'condition' => [ + 'type' => 'CUSTOM_FORMULA', + 'values' => [['userEnteredValue' => '=GT($D2,median($D$2:$D$11))']] + ], + 'format' => [ + 'textFormat' => ['foregroundColor' => ['red' => 0.8]] + ] + ] + ], + 'index' => 0 + ] + ]), + new Google_Service_Sheets_Request([ + 'addConditionalFormatRule' => [ + 'rule' => [ + 'ranges' => [$myRange], + 'booleanRule' => [ + 'condition' => [ + 'type' => 'CUSTOM_FORMULA', + 'values' => [['userEnteredValue' => '=LT($D2,median($D$2:$D$11))']] + ], + 'format' => [ + 'backgroundColor' => ['red' => 1, 'green' => 0.4, 'blue' => 0.4] + ] + ] + ], + 'index' => 0 + ] + ]) + ]; + + $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ + 'requests' => $requests + ]); + $response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest); + printf("%d cells updated.", count($response->getReplies())); + return $response; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } +} +// [END sheets_conditional_formatting] + conditionalFormatting('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8'); + ?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_create.php b/sheets/snippets/src/sheets_create.php new file mode 100644 index 0000000..1eee7b5 --- /dev/null +++ b/sheets/snippets/src/sheets_create.php @@ -0,0 +1,52 @@ +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + try { + + $spreadsheet = new Google_Service_Sheets_Spreadsheet([ + 'properties' => [ + 'title' => $title + ] + ]); + $spreadsheet = $service->spreadsheets->create($spreadsheet, [ + 'fields' => 'spreadsheetId' + ]); + printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId); + return $spreadsheet->spreadsheetId; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } + } + // [END sheets_create] + create('title'); +?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_get_values.php b/sheets/snippets/src/sheets_get_values.php new file mode 100644 index 0000000..7ad6866 --- /dev/null +++ b/sheets/snippets/src/sheets_get_values.php @@ -0,0 +1,42 @@ +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + $result = $service->spreadsheets_values->get($spreadsheetId, $range); + $numRows = $result->getValues() != null ? count($result->getValues()) : 0; + printf("%d rows retrieved.", $numRows); + return $result; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } +} +// [END sheets_get_values] + getValues('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8', 'Sheet1!A1:B2'); +?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_pivot_tables.php b/sheets/snippets/src/sheets_pivot_tables.php new file mode 100644 index 0000000..574507e --- /dev/null +++ b/sheets/snippets/src/sheets_pivot_tables.php @@ -0,0 +1,106 @@ +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + try { + + $requests = [ + new Google_Service_Sheets_Request([ + 'addSheet' => [ + 'properties' => [ + 'title' => 'Sheet 1' + ] + ] + ]), + new Google_Service_Sheets_Request([ + 'addSheet' => [ + 'properties' => [ + 'title' => 'Sheet 2' + ] + ] + ]) + ]; + // Create two sheets for our pivot table + $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([ + 'requests' => $requests + ]); + $batchUpdateResponse = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest); + $sourceSheetId = $batchUpdateResponse->replies[0]->addSheet->properties->sheetId; + $targetSheetId = $batchUpdateResponse->replies[1]->addSheet->properties->sheetId; + // [START sheets_pivot_tables] + $requests = [ + 'updateCells' => [ + 'rows' => [ + 'values' => [ + [ + 'pivotTable' => [ + 'source' => [ + 'sheetId' => $sourceSheetId, + 'startRowIndex' => 0, + 'startColumnIndex' => 0, + 'endRowIndex' => 20, + 'endColumnIndex' => 7 + ], + 'rows' => [ + [ + 'sourceColumnOffset' => 1, + 'showTotals' => true, + 'sortOrder' => 'ASCENDING', + ], + ], + 'columns' => [ + [ + 'sourceColumnOffset' => 4, + 'sortOrder' => 'ASCENDING', + 'showTotals' => true, + ] + ], + 'values' => [ + [ + 'summarizeFunction' => 'COUNTA', + 'sourceColumnOffset' => 4 + ] + ], + 'valueLayout' => 'HORIZONTAL' + ] + ] + ] + ], + 'start' => [ + 'sheetId' => $targetSheetId, + 'rowIndex' => 0, + 'columnIndex' => 0 + ], + 'fields' => 'pivotTable' + ] + ]; + return $batchUpdateResponse; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } +} +// [END sheets_pivot_tables] + pivotTables('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8'); + ?> \ No newline at end of file diff --git a/sheets/snippets/src/sheets_update_values.php b/sheets/snippets/src/sheets_update_values.php new file mode 100644 index 0000000..30b962b --- /dev/null +++ b/sheets/snippets/src/sheets_update_values.php @@ -0,0 +1,47 @@ +useApplicationDefaultCredentials(); + $client->addScope(Google\Service\Drive::DRIVE); + $service = new Google_Service_Sheets($client); + try { + $values = $_values; + $body = new Google_Service_Sheets_ValueRange([ + 'values' => $values + ]); + $params = [ + 'valueInputOption' => $valueInputOption + ]; + $result = $service->spreadsheets_values->update($spreadsheetId, $range, + $body, $params); + printf("%d cells updated.", $result->getUpdatedCells()); + return $result; + } catch (Exception $e) { + echo 'Message: ' . $e->getMessage(); + } +} +// [END sheets_update_values] + updateValues('1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8','Sheet1!A1:B2',"RAW", []); + ?> \ No newline at end of file