Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Convert instance of 'google.visualization.DataTable' to CSV
/**
* 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;
}
@olmomp

This comment has been minimized.

Show comment Hide comment
@olmomp

olmomp Nov 20, 2013

Thanks, that's perfect! Loads of posts on Google with unsuccessful attempts of getting this done;)

In case someone is looking for a quick way to have $browser download the result:

function downloadCSV (csv_out) {
            var blob = new Blob([csv_out], {type: 'text/csv;charset=utf-8'});
            var url  = window.URL || window.webkitURL;
            var link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
            link.href = url.createObjectURL(blob);
            link.download = "filenameOfChoice.csv"; 

            var event = document.createEvent("MouseEvents");
            event.initEvent("click", true, false);
            link.dispatchEvent(event); 
}

olmomp commented Nov 20, 2013

Thanks, that's perfect! Loads of posts on Google with unsuccessful attempts of getting this done;)

In case someone is looking for a quick way to have $browser download the result:

function downloadCSV (csv_out) {
            var blob = new Blob([csv_out], {type: 'text/csv;charset=utf-8'});
            var url  = window.URL || window.webkitURL;
            var link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
            link.href = url.createObjectURL(blob);
            link.download = "filenameOfChoice.csv"; 

            var event = document.createEvent("MouseEvents");
            event.initEvent("click", true, false);
            link.dispatchEvent(event); 
}
@Nickholas

This comment has been minimized.

Show comment Hide comment
@Nickholas

Nickholas Apr 7, 2014

Great post!! Very usefull. Thanks

Great post!! Very usefull. Thanks

@heikkiv

This comment has been minimized.

Show comment Hide comment
@heikkiv

heikkiv Apr 12, 2014

Thanks for a nice utility. Another way to download the result directly from the browser is by using data URI's: http://stackoverflow.com/a/7588465

heikkiv commented Apr 12, 2014

Thanks for a nice utility. Another way to download the result directly from the browser is by using data URI's: http://stackoverflow.com/a/7588465

@jvkumar

This comment has been minimized.

Show comment Hide comment
@jvkumar

jvkumar May 29, 2014

👍

jvkumar commented May 29, 2014

👍

@MULLAINATHAN

This comment has been minimized.

Show comment Hide comment
@MULLAINATHAN

MULLAINATHAN Jul 24, 2014

How to use above function..?
Thanks in advance.

How to use above function..?
Thanks in advance.

@MULLAINATHAN

This comment has been minimized.

Show comment Hide comment
@MULLAINATHAN

MULLAINATHAN Aug 1, 2014

I can't get downloaded. Please help me.

I can't get downloaded. Please help me.

@sophiste

This comment has been minimized.

Show comment Hide comment
@sophiste

sophiste Oct 1, 2014

olmomp thanks .

Can i ask you something? .

if *.csv fiel open then xEF, xBB, xBF not in the *.csv file.

so i want the hexstring to add .

I opened the excel(windows) charset is unknow. ^^;

sophiste commented Oct 1, 2014

olmomp thanks .

Can i ask you something? .

if *.csv fiel open then xEF, xBB, xBF not in the *.csv file.

so i want the hexstring to add .

I opened the excel(windows) charset is unknow. ^^;

@hassanejaz

This comment has been minimized.

Show comment Hide comment
@hassanejaz

hassanejaz Mar 4, 2015

Can you tell me how do we integrate both sets of code on the drawToolbar where for CSV we have to give a URL?

Can you tell me how do we integrate both sets of code on the drawToolbar where for CSV we have to give a URL?

@smallsame

This comment has been minimized.

Show comment Hide comment
@smallsame

smallsame Aug 17, 2015

Very nice, appreciate that~

Very nice, appreciate that~

@magic5650

This comment has been minimized.

Show comment Hide comment
@magic5650

magic5650 Aug 4, 2016

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

magic5650 commented Aug 4, 2016

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

@rajanpaudel

This comment has been minimized.

Show comment Hide comment
@rajanpaudel

rajanpaudel Jan 11, 2018

Great Post.

Great Post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment