-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sanjuktaghosh7/sheets-snippets
Sheets snippets
- Loading branch information
Showing
10 changed files
with
553 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"google/apiclient": "^2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
|
||
// [START sheets_append_values] | ||
function appendValues($spreadsheetId, $range, $valueInputOption, | ||
$_values) | ||
{ | ||
|
||
$client = new Google\Client(); | ||
$client->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", []); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
// [START sheets_batch_get_values] | ||
function batchGetValues($spreadsheetId, $_ranges) | ||
{ | ||
$client = new Google\Client(); | ||
$client->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'); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
// [START sheets_batch_update] | ||
function batchUpdate($spreadsheetId, $title, $find, $replacement) | ||
{ | ||
$client = new Google\Client(); | ||
$client->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'); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
<?php | ||
/** | ||
* Copyright 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
// [START sheets_batch_update_values] | ||
function batchUpdateValues($spreadsheetId, $range, $valueInputOption, | ||
$_values) | ||
{ | ||
$client = new Google\Client(); | ||
$client->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", []); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
<?php | ||
/** | ||
* Copyright 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
// [START sheets_conditional_formatting] | ||
function conditionalFormatting($spreadsheetId) | ||
{ | ||
$client = new Google\Client(); | ||
$client->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'); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Copyright 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
|
||
|
||
//[START sheets_create] | ||
/** | ||
* create an empty spread sheet | ||
* | ||
*/ | ||
function create($title) | ||
{ | ||
|
||
$client = new Google\Client(); | ||
$client->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'); | ||
?> |
Oops, something went wrong.