I'm using a Google Sheet to output a nice chart using Google Visualization API.
Recently I moved my code that creates the dataTable to Google Sheets v4 from Google Sheets v3 and I'm not able to achieve this anymore since v4 doesn't support direct "query" manipulation.
I create multiple charts based on the same data so I can't rearrange the columns in the actual sheet.
I know an array or a JSON array doesn't have a method to shift/rearrange columns but I was able to accomplish it in v3 very easy by just querying "SELECT A,B,D,C), like so:
function makeApiCall() {
var queryString = encodeURIComponent('SELECT A,B,D,C');
var tqURL = new google.visualization.Query(
'https://docs.google.com/spreadsheets/d/*yourspreadsheetid*/gviz/tq?gid=*yourworkbookid*&headers=1&tqx=responseHandler:handleTqResponse' + '&access_token=' + encodeURIComponent(gapi.auth.getToken().access_token));
tqURL.send(handleTqResponse);
}
function handleTqResponse(resp) {
var dataTable = resp.getDataTable();
}
This would output the chart with columns rearranged A,B,D,C.
Here's what I tried in v4:
function makeApiCall() {
var params = {
spreadsheetId: 'my-spreadsheet-id',
range: 'Sheet1!A:D', // Retrieve the values of "A:D".
};
var request = gapi.client.sheets.spreadsheets.values.get(params); // Modified
request.then(function(response) {
var values = response.result.values.map((e) => [e[0], e[1], e[3], e[4]]); // THIS doesn't work at all
var w = new google.visualization.ChartWrapper({dataTable: values}); // Added
var dataTable = w.getDataTable();
}, function(reason) {
console.error('error: ' + reason.result.error.message);
});
}
Can this be accomplished with something like this?
var values = response.result.values.map((e) => [e[0], e[1], e[3], e[4]]); // THIS doesn't work at all
Or maybe just specifying multiple ranges like so?
range: 'Sheet1!A:B','Sheet1!D:D','Sheet1!C:C' // Retrieve the values of "A:B",D:D,C:C /THIS doesn't work