For those interested, here is the solution I came up with:
HTML:
<div id="my_table"></div>
<a id="exportCSV" href="">Export as CSV</a>
The table object in my code is a .ChartWrapper
, allowing me to utilize the .getDataTable()
method later on.
var table = new google.visualization.ChartWrapper({
chartType: 'Table',
containerId: 'my_table',
options: {allowHtml: true}
});
Javascript - Google visualization event listener
document.getElementById("exportCSV").addEventListener("click", function () {
var csvData = table.getDataTable(); //google visualization DataTable for download
export_CSV("exportCSV", csvData);
});
Javascript - function reference link
function export_CSV(elementID, data) {
var csvColumns;
var csvContent;
var downloadLink;
// creating column headings
csvColumns = '';
for (var i = 0; i < data.getNumberOfColumns(); i++) {
csvColumns += data.getColumnLabel(i);
if (i < data.getNumberOfColumns() - 1) {
csvColumns += ',';
}
}
csvColumns += '\n';
// getting data rows
csvContent = csvColumns + google.visualization.dataTableToCsv(data);
//Creating Download Link
downloadLink = document.getElementById(elementID);
downloadLink.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvContent);
downloadLink.download = 'data.csv';
downloadLink.target = '_blank';
}
This solution worked well for my project. Thank you!