Google apps script 从同一Google电子表格文件中的两个工作表为Google Charts数据源配置DataView
我发现很难得到这个答案。我正在考虑为我的客户创建一个Google图表仪表板,并有一个年、月、日过滤器。然而,他们一年的记录超过4000行,我想在文件中添加更多的表格,并将每年的数据放在一个单独的表格中 如何在脚本中创建一个视图,从所有工作表中获取所有信息,并将它们放在一个视图中,以便将其用作Google图表仪表板的数据源 我尝试在一个工作表中添加20000行,但它会返回服务器错误Google apps script 从同一Google电子表格文件中的两个工作表为Google Charts数据源配置DataView,google-apps-script,google-visualization,google-sheets,google-spreadsheet-api,Google Apps Script,Google Visualization,Google Sheets,Google Spreadsheet Api,我发现很难得到这个答案。我正在考虑为我的客户创建一个Google图表仪表板,并有一个年、月、日过滤器。然而,他们一年的记录超过4000行,我想在文件中添加更多的表格,并将每年的数据放在一个单独的表格中 如何在脚本中创建一个视图,从所有工作表中获取所有信息,并将它们放在一个视图中,以便将其用作Google图表仪表板的数据源 我尝试在一个工作表中添加20000行,但它会返回服务器错误 任何帮助都将不胜感激 查询API一次只能从一个工作表中选择数据,因此您必须进行多个查询,然后在查询返回时将所有数据合
任何帮助都将不胜感激 查询API一次只能从一个工作表中选择数据,因此您必须进行多个查询,然后在查询返回时将所有数据合并到一个数据表中:
function drawChart () {
// use "gid" parameter to select the sheet number
// or the "sheet" parameter to select the sheet by name
var query1 = new google.visualization.Query(spreadsheetUrl + '&gid=0'); // sheet 1
var query2 = new google.visualization.Query(spreadsheetUrl + '&gid=1'); // sheet 2
var query3 = new google.visualization.Query(spreadsheetUrl + '&gid=2'); // sheet 3
// etc...
// set up each query
// eg: query1.setQuery('select a, b');
var queriesReady = [false, false, false]; // array should have a number of elements equal to the number of queries
var dataTables = [];
// readyCheck function takes the response returned by the query and the index in the "queriesReady" array to set to true
// each query should use a unique index
function readyCheck (response, index) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
data[index] = response.getDataTable();
queriesReady[index] = true;
var allDone = true;
for (var i = 0; i < queriesReady.length; i++) {
allDone = allDone & queriesReady[i];
}
if (allDone) {
// merge DataTables and draw chart
}
}
query1.send(function (response) {
readyCheck(response, 0);
});
query2.send(function (response) {
readyCheck(response, 1);
});
query3.send(function (response) {
readyCheck(response, 2);
});
}
函数绘图图(){
//使用“gid”参数选择图纸编号
//或使用“图纸”参数按名称选择图纸
var query1=new google.visualization.Query(spreadsheetUrl+'&gid=0');//表1
var query2=new google.visualization.Query(spreadsheetUrl+'&gid=1');//表2
var query3=new google.visualization.Query(spreadsheetUrl+'&gid=2');//表3
//等等。。。
//设置每个查询
//例如:query1.setQuery('selecta,b');
var queryready=[false,false,false];//数组的元素数应等于查询数
var dataTables=[];
//readyCheck函数将查询返回的响应和“queriesReady”数组中的索引设置为true
//每个查询都应该使用唯一的索引
函数readyCheck(响应、索引){
if(response.isError()){
警报('查询中的错误:'+response.getMessage()+'+response.getDetailedMessage());
返回;
}
data[index]=response.getDataTable();
queriesReady[index]=true;
var allDone=true;
对于(var i=0;i
这是一个很好的解决方案,但还有另一个输入错误:
data[index] = response.getDataTable();
应该是:
dataTables[index] = response.getDataTable();
只需将行query就绪[index]=true;要查询就绪[索引]=true;让这一切顺利进行。数组名中有一个输入错误。哦,答案中已修复。谢谢您的关注。嗨,您知道如何从电子表格中获取所有GID或工作表选项卡名称吗?对不起,@SERG,我不知道。如果搜索没有给你答案,你可以在这里问你自己的问题。