I'm trying to periodically dump some data from another source (MySQL query) using google-api-php-client
into a specific Google spreadsheet
Final result should be a php script that runs in the cron of my server to periodically dump some data from a SQL query to my specific google spreadsheet.
I have everything from google documentation.
serviceID, Json key, Spreadsheet ID, Range
I'm actually able to read a range from my spreadsheet
The question is... How do I write data in my spreadsheet using google-api-php-client?
This is the code I'm using to read data from the spreadsheet.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__.'/google-api-php-client/src');
require_once 'Google/autoload.php';
$key = json_decode(file_get_contents('myJasonkeyFile.json'));
$service_account_email='[SERVICE_EMAIL]@myproject.iam.gserviceaccount.com';
$appName='project-name';
$scopes = array(Google_Service_Sheets::SPREADSHEETS);
$credentials = new Google_Auth_AssertionCredentials(
$service_account_email, // it's the client email
$scopes, // it's google spreadsheet scope
$key->private_key // here is the private key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$client->setApplicationName($appName);
$service = new Google_Service_Sheets($client);
$spreadsheetId = '984984kjldkjldkd0984lkjdlkdj[randomkey]';
$range = 'hoja1!A1:C4';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
print "No data found.\n";
} else {
print "Name, Major:\n";
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s, %s\n", $row[0], $row[2]);
}
}
Thanks in advance for your help