Created
August 30, 2019 09:34
-
-
Save GeeH/8ad387b322b9511a0cfdd575c568553e to your computer and use it in GitHub Desktop.
google sheets access in php
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
<?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