函数完全执行前,带有withSuccessHandler的Google Apps脚本正在运行 [英] Google Apps Script withSuccessHandler is running before function fully executes
问题描述
我在Apps脚本项目中有两个文件.一个是充当服务器"的.gs文件,另一个是包含根据Google Apps脚本指南的JavaScript的.html文件.
I have two files in an Apps Script project. One is a .gs file acting as a "server" and one is a .html file containing JavaScript as per Google's Apps Script guidelines.
在此项目的最初40个小时开发中,一切工作都如雨后春笋般进行.我在任何功能之外都有以下代码行,位于.html文件中的两个标签之间:
Everything has been going swimmingly for the first 40 hours of development on this project. I have the following line of code outside of any function, in between two tags in the .html file:
google.script.run.withSuccessHandler(setSheetData).getSheetData();
文档: https://developers. google.com/apps-script/guides/html/reference/run#withSuccessHandler(函数)
根据文档,getSheetData()应该首先在.gs文件中执行,然后返回一个值,然后将该值传递到存在于.html文件中的setSheetData中.
According to the documentation, getSheetData() should first execute in the .gs file, and return a value that is then passed into setSheetData which exists in the .html file.
服务器文件:
function getSheetData() {
var ss = SpreadsheetApp.getActive();
var activeSheet = ss.getActiveSheet();
var sheetName = activeSheet.getName();
var sheetVals = activeSheet.getDataRange().getValues();
return [sheetName, sheetVals];
}
HTML文件:
function setSheetData(data) {
alert(data);
sheetName = data[0];
sheetData = data[1];
headers = sheetData[0];
document.getElementById('sheetLook').innerHTML = 'Looking at sheet: ' + sheetName;
}
我怎么知道这与执行速度有关: 当前,alert()调用仅打印出null.它从中绘制的图纸包含4行数据.但是,所有其他情况保持不变,如果我只是查看具有0-1行数据的工作表,它将正确地警告整个数据值.
How I know it is a matter of execution speed: Currently the alert() call just prints out null. The sheet it is drawing from contains 4 rows of data. However, all other things remaining the same, if I simply am looking at a sheet with 0-1 rows of data, it correctly alerts the entire data vals.
在getSheetData()内,如果我添加Logger.log(sheetVals),则无论大小如何,它都能正确记录整个工作表的数据.问题在于成功处理程序正在之前执行,有时间进行评估.
Inside of getSheetData() if I add Logger.log(sheetVals) it correctly logs the entire sheet's data regardless of size. The issue is that the successhandler is executing before it has time to evaluate.
推荐答案
可能的原因:
- 数据第3-4行中的非法返回值
- 没有
active
工作表. - Illegal return values in rows 3-4 of data
- No
active
sheet is present. - 避免返回非法值,例如
Date
对象.或JSON.stringify()
他们,然后再返回给客户. -
getSheetByName
或数字,而不是通过active
ness来获取. - Avoid returning illegal values like
Date
objects. OrJSON.stringify()
them before returning to client. getSheetByName
or number instead of getting it byactive
ness.
Possible cause(s):
这篇关于函数完全执行前,带有withSuccessHandler的Google Apps脚本正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!