PHP code example of mach3 / google-spreadsheet

1. Go to this page and download the library: Download mach3/google-spreadsheet library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mach3 / google-spreadsheet example snippets


$client = Google_Spreadsheet::getClient('the/path/to/credential.json');
// Get the sheet instance by sheets_id and sheet name
$sheet = $client->file('XXXxxxXXXXxxxXXXX')->sheet('Sheet1');
// Fetch data from remote (or cache)
$sheet->fetch();
// Flush all rows in the sheet
var_dump($sheet->items);

$sheet->init(array(
  'id',
  'name',
  'age',
  'email',
  'note'
));

// Array
$items = $sheet->select(array(
  'id' => '1'
));
// Closure
$items = $sheet->select(function($row){
  return (int) $row['age'] < 30;
});

// Insert a new row
$sheet->insert(array(
  'name' => 'John',
  'age' => 23,
  'email' => '[email protected]'
));

// Get up-to-date items
$items = $sheet->fetch(true)->items;

// Update rows selected by array
$sheet->update(
  array(
    'email' => '[email protected]'
  ),
  array(
    'name' => 'Tom'
  )
);

// Update rows selected by closure
$sheet->update(
  array(
    'email' => '[email protected]'
  ),
  function($row){
    return $row['name'] === 'Tom';
  }
);

// Get up-to-date items
$items = $sheet->fetch(true)->items;

// Update `B2` cell
$sheet->edit(2, 2, 'Tom');

// Update `C1:C4` cells
$sheet->edit(3, 1, array(1, 'John', 23, '[email protected]'));

// Pass `true` to ignore cache
$items = $sheet->fetch(true)->items;

$sheet->config(array(
  'cache' => true,
  'cache_dir' => __DIR__ . '/cache',
  'cache_expires' => 360
));