PHP code example of sectsect / google-spreadsheet-to-db

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

    

sectsect / google-spreadsheet-to-db example snippets


  define( 'GOOGLE_SS2DB_CLIENT_SECRET_PATH', '/path/to/your/client_secret.json' );
  

add_filter( 'google_ss2db_before_save', function ( $row, $worksheet_id, $worksheet_name, $sheet_name ) {
  // Example
  if ( $worksheet_name === 'My Spreadsheet' && $sheet_name === 'Sheet1' ) {
    // Do something.

    return $something;
  }

  return $row;
}, 10, 3 );

add_filter( 'google_ss2db_after_save', function ( $data ) {
  if ( 'My Spreadsheet' === $data['worksheet_name'] ) {
    // $id              = $data['id'];
    // $date            = $data['date'];
    // $title           = $data['title'];
    // $value           = $data['value'];
    // $work_sheet_id   = $data['worksheet_id'];
    // $work_sheet_name = $data['worksheet_name'];
    // $sheet_name      = $data['sheet_name'];
    // $result          = $data['result']; // `int|false` The number of rows inserted, or false on error.

    // Example
    my_callback( $data );
  }
});

new Google_Spreadsheet_To_DB_Query();

$sheet = new Google_Spreadsheet_To_DB_Query();
$rows  = $sheet->getrow();
foreach ( $rows as $row ) {
  $id   = $row->id;
  $date = $row->date;
  $val  = json_decode( $row->value );
}

$args = array(
  'orderby' => 'id',
  'order'   => 'ASC',
  'limit'   => 3,
  'offset'  => 3,
);
$sheet = new Google_Spreadsheet_To_DB_Query( $args );
$rows  = $sheet->getrow();
foreach ( $rows as $row ) {
  $id   = $row->id;
  $date = $row->date;
  $val  = json_decode( $row->value );
}

$args = array(
  'where' => array(
    array(
      'key'   => 'id',
      'value' => 3,
    )
  ),
);

$args = array(
  'orderby' => 'id',
  'order'   => 'ASC',
  'limit'   => 3,
  'where'   => array(
    array(
      'key'     => 'worksheet_name',
      'value'   => 'My Spreadsheet',
      'compare' => '='
    ),
  ),
);

$args = array(
  'where' => array(
    array(
      'key'     => 'date',
      'value'   => '2020-08-01 12:34:56',
      'compare' => '>=',
    )
  ),
);

$args = array(
  'orderby' => 'id',
  'order'   => 'DESC',
  'limit'   => 10,
  'offset'  => 10,
  'where'   => array(
    'relation' => 'AND', // or 'OR'
    array(
      'key'     => 'date',
      'value'   => '2020-08-01 12:34:56',
      'compare' => '>='
    ),
    array(
      'key'     => 'worksheet_name',
      'value'   => 'My Spreadsheet',
      'compare' => '='
    ),
  ),
);