将Google可视化查询结果转换为javascript数组 [英] converting Google Visualization Query result into javascript array
问题描述
我要查询的电子表格的网址是
The url to the spreadsheet I am querying is
docs.google.com/spreadsheets/d/1EIBhBQY1zbdBEKXsJIY1uyvdQw0b1cIBSrBE_tZvA6Y/edit?usp=sharing
正在使用的查询网址是
The query url being used is
https://spreadsheets.google.com/tq?tqx=out:&key=1EIBhBQY1zbdBEKXsJIY1uyvdQw0b1cIBSrBE_tZvA6Y&gid=0&headers=1&tq=select%20B%2CC%2CD%20where%20(A%20matches%20%22DIS%22)
是否可以将结果转换或存储在JavaScript数组中?
Is there a way to convert or store this result in a JavaScript array?
var dis = ["The Walt Disney Company","Entertainment",.1]
我需要能够一次操纵数据并将新数据添加到可视化文件中.
I need to be able to manipulate the data at one point and add the new data to the visualization.
来自多个查询之一的数据->转换为数组->处理数据例如: 相乘 一个 输入-> data.addRows(操作输入);
Data from one of multiple queries --> Convert to array --> Manipulate data ex: multiplying an input --> data.addRows(manipulated input);
推荐答案
您的查询返回的字符串确实包含包装在函数调用中的JSON:
Your query does return a string containing JSON wrapped in a function call:
var responseText = 'google.visualization.Query.setResponse({…});';
This is because you specified out:
as an argument for tqx
(see Google Developers guides).
If you want it all raw, you can extract and parse the JSON of multiple queries and push
the data to an array, so you end up with an array of arrays of row data. For your single query, you could start from something like this:
responseJSON = JSON.parse(
responseText.replace(/(^google\.visualization\.Query\.setResponse\(|\);$)/g,'')
);
var rowsArray = [];
responseJSON.table.rows.forEach(function(row){
var rowArray = [];
row.c.forEach(function(prop){ rowArray.push(prop.v); });
rowsArray.push(rowArray);
});
console.log(rowsArray); // === [["The Walt Disney Company", "Entertainment", 0.1]]
这篇关于将Google可视化查询结果转换为javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!