Skip to content

Instantly share code, notes, and snippets.

@GeeH
Created August 30, 2019 09:34
Show Gist options
  • Save GeeH/8ad387b322b9511a0cfdd575c568553e to your computer and use it in GitHub Desktop.
Save GeeH/8ad387b322b9511a0cfdd575c568553e to your computer and use it in GitHub Desktop.
google sheets access in php
<?php declare(strict_types=1);
require('vendor/autoload.php');
$client = new Google_Client();
$client->setApplicationName('My Application');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAccessType('offline');
// renamed credentials file you downloaded when you created your credentials from Google Admin
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
// unique Id of your spreadsheet found in URL after the /d/ and before /edit
$spreadsheetId = "1zXkf1rzuDFEJEs_3t6qdvWnb-rkjo7tGoJJ3esdD150";
// range of the cells you want to grab
$range = "congress!D2:F8";
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (empty($values)) {
echo 'No data found' . PHP_EOL;
die();
}
foreach($values as $row) {
echo "{$row[2]} {$row[1]} {$row[0]}" . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment