PHP code example of merceslab / spreadsheet-client

1. Go to this page and download the library: Download merceslab/spreadsheet-client 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/ */

    

merceslab / spreadsheet-client example snippets


    
    
    $credentials = file_get_contents('/var/secrets/credentials.json'); // Get the content of the JSON credentials file generated by Google (either from a file or via an environment variable eg. $credentials = getenv('GOOGLE_CREDENTIALS'))
    $googleServiceSheets = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createServiceSheets($credentials);

    
    
    /** @var \Google_Service_Sheets $googleServiceSheets */
    $client = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createClient($googleServiceSheets);
    $rows = $client->read(
        'fileId', /* ID of the google document */
        'Sheet1', /* Name of the sheet where your table is (default Sheet1) */
        'A1:H1', /* Range of the table header row (default A1) */
        0, /* Offset - number of rows to be ignored at the start of the table (default 0) */
        100 /* Limit - maximum number of rows to return (default -1 = unlimited) */
    );
    
    foreach ($rows as $row) {
        $myData = $row[0];
        // Process $row which is a sequential array
    }
    
    $data = ['foo', 'bar',];
  
    $client->write($data, 'fileId', 'Sheet1', 'A1:H1'); // range is optional, and should match the headers of the table into which you want to export your data. Leaving it blank lets Google auto determine where your data should go (which is fine if you only have one table in the spreadsheet)

    
    
    /** @var \Google_Service_Sheets $googleServiceSheets */
    $file = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createFile($googleServiceSheets, 'myFileId');
    $rows = $file->read(
        'Sheet1', /* Name of the sheet where your table is (default Sheet1) */
        'A1:H1', /* Range of the table header row (default A1) */
        0, /* Offset - number of rows to be ignored at the start of the table (default 0) */
        100 /* Limit - maximum number of rows to return (default -1 = unlimited) */
    );
    
    foreach ($rows as $row) {
        $myData = $row[0];
        // Process $row which is a sequential array
    }
    
    $data = ['foo', 'bar',];
  
    $file->write($data, 'Sheet1', 'A1:H1'); // range is optional, and should match the headers of the table into which you want to export your data. Leaving it blank lets Google auto determine where your data should go (which is fine if you only have one table in the spreadsheet)

    
    
    /** @var \Google_Service_Sheets $googleServiceSheets */
    $sheet = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createSheet($googleServiceSheets, 'myFileId', 'mySheet');
    $rows = $sheet->read(
        'A1:H1', /* Range of the table header row (default A1) */
        0, /* Offset - number of rows to be ignored at the start of the table (default 0) */
        100 /* Limit - maximum number of rows to return (default -1 = unlimited) */
    );
    
    foreach ($rows as $row) {
        $myData = $row[0];
        // Process $row which is a sequential array
    }
    
    $data = ['foo', 'bar',];
  
    $sheet->write($data, 'A1:H1'); // range is optional, and should match the headers of the table into which you want to export your data. Leaving it blank lets Google auto determine where your data should go (which is fine if you only have one table in the spreadsheet)

    
    
    /** @var \Google_Service_Sheets $googleServiceSheets */
    $table = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createTable($googleServiceSheets, 'myFileId', 'mySheet', 'A1:H1');
    $rows = $table->read(
        0, /* Offset - number of rows to be ignored at the start of the table (default 0) */
        100 /* Limit - maximum number of rows to return (default -1 = unlimited) */
    );
    
    foreach ($rows as $row) {
        $myData = $row[0];
        // Process $row which is a sequential array
    }
    
    $data = ['foo', 'bar',];
  
    $table->write($data);