Last active
December 12, 2015 09:49
-
-
Save jancbeck/4754316 to your computer and use it in GitHub Desktop.
This function takes a (public) spreadsheet key and optionally sheet name and returns a clean 2-dimensional array of the table. Had to use CURL for my projects. You may use file_get_contents or fopen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function query_google_spreadsheets( $key, $query, $sheet_name = '' ) { | |
// queries a spreadsheet url and returns a clean array | |
$sheet_url = 'https://spreadsheets.google.com/tq?key='. $key .'&tq='. $query .'&sheet='. $sheet_name; | |
$str = file_get_contents( $sheet_url ); | |
// clean json string | |
$str = str_replace( "// Data table response\ngoogle.visualization.Query.setResponse(", '', $str ); | |
$str = rtrim( $str, ');' ); | |
$str = json_decode( $str ); | |
// flatten array | |
$arr = array(); | |
foreach ( $str->table->rows as $row ) : | |
$new_row = array(); | |
foreach ( $row->c as $cell ) : | |
$new_row[] = $cell->v; | |
endforeach; | |
$arr[] = $new_row; | |
endforeach; | |
return $arr; | |
} ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment