I've developed a WordPress plugin called 'CSV-to-SortTable' that imports 
CSV file data, parses it into an indexed array, and outputs it as an table 
using Stuart Langridge's sorttable.js.

The plugin works well, but I want to add an option for users to import data 
from a Google Spreadsheet. Most importantly, I want to re-use the existing 
code as much as possible to avoid any redundancy.

I've spent hours sifting through Google's documentation and countless 
forums, and I think I'm close...but I keep getting a 'Moved Temporarily' 
message which, based on my research, indicates that the URL I'm using gets 
redirected to an authorization page.

Here is what I've got so far:

*PHP Code:*

/* Source = Google Spreadsheet */
if( $atts[gkey] <> '' ) {
    $atts[csv] = 
'https://spreadsheets.google.com/feeds/download/spreadsheets/Export?v=3&key=' . 
$atts[gkey] . '&exportFormat=csv&ndplr=1';
}

/* Source = CSV file or Google Spreadsheet */
if( $atts[csv] <> '' ) {
    $session = curl_init();
    curl_setopt( $session, CURLOPT_HEADER, 0 );
    curl_setopt( $session, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $session, CURLOPT_URL, $atts[csv] );
    $csv_data = curl_exec( $session ) or die( 'CURL ERROR: '.curl_error( 
$session ) );
    curl_close( $session );

    /* Call function to parse .CSV data string into an indexed array. */
    $csv_data_array = $this->parse_csv_data( $csv_data );

    /* Call function to render the table. */
    print_r( $this->table_data );

}

*Explanation:* The value of $atts[gkey] is a user-defined shortcode 
parameter. If it is set to the key of a Google Spreadsheet, I want the 
plugin to define $atts[csv] as a URL that will output the contents of the 
Google Spreadsheet as a CSV file. (Normally, the user would define 
$atts[csv] as the URL of a .CSV file.)

*Testing Site URL:* http://sandbox.mynewsitepreview.com/test/wp-sorttable/

*Public Google Spreadsheet URL:* 
https://docs.google.com/spreadsheet/ccc?key=0Aj-ZlsTpsY_wdEhIdTdhYzlmTmdEMXhjVEJaWERtUFE

*CSV File URL:* http://mynewsitepreview.com/plugins/sample.csv

On this testing site, I am first using the shortcode with the 'csv' 
parameter set to the URL of a CSV file, then using the shortcode again but 
with the 'gkey' parameter set to the KEY of the Google Spreadsheet. The two 
outputs should match, but clearly they do not.

*Credit:* I will gladly give contribution credits to the person who 
provides the most elegant solution.

This question was also posted on StackOverflow: 
http://stackoverflow.com/questions/9890033/how-can-i-get-the-contents-of-a-google-spreadsheet-as-csv-data-in-the-simplest-w

Thanks in advance!

Reply via email to