stopsopa/google-spreadsheets-api-extension

My own approach to manipulating data in Google Spreadsheets

v1.1.0 2016-04-24 17:29 UTC

This package is auto-updated.

Last update: 2024-05-01 00:11:28 UTC


README

Build Status Latest Stable Version Coverage Status License

DEPRECATED

Created in 2016 - quite old now and not maintained.

stopsopa/google-spreadsheets-api-extension

Instalation

Follow packagist instructions: Packagist

Setup access to google spreadsheet documents

First You need to generate credentials in Google API Console and setup Service account keys (read more). After that you recieve file '.p12' which is a private key and special e-mail address (eq: applicationname@applicationname.iam.gserviceaccount.com) that you should use to give access to api to specific spreadsheets in google-drive.

Setup this library

use Stopsopa\GoogleSpreadsheets\Services\GoogleSpreadsheets;

$service = new GoogleSpreadsheets();

$service->setupByServiceAccountKey(
    'pathtofile.p12', 
    'applicationname@applicationname.iam.gserviceaccount.com'
);

... and now you are ready to go.

Api

Most of methods is pretty self-explanatory so normally you can explore them through IDE or just by exploring returned data structures (especially if you read terminology used in Sheets API), they don't need to be explained here in details, but it is also good thing to list them here. It is also good idea to see tests starting from method testFindWorksheets.

Methods of GoogleSpreadsheets class

Method with default parameters Returned type Additional description
$service->findSpreadsheets($rawResponse = false); array
$service->findWorksheets($key, $rawResponse = false); array
$service->getWorksheetMetadata($key, $wid); array
$service->updateWorksheetMetadata($key, $wid, $title = null, $rows = null, $cols = null); array
$service->findWorksheetData($key, $wid, $rawResponse = false, $filter = array()); array
$service->deleteWorksheet($key, $wid); array
$service->update($key, $wid, $data); array doc
$service->findFirstFreeRowForData($key, $wid); int (1 indexed)
$service->getList($key, $wid); GoogleSpreadsheetsList

Methods of GoogleSpreadsheetsList class

Read more about list based feed.

Method with default parameters Returned type Additional description
$list->add($data); array doc
$list->update($row, $data); array $row is 1 indexed, $data format like above
$list->get($filters = null); array $filters format see this.
Usually use only min-row, max-row.

Commonly used parameters

$rawResponse:

Many of this methods has special parameter $rawResponse, setting up this to true turns returned data structure to raw form, just like it's comes from google api. But in most cases it is better to leave this parameter as is and get more consist and better to iterate through data structure at the output of this methods.

$key

You can find the key in the spreadsheet URL (bolded in below example).

https://docs.google.com/spreadsheets/d/1IAP4HOacD4Az6q_PFfxxxxxxxxxxxxxxxx9KBX-IMO25s/edit?usp=sharing

$wid

This is unique identifier for each worksheet in spreadsheet, You can get these ids by using method findWorksheets.

$filter

This parameter is used to fetch specific rows or columns ranges from worksheet, eq:

  $result = $service->findWorksheetData($key, $wid, false, array(
    'max-col' => 4,
    'max-row' => 3
  ));

For more informations see in google api Fetch specific rows or columns.

Other details

Method GoogleSpreadsheets->update()

    $service->update($key, $wid, array(
        'A1' => "Name",
        'B1' => "Surname",
        'C1' => "Age",
        'D1' => "Weight",
        'E1' => "Height",
        'A2' => "Something else... ",
        'R2C5' => "And again...",
        'R3C2' => '' // empty string to delete value from cell
    ));

Using this method you can use two types of positioning notations. Using method update you can set/update/delete data in many cells by one batch request.

To convert in both ways these two types of formats use this lib.

Method GoogleSpreadsheetsList->add()

    $list->add(array(
        'Name' => 'John',
        'Surname' => 'Smith',
        'Age' => '35'
    ));

To read more about basic assumptions that describes working with list based feeds go here.

License

The MIT License (MIT) Copyright (c) 2016 Szymon Działowski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.