Updating Google Sheets manually has been a tedious task always as the data to be worked upon is supposed to be fetched from a third-party source. However, it also made an important step for keeping track of certain information.
Since nothing is unworkable, we came up with a solution for managing sheets automatically- to simply reduce efforts and save time. This solution encompasses integrating PHP with Google Sheets API.
This blog will give you insights on what is Google Sheets API, how to integrate PHP with Google Sheets API, and how it can reduce your manual efforts.
Google Sheets API is an online spreadsheet app that allows users to create, edit, and format spreadsheets simultaneously with other users. The new API lets users access data by code as well.
Sheets API lets developers programmatically perform the following tasks:
Firstly, we will get a quick check on the pre-requisite to work with Google Sheets API.
You’ll need to install composer for getting started-
A composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on to manage installations and updates automatically on their own.
These are the following steps that you need to follow for successful integration-
Step 1
Turn on the Google Sheets API and get credentials for your project.
composer require google/apiclient:^2.0
cat credentials.json
How did I implement it?
require __DIR__ . '/vendor/autoload.php';
//Reading data from spreadsheet.
$client = new \Google_Client();
$client->setApplicationName('Google Sheets and PHP');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheetId = "Your SpreadsheetID"; //It is present in your URL
$get_range = "Your Sheet Name! Range”;
Note: Sheet name is found in the bottom of your sheet and range can be an example
”A2: B10” or “A2: C50” or “B1: B10” etc.
//Request to get data from spreadsheet.
$response = $service->spreadsheets_values->get($spreadsheetId, $get_range);
$values = $response->getValues();
2. Updating the data in your google sheet
$update_range = "Your Sheet Name! Range”;
$values = [[$value1, $value2]];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => 'RAW'
];
$update_sheet = $service->spreadsheets_values->update($spreadsheetId, $update_range, $body, $params);
Open your terminal and run this command
Note: You need to run this command in the directory where your PHP file is present.
Conditional Formatting was one of the features that Google Sheets API provides. I honestly love this feature because you can manage things dynamically using your codebase.
There are a bunch of other features also provided by Google APIs which you can implement as per your requirements.
The integration of PHP script with Google Sheets API facilitates users in updating Google Sheets automatically - be its rows or columns, whatever instructions you provide. This way, an entire sheet can be updated within a few minutes.
You can also use its various features to avoid manual labor and re-organize things with as little efforts as possible.