Exporting Google Visualization DataView to an Excel file

Looking for guidance on the best way to provide users with a button to export data from a DataView behind a chart or table into Excel.

I explored using the Toolbar functionality but discovered it was not suitable for my needs since the DataView is manually populated and does not require a URL for the API. (Please let me know if I am misunderstanding this.)

I would like to employ industry-standard methods and practices. Any recommendations?

Answer №1

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!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to set up 'ng serve' command in Angular to automatically open a private browsing window?

I am looking for a way to open my project in an Incognito Mode browser without storing any cache. Is there a specific Angular CLI flag that can be included in the ng serve -o command or in the Angular CLI configuration file to enable opening a browser in ...

How can I create a dynamic form with AJAX, PHP, JavaScript, and HTML?

If you come across a situation where you have the following files: <head></head> <body> <div id="my_personal_div"> </div> </body> And in your Javascript file: $.(document).ready(){ $.ajax({ url:/any ...

Sending SMS through Twizo using CURL is a breeze

I have been attempting to send SMS utilizing the API guidelines provided at: $data = array( 'sender'=>'Me', 'body'=>'Message', 'recipients'=>'201*****0' ); $string = http_b ...

Learn how to easily upload multiple files from various upload points onto a single page using Node.js and express-fileupload

After searching on various platforms, including StackOverflow, I couldn't find a solution that fits my specific scenario. I've been struggling for hours to resolve this issue... In my handlebars page, there is an option for the user to upload fi ...

Working with JSON encoding and decoding for multidimensional arrays in PHP

After encoding an array as JSON and saving it to a database, the structure of the array is as follows: $myarray = array( 'test'=>array('key1'=>'1', 'key2'=>'2', ...

Tips for observing the return value of a function in JavaScript

When dealing with an object in a browser, each property has a getter and setter that can be overridden in order to monitor the attribute. But what about the return value of a function? In AngularJS, we can use 'ng-show' to control the visibility ...

Paging in Ext JS does not function properly when using local data sources

I am facing an issue with enabling paging in ExtJs4 grid. The paging toolbar appears to be functioning correctly, however, the paging feature does not seem to work within the grid itself. Can anyone provide guidance on what might be missing? Ext.onReady(f ...

Please verify the utilization of jQuery.ajaxSettings.xhr

var _orgAjax = jQuery.ajaxSettings.xhr; jQuery.ajaxSettings.xhr = function () { var xhr = _orgAjax(); DoSomeLogging(); return xhr; }; I came across this code snippet and I'm trying to make sense of it. Does this mean that any $.ajax(...) ...

Challenges Encountered when Encoding with Base64 in Python and Decoding in Java

Running two different packages, one coded in python and the other in Java. The python package encodes a json(dictionary) object and sends it to the java package where it is decoded into a specific POJO. The json in python looks like this: { "name&qu ...

After the rendering of the HTML, the value of the jQuery input does not change

Here we have a function that loads HTML onto the form class in a file. The quest[q] locates the appropriate HTML template to load from an array of templates. The HTML being loaded contains an input with an id of "p". I am attempting to set the value of th ...

What is the best way to enable editing of a form when the edit button is clicked?

I have created a simple profile page where users can edit their profile information. I have placed a button at the end of the page. Initially, the form should be uneditable, but when the button is clicked, the form becomes editable. I attempted to use `dis ...

Guidelines for incorporating Context API in Material UI

Currently, I am encountering a TypeScript error while attempting to pass a property using the Context API within my components. Specifically, the error message reads: "Property 'value' does not exist on type 'String'" To provide conte ...

I'm experiencing an issue with Gravity Forms validating hidden fields, is there a solution for this problem?

On my webpage, there are four fields labeled A, B, C, and D. Each field has its own set of conditional logic that determines whether it should be visible or hidden. Let's say a user lands on this page and only field B is displayed while the others are ...

Is there a way to change the background color of the body on click and have it applied throughout the entire site

Is there a way to change the background-color of the entire site and specific modal elements when a user switches between two radio buttons? Here is the HTML code for the radio buttons: <div class="btn-group btn-group-xs" data-toggle="buttons"> ...

Save essential data in local/session storage to have access to it even after the page is reloaded

Dear Team, we recently developed a single-page application that stores data in the root scope for access on other pages. While everything functions well in the regular flow, we encountered an issue with browser refresh. If a user refreshes the applicatio ...

What could be the reason for the absence of 'server' and 'client'?

Trying to retrieve data from db.json but facing some challenges. Below is the content of my db.json file: { "posts": [ { "id": "react-hooks", "title": "React Hooks", "content": "The greatest thing since sliced bread!", "author": "ali" }, ...

A guide to determining the dimensions (width, height, length) of a mesh using THREE.js

I've been scouring different sources in hopes of finding a way to obtain the width and height of a mesh, but I haven't had any luck. I have imported a collada model into my project, and all I need is to determine its dimensions in Webgl/Three.js ...

What is the best way to ensure that the text remains visible on the image?

Is there a way to maintain the static 'BUY NOW' tag on an image while allowing other elements to be dynamic? Any suggestions on how to achieve this within an img tag?https://i.sstatic.net/6eLoN.jpg ...

Potential Scope Problem in Angular JS Controller

The HTML code snippet I have is as follows: <body ng-controller = "Control as control"> <button ng-click = "control.prepareAction()">Do Action </button> <div id="one" ng-hide = "!control.showOne" > <div> <h6> ...

A guide on updating the SQL order value through a select option using PHP and jQuery

To modify the arrangement of SQL data according to the chosen select option, I am looking to adjust the ORDER value. Within my action.php file, I retrieve values from the database and aim to incorporate a sorting select option that allows for changing the ...