我有一个Google电子表格,比如一个键'ABCKEY',并希望在使用Google Apps脚本信息中心中的数据之前对数据执行QUERY(SQL)功能。
var ss = SpreadsheetApp.openById('ABCKEY');
var mydata = ss.getDataRange();
This article解释了如何使用电子表格中数据的QUERY功能来生成分组数据。
以下查询生成了我想用作Google Apps脚本信息中心数据源的正确分组数据:
https://spreadsheets.google.com/a/mybusinessappdomain.com/tq?key=ABCKEY&tq=select%20A%2CC%2Csum(F)%2Csum(G)%20group%20by%20A%2C%20C
因此,我基本上希望使用上述SQL查询的结果填充上面的mydata
变量,该查询会生成JSON输出字符串。
如何实现这一目标?
答案 0 :(得分:2)
我建议的方法是:
UrlFetchApp.fetch()
将查询结果uri放入脚本中的变量中。查询URI返回javascript以设置Google可视化服务的结果。mydata
。这需要了解表如何在可视化查询中表示为JSON对象。JSON查询结果的结构如下:
{
"version": "0.6",
"status": "ok",
"sig": "862651648",
"table": {
"cols": [
{
"id": "A",
"label": "",
"type": "string",
"pattern": ""
},
{
"id": "D",
"label": "Population Density",
"type": "number",
"pattern": "#0.###############"
}
],
"rows": [
{
"c": [
{
"v": "Indonesia"
},
{
"v": 117,
"f": "117"
}
]
},
{
"c": [
{
"v": "China"
},
{
"v": 137,
"f": "137"
}
]
},
{
"c": [
{
"v": "Nigeria"
},
{
"v": 142,
"f": "142"
}
]
},
{
"c": [
{
"v": "Pakistan"
},
{
"v": 198,
"f": "198"
}
]
},
{
"c": [
{
"v": "India"
},
{
"v": 336,
"f": "336"
}
]
},
{
"c": [
{
"v": "Japan"
},
{
"v": 339,
"f": "339"
}
]
},
{
"c": [
{
"v": "Bangladesh"
},
{
"v": 1045,
"f": "1045"
}
]
}
]
}
}
您会注意到table
对象由一组cols
个对象组成,这些对象描述了表中的列。对于您的数组,您感兴趣的部分是列的label
。
之后,table
对象包含一个rows
个对象数组,每个对象都有一个c
个对象数组,其中包含行中每列的数据。对于您的数组,它是您感兴趣的v
或值。(f
包含相同数据的格式化版本)
因此,我们的解析器将首先遍历列标题,然后遍历表的每一行,push
将感兴趣的值mydata
转换为二维数组 // Specify the spreadsheet key and the query to be retrieved
var ssKey = 'pCQbetd-CptGXxxQIG7VFIQ';
var query = encodeURIComponent('SELECT A,D WHERE D > 100 ORDER BY D');
// Build url to peform query
var url = 'http://spreadsheets.google.com/tq?key=%KEY%&tq=%QUERY%'
.replace('%KEY%',ssKey)
.replace('%QUERY%',query);
// Use UrlFetchApp to get the results of the query as a string.
var response = UrlFetchApp.fetch(url);
var content = response.getContentText();
//Logger.log(content);
// Extract the JSON object from the response. Note that the response contains
// multiple lines of Javascript, and that it's the second line that has our
// data table in it.
var regex = /.*google.visualization.Query.setResponse\((.*)\)/g
var jsonContent = regex.exec(content)[1];
Logger.log(jsonContent);
var objectContent = Utilities.jsonParse(jsonContent);
var numCols = objectContent.table.cols.length;
var numRows = objectContent.table.rows.length;
// Decode objectContent into a two-dimensional array.
var mydata = []
// Start with the header row.
mydata[0] = [];
for (var col = 0; col < numCols; col++ ) {
mydata[0].push(objectContent.table.cols[col].label);
}
// Then data rows
for (var row = 0; row < numRows; row++) {
mydata[row+1] = [];
for (var col = 0; col < numCols; col++ ) {
mydata[row+1].push(objectContent.table.rows[row].c[col].v);
}
}
// Done - mydata is now a two-dimensional array with the results of the query
debugger; // If debugging, pause to examine results
。
对于此示例,我正在访问Interactive Code Sample中提供的Query Language Reference中使用的公开电子表格,并使用他们的示例查询。我编写了这个示例,因此可以轻松修改它以使用您自己的查询访问您自己的电子表格。这是代码:
table
根据您计划在信息中心中使用数据的内容,您可能只想在调用jsonParse()
后使用{{1}}对象。
答案 1 :(得分:0)
我不认为json解析器var objectContent = Utilities.jsonParse(jsonContent)
可以使用那些将日期作为单元格值之一的可视化查询响应,例如下面的示例
{
"v": newDate(2013,
11,
31),
"f": "31-Dec-2013",
"p": {
"style": "color: rgb(0, 0, 0);font-family:Dialog;"
}
}