将Google可视化查询结果转换为javascript数组 [英] converting Google Visualization Query result into javascript array

查看:64
本文介绍了将Google可视化查询结果转换为javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要查询的电子表格的网址是

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({…});';

这是因为您将out:指定为tqx的参数(请参见

This is because you specified out: as an argument for tqx (see Google Developers guides).

如果全部原始,则可以提取并解析多个查询的JSON,并

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆