data table download mechanism

90 views
Skip to first unread message

T-Roy

unread,
Apr 20, 2012, 7:21:47 PM4/20/12
to Google Visualization API
Does anyone know of a way to provide a data download capability for a
resident data table or data view?

Ideally it would provide csv or json data that is filtered and sorted
in accordance with the currently displayed data view in a dashboard.

Thanks for any ideas!

knight

unread,
Apr 21, 2012, 12:47:54 AM4/21/12
to Google Visualization API
Hi,

I have written this JS function called getCSV() to offer CSV download
of a data table, using various pointers on the web.
Just call this based on a button click or some event. I have not
tested this with a data view yet.

String.prototype.EntityDecode = function() // For Converting HTML
encoded chars to normal form
{
var t = document.createElement( 'div' );
t.innerHTML = this;
return t.firstChild.nodeValue;
}

function getCSV(dataTable) {

var rows = dataTable.getNumberOfRows();
var cols = dataTable.getNumberOfColumns();

var str = '';

// header
for (var j = 0; j < cols; j++) {
str += '"' + dataTable.getColumnLabel(j) + '"';
if (j != cols - 1) { str += ',';};
}
str += '\n';

// data

for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
var d = new String(dataTable.getValue(i, j));
if (d.search("\&") != -1) {
str += '"' + d.EntityDecode() + '"';
} else
str += '"' + d + '"';
if (j != cols - 1) { str += ',';};
}
str += '\n';
}


window.open( "data:text/csv;charset=utf-8," + escape(str),
"csvWindow");

Peter van Raamsdonk

unread,
Apr 21, 2012, 5:08:12 AM4/21/12
to google-visua...@googlegroups.com
drawToolbar you can use for export csv and html (dataTable from servlet), ImageServlet can be used (url) to put image on iGoogle.

Regards Peter
> --
> You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
> To post to this group, send email to google-visua...@googlegroups.com.
> To unsubscribe from this group, send email to google-visualizati...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-visualization-api?hl=en.
>

T-Roy

unread,
Apr 21, 2012, 12:24:52 PM4/21/12
to Google Visualization API
This works extremely well. Thank you!

The only real change I made was to define my dataTable as a global
variable so it could be passed from the button click.

I do not intend to use a URL datasource so the toolbar does not seem
to be the right solution for me.

I want to try it with a dataview so I will need to investigate how to
get a databiew object from a table that has been sorted and
filtered...

asgallant

unread,
Apr 23, 2012, 10:08:47 AM4/23/12
to google-visua...@googlegroups.com
If you assign the event handler in your javascript, rather than in HTML, you don't need to make your DataTable a global variable.  You could add something like this to your chart drawing function:

document.getElementById('myButton').onclick = function () {
    getCSV(data);
};

You could also pass a DataView object to the same function, as DataViews support all of the necessary methods that getCSV relies on.
Reply all
Reply to author
Forward
0 new messages