2017 © Pedro Peláez
 

library php-google-spreadsheet-client

Google Spreadsheet PHP Client

image

asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  • Sunday, March 25, 2018
  • by Asim
  • Repository
  • 40 Watchers
  • 488 Stars
  • 632,974 Installations
  • PHP
  • 9 Dependents
  • 1 Suggesters
  • 151 Forks
  • 36 Open issues
  • 31 Versions
  • 6 % Grown

The README.md

Build Status, (*1)

This library is no longer maintained. Please use the official Google PHP Client

Contents

Introduction

This library provides a simple interface to the Google Spreadsheet API v3., (*2)

There are a couple of important things to note., (*3)

  • This library requires a valid OAuth access token to work but does not provide any means of generating one. The Google APIs Client Library for PHP has all the functionality required for generating and refreshing tokens so it would have been a waste of time duplicating the official Google library.
  • You can not create spreadsheets using this library, as creating spreadsheets is not part of the Google Spreadsheet API, rather it's part of the Google Drive API. See the official Google APIs Client Library for PHP.

I strongly recommend you read through the official Google Spreadsheet API documentation to get a grasp of the concepts., (*4)

Usage

Installation

Using Composer is the recommended way to install it., (*5)

1 - Add "asimlqt/php-google-spreadsheet-client" as a dependency in your project's composer.json file., (*6)

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

2 - Download and install Composer., (*7)

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

3 - Install your dependencies., (*8)

php composer.phar install

4 - Require Composer's autoloader., (*9)

require 'vendor/autoload.php';

Bootstrapping

The first thing you will need to do is initialize the service request factory:, (*10)

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

$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);

Note: For Windows users, you can disable the ssl verification by '$serviceRequest->setSslVerifyPeer(false)', (*11)

Spreadsheet

Retrieving a list of spreadsheets

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();

Once you have a SpreadsheetFeed you can iterate over the spreadsheets using a foreach loop by calling the 'getEntries()' method or you can retrieve a single spreadsheet by it's title., (*12)

$spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');

Note: The 'getByTitle' method will return the first spreadsheet found with that title if you have more than one spreadsheet with the same name., (*13)

Retrieving a public spreadsheet

A public spreadsheet is one that has been "published to the web". This does not require authentication. e.g., (*14)

$serviceRequest = new DefaultServiceRequest("");
ServiceRequestFactory::setInstance($serviceRequest);

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$worksheetFeed = $spreadsheetService->getPublicSpreadsheet("spreadsheet-id");

The spreadsheet id can be copied from the url of the actual spreadsheet in Google Drive., (*15)

Worksheet

Retrieving a list of worksheets

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

$spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
$worksheetFeed = $spreadsheet->getWorksheetFeed();

You can loop over each worksheet using 'getEntries()' or retrieve a single worksheet by it's title., (*17)

$worksheet = $worksheetFeed->getByTitle('Sheet1');

Adding a worksheet

To create a new worksheet simply use the 'addWorksheet()' method. This takes 3 arguments: - Worksheet name - Number of rows - Number of columns, (*18)

$spreadsheet->addWorksheet('New Worksheet', 50, 20);

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., (*19)

To add headers to a worksheet, use the following:, (*20)

$cellFeed = $worksheet->getCellFeed();

$cellFeed->editCell(1,1, "Row1Col1Header");
$cellFeed->editCell(1,2, "Row1Col2Header");

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., (*21)

Deleting a worksheet

It's also possible to delete a worksheet., (*22)

$worksheet->delete();

List feed

List feeds work at the row level. Each entry will contain the data for a specific row., (*23)

Note: You can not use formulas with the list feed. If you want to use formulas then you must use the cell feed (described below)., (*24)

Retrieving a list feed

$listFeed = $worksheet->getListFeed();

Once you have a list feed you can loop over each entry., (*25)

foreach ($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., (*26)

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., (*27)

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., (*28)

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

$listFeed = $worksheet->getListFeed(["sq" => "age > 45", "reverse" => "true"]);

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

Adding a list row

$listFeed->insert(["name" => "Someone", "age" => 25]);

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., (*31)

Updating a list row

$entries = $listFeed->getEntries();
$listEntry = $entries[0];

$values = $listEntry->getValues();
$values["name"] = "Joe";
$listEntry->update($values);

Cell feed

Cell feed deals with individual cells. A cell feed is a collection of cells (of type CellEntry), (*32)

Retrieving a cell feed

$cellFeed = $worksheet->getCellFeed();

Updating a cell

You can retrieve a single cell from the cell feed if you know the row and column numbers for that specific cell., (*33)

$cell = $cellFeed->getCell(10, 2);

You can then update the cell value using the 'update' method. The value can be a primitive value or a formula e.g., (*34)

$cell->update("=SUM(B2:B9)");

Updating multiple cells with a batch request

When attempting to insert data into multiple cells then consider using the batch request functionality to improve performance., (*35)

To use the batch request functionality you need access to a cell feed first. You can not use batch requests with list feeds., (*36)

$cellFeed = $worksheet->getCellFeed();

$batchRequest = new Google\Spreadsheet\Batch\BatchRequest();
$batchRequest->addEntry($cellFeed->createCell(2, 1, "111"));
$batchRequest->addEntry($cellFeed->createCell(3, 1, "222"));
$batchRequest->addEntry($cellFeed->createCell(4, 1, "333"));
$batchRequest->addEntry($cellFeed->createCell(5, 1, "=SUM(A2:A4)"));

$batchResponse = $cellFeed->insertBatch($batchRequest);

The Versions

25/03 2018

v4.x-dev

4.9999999.9999999.9999999-dev https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

25/03 2018

dev-master

9999999-dev https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

09/11 2016

v3.0.2

3.0.2.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

06/07 2016

v3.x-dev

3.9999999.9999999.9999999-dev https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

22/04 2016

3.0.1

3.0.1.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

03/04 2016

3.0.0

3.0.0.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.6.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

13/12 2015

2.3.7

2.3.7.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

24/11 2015

2.3.6

2.3.6.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

21/06 2015

2.3.5

2.3.5.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

12/04 2015

2.3.4

2.3.4.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

22/02 2015

2.3.3

2.3.3.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

22/02 2015

2.3.2

2.3.2.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

22/02 2015

2.3.1

2.3.1.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Asim Liaquat

google spreadsheet

02/12 2014

2.3.0

2.3.0.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

02/12 2014

2.2.4

2.2.4.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

18/11 2014

2.2.3

2.2.3.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

18/11 2014

2.2.2

2.2.2.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

17/11 2014

2.2.1

2.2.1.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

17/08 2014

2.2.0

2.2.0.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

10/08 2014

2.1.8

2.1.8.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

09/08 2014

2.1.7

2.1.7.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

05/08 2014

dev-dev-stream-request

dev-dev-stream-request https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

05/08 2014

2.1.6

2.1.6.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

18/06 2014

2.1.5

2.1.5.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

07/06 2014

2.1.4

2.1.4.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

18/05 2014

2.1.3

2.1.3.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

18/05 2014

2.1.2

2.1.2.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

18/05 2014

2.1.1

2.1.1.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

18/05 2014

2.1.0

2.1.0.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

22/02 2014

2.0.0

2.0.0.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.0

 

by Asim Liaquat

google spreadsheet

28/07 2013

1.0

1.0.0.0 https://github.com/asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP Client

  Sources   Download

by Asim Liaquat

google spreadsheet