Skip to content

Commit

Permalink
Merge pull request #1 from sanjuktaghosh7/sheets-snippets
Browse files Browse the repository at this point in the history
Sheets snippets
  • Loading branch information
RajeshGogo authored Jun 1, 2022
2 parents 5539abd + 196b8f0 commit 15b96bd
Show file tree
Hide file tree
Showing 10 changed files with 553 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sheets/snippets/src/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"google/apiclient": "^2.2"
}
}
55 changes: 55 additions & 0 deletions sheets/snippets/src/sheets_append_values.php
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", []);

?>
45 changes: 45 additions & 0 deletions sheets/snippets/src/sheets_batch_get_values.php
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');

?>
60 changes: 60 additions & 0 deletions sheets/snippets/src/sheets_batch_update.php
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');

?>
54 changes: 54 additions & 0 deletions sheets/snippets/src/sheets_batch_update_values.php
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", []);

?>
87 changes: 87 additions & 0 deletions sheets/snippets/src/sheets_conditional_formatting.php
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');
?>
52 changes: 52 additions & 0 deletions sheets/snippets/src/sheets_create.php
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');
?>
Loading

0 comments on commit 15b96bd

Please sign in to comment.