Last active
April 6, 2019 16:45
-
-
Save kundancool/3c26b39f09764e6d3a32e8bd989adab9 to your computer and use it in GitHub Desktop.
Google Sheet Controller for Laravel
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 | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
class GoogleSheetController extends Controller | |
{ | |
protected $sheets; | |
/** | |
* [getSheet description] | |
* @param [type] $spreadsheetId [description] | |
* @param [type] $range [description] | |
* @return [type] [description] | |
*/ | |
public function getSheet($spreadsheetId) | |
{ | |
/* | |
* We need to get a Google_Client object first to handle auth and api calls, etc. | |
*/ | |
$client = new \Google_Client(); | |
$client->setApplicationName('GoogleSheet'); | |
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]); | |
$client->setAccessType('offline'); | |
/* | |
* The JSON auth file can be provided to the Google Client in two ways, one is as a string which is assumed to be the | |
* path to the json file. This is a nice way to keep the creds out of the environment. | |
* | |
* The second option is as an array. For this example I'll pull the JSON from an environment variable, decode it, and | |
* pass along. | |
*/ | |
$jsonAuth = file_get_contents(storage_path('credentials.json')); | |
$client->setAuthConfig(json_decode($jsonAuth, true)); | |
/* | |
* With the Google_Client we can get a Google_Service_Sheets service object to interact with sheets | |
*/ | |
$this->sheets = new \Google_Service_Sheets($client); | |
} | |
/** | |
* [clearSheet description] | |
* @param [type] $spreadsheetId [description] | |
* @param [type] $range [description] | |
* @return [type] [description] | |
*/ | |
public function clearSheet($spreadsheetId, $range) | |
{ | |
$clear_range = explode('!', $range); | |
return $this->sheets->spreadsheets_values->clear( | |
$spreadsheetId, | |
$clear_range[0], | |
new \Google_Service_Sheets_ClearValuesRequest() | |
); | |
} | |
/** | |
* [getData description] | |
* @param [type] $spreadsheetId [description] | |
* @param [type] $range [description] | |
* @return [type] [description] | |
*/ | |
public function getData($spreadsheetId, $range) | |
{ | |
$this->getSheet($spreadsheetId); | |
return $this->sheets->spreadsheets_values->get($spreadsheetId, $range); | |
} | |
/** | |
* [setData description] | |
* @param [type] $spreadsheetId [description] | |
* @param [type] $range [description] | |
* @param [type] $data [description] | |
*/ | |
public function setData($spreadsheetId, $range, $data) | |
{ | |
$this->getSheet($spreadsheetId); | |
$this->clearSheet($spreadsheetId, $range); // clear sheet | |
$body = new \Google_Service_Sheets_ValueRange([ | |
'majorDimension' => 'ROWS', | |
'values' => $data | |
]); | |
$valueInputOption = [ | |
'valueInputOption' => 'RAW' | |
]; | |
return $this->sheets->spreadsheets_values->update($spreadsheetId, $range, $body, $valueInputOption); | |
} | |
/** | |
* [appendData description] | |
* @param [type] $spreadsheetId [description] | |
* @param [type] $range [description] | |
* @param [type] $data [description] | |
* @return [type] [description] | |
*/ | |
public function appendData($spreadsheetId, $range, $data) | |
{ | |
$this->getSheet($spreadsheetId); | |
$body = new \Google_Service_Sheets_ValueRange([ | |
'majorDimension' => 'ROWS', | |
'values' => $data | |
]); | |
$valueInputOption = [ | |
'valueInputOption' => 'RAW' | |
]; | |
return $this->sheets->spreadsheets_values->append($spreadsheetId, $range, $body, $valueInputOption); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment