Created
December 14, 2011 16:43
-
-
Save pilate/1477368 to your computer and use it in GitHub Desktop.
Convert instance of 'google.visualization.DataTable' to CSV
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
/** | |
* Convert an instance of google.visualization.DataTable to CSV | |
* @param {google.visualization.DataTable} dataTable_arg DataTable to convert | |
* @return {String} Converted CSV String | |
*/ | |
function dataTableToCSV(dataTable_arg) { | |
var dt_cols = dataTable_arg.getNumberOfColumns(); | |
var dt_rows = dataTable_arg.getNumberOfRows(); | |
var csv_cols = []; | |
var csv_out; | |
// Iterate columns | |
for (var i=0; i<dt_cols; i++) { | |
// Replace any commas in column labels | |
csv_cols.push(dataTable_arg.getColumnLabel(i).replace(/,/g,"")); | |
} | |
// Create column row of CSV | |
csv_out = csv_cols.join(",")+"\r\n"; | |
// Iterate rows | |
for (i=0; i<dt_rows; i++) { | |
var raw_col = []; | |
for (var j=0; j<dt_cols; j++) { | |
// Replace any commas in row values | |
raw_col.push(dataTable_arg.getFormattedValue(i, j, 'label').replace(/,/g,"")); | |
} | |
// Add row to CSV text | |
csv_out += raw_col.join(",")+"\r\n"; | |
} | |
return csv_out; | |
} |
it raise a bug when table has multi pages.
so i change "raw_col.push(dataTable_arg.getFormattedValue(i, j, 'label').replace(/,/g,""));"
like this
"raw_col.push(dataTable_arg.getFormattedValue(i, j).replace(/,/g,""));"
and it work
i remove "label",i think it no use.
https://developers.google.com/chart/interactive/docs/reference
Great Post.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice, appreciate that~