10up/php-google-spreadsheet-client

Name: php-google-spreadsheet-client

Owner: 10up

Description: A PHP library for accessing and manipulating Google Spreadsheets (For WordPress)

Forked from: asimlqt/php-google-spreadsheet-client

Created: 2015-07-22 15:46:27.0

Updated: 2017-08-17 15:06:31.0

Pushed: 2015-07-22 21:16:16.0

Homepage:

Size: 216

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Introduction

This library provides a simple interface to the Google Spreadsheet API.

There are a couple of important things to note.

I strongly recommend you read through the official Google Spreadsheet API documentation to get a grasp of the concepts.

Usage

Installation

Using composer is the recommended way to install it.

1 - Add “asimlqt/php-google-spreadsheet-client” as a dependency in your project's composer.json file.


"require": {
    "asimlqt/php-google-spreadsheet-client": "2.3.*"
}

2 - Download and install Composer.

 -sS https://getcomposer.org/installer | php

3 - Install your dependencies.

composer.phar install

4 - Require Composer's autoloader.

ire 'vendor/autoload.php';
Bootstrapping

The first thing you will need to do is initialize the service request factory:

Google\Spreadsheet\DefaultServiceRequest;
Google\Spreadsheet\ServiceRequestFactory;

viceRequest = new DefaultServiceRequest($accessToken);
iceRequestFactory::setInstance($serviceRequest);
Retrieving a list of spreadsheets
eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();

SpreadsheetFeed implements ArrayIterator so you can iterate over it using a foreach loop or you can retrieve a single spreadsheet by name.

eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
Retrieving a list of worksheets

You can retrieve a list of worksheets from a spreadsheet by calling the getWorksheets() method.

eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();
eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
ksheetFeed = $spreadsheet->getWorksheets();

You can loop over each worksheet or get a single worksheet by title.

ksheet = $worksheetFeed->getByTitle('Sheet 1');
Adding a worksheet
eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();
eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
eadsheet->addWorksheet('New Worksheet', 50, 20);

The only required parameter is the worksheet name, The row and column count are optional. The default value for rows is 100 and columns is 10.

Deleting a worksheet
eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();
eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
ksheetFeed = $spreadsheet->getWorksheets();
ksheet = $worksheetFeed->getByTitle('New Worksheet');
ksheet->delete();
Working with list-based feeds
Retrieving a list-based feed
eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();
eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
ksheetFeed = $spreadsheet->getWorksheets();
ksheet = $worksheetFeed->getByTitle('Sheet 1');
tFeed = $worksheet->getListFeed();

Once you have a list feed you can loop over each entry.

ach ($listFeed->getEntries() as $entry) {
$values = $entry->getValues();

The getValues() method returns an associative array where the keys are the column names and the values are the cell content.

Note: The Google api converts the column headers to lower case so the column headings might not appear to be the same as what you see in Google Drive using your browser.

Note: If there is data for a particular row which does not have a column header then Google randomly generates a header and as far as i know it always begins with an underscore. Bear in mind that this is not generated by this library.

You can also sort and filter the data so you only retrieve what is required, this is expecially useful for large worksheets.

tFeed = $worksheet->getListFeed(array("sq" => "age > 45", "reverse" => "true"));

To find out all the available options visit https://developers.google.com/google-apps/spreadsheets/#sorting_rows.

Adding a list row
eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();
eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
ksheetFeed = $spreadsheet->getWorksheets();
ksheet = $worksheetFeed->getByTitle('Sheet 1');
tFeed = $worksheet->getListFeed();

 = array('name'=>'John', 'age'=>25);
tFeed->insert($row);

When adding or updating a row the column headers need to match exactly what was returned by the Google API, not what you see in Google Drive.

Updating a list row
eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();
eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
ksheetFeed = $spreadsheet->getWorksheets();
ksheet = $worksheetFeed->getByTitle('Sheet 1');
tFeed = $worksheet->getListFeed();
ries = $listFeed->getEntries();
tEntry = $entries[0];

ues = $listEntry->getValues();
ues['name'] = 'Joe';
tEntry->update($values);
Adding headers to a new worksheet

The Google Spreadsheet API does not allow you to update a list row if headers are not already assigned. So, when you create a new worksheet, before you can add data to a worksheet using the 'Adding/Updating a list row' methods above, you need to add headers.

To add headers to a worksheet, use the following:

eadsheetService = new Google\Spreadsheet\SpreadsheetService();
eadsheetFeed = $spreadsheetService->getSpreadsheets();
eadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
ksheetFeed = $spreadsheet->getWorksheets();
ksheet = $worksheetFeed->getByTitle('Sheet 1');
lFeed = $worksheet->getCellFeed();

lFeed->editCell(1,1, "Row1Col1Header");
lFeed->editCell(1,2, "Row1Col2Header");
lFeed->editCell(1,3, "Row1Col3Header");
lFeed->editCell(1,4, "Row1Col4Header");

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.