Google Apps脚本中的成功处理程序从服务器函数接收null
Success handler in Google Apps script receives null from server function
我正试图在Google Sheets侧边栏中显示来自服务器函数的数据。我正在使用一个带有成功处理程序的异步服务器调用,但客户端不知何故收到了一个null值。尽管在客户端-服务器通信上进行了广泛的搜索,但我一直无法找出原因。
目前,服务器函数末尾的日志显示完全定义的对象"flags",但successHandler"showErrors"开头的控制台日志显示"flags"未定义。
我查看了HTML服务上的Google文档,据我所知,flags是一个有效的返回值,因为它是一个包含整数、字符串和字符串数组的对象。我尝试将对象中的"flags"更改为一个简单的字符串,但它在"showErrors"中仍然未定义有人知道为什么"flags"的内容在服务器和客户端之间丢失吗提前谢谢!
HTML:
<form onsubmit="google.script.host.close()">
<div id="intro" style="font-style:italic">
<p><b>Title</b><br><br>
Introduction text</p>
<HR>
<input type="button" style="button" id="start" value="Start" onclick="hideDiv('intro');google.script.run.withSuccessHandler(showErrors).checkList2(0,0)"> <!-- Intro starts loop -->
</div>
<div id="showErrors"></div>
</form>
<script>
function showErrors(flags){
console.log('client side flags:');
console.log(flags);
var div = document.getElementById('showErrors');
div.innerHTML = '<p style="font-style:italic">';
div.innerHTML += 'Sheet '+flags.pageNum+' of '+flags.numPages+'.';
//... more div.innerHTML += ...
div.innerHTML += '<input type="button" style="button" value="Next" onclick="google.script.run.withSuccessHandler(showErrors).checkList2('+Number(flags.pageNum)+1+','+flags.totalErrors+')"';
div.innerHTML += '<input type="submit" style="button" value="Cancel">';
}
function hideDiv(div){
document.getElementById(div).innerHTML=''; // clear div
}
</script>
服务器功能:
function checkList2(nComplete,nErrors){
var nSheets=21;
nComplete = Number(nComplete);
nErrors = Number(nErrors);
var results = errorList(nComplete); // Get results.name (string) and results.errors (array)
var errors = results.errors;
if (errors=='') {
checkList2(nComplete+1,nErrors); // Move on to next sheet
} else {
nErrors = nErrors + errors.length;
var flags = {};
flags.numErrors = errors.length;
flags.totalErrors = nErrors;
flags.pageNum = nComplete;
flags.numPages = nSheets;
flags.sheetName = results.name;
flags.errors = errors;
Logger.log('server side flags:')
Logger.log(flags)
return flags;
}
}
如果输入以下if
块,则不会向客户端返回任何内容:
if (errors=='') {
checkList2(nComplete+1,nErrors); // Move on to next sheet
// no return
} else {
nErrors = nErrors + errors.length;
var flags = {};
flags.numErrors = errors.length;
flags.totalErrors = nErrors;
flags.pageNum = nComplete;
flags.numPages = nSheets;
flags.sheetName = results.name;
flags.errors = errors;
Logger.log('server side flags:')
Logger.log(flags)
return flags;
}
你所需要做的就是返回递归调用,我认为你会得到你期望的行为。
if (errors=='') {
return checkList2(nComplete+1,nErrors); // Move on to next sheet
} else {
nErrors = nErrors + errors.length;
var flags = {};
flags.numErrors = errors.length;
flags.totalErrors = nErrors;
flags.pageNum = nComplete;
flags.numPages = nSheets;
flags.sheetName = results.name;
flags.errors = errors;
Logger.log('server side flags:')
Logger.log(flags)
return flags;
}
相关文章:
- 如何从服务器向客户端调用函数?(远程功能调用)
- 从javascript调用服务器端的下拉列表onchange函数
- 正在使用在服务器端或客户端执行的 Jquery 函数
- 在服务器上创建一个复选框,使用 javascript 处理函数
- 无法从服务器中的 php 调用 Ajax 函数
- 如何在 Javascript 函数中拦截 Web 服务器响应
- Node.js:如何在服务器初始化时运行一个特殊的函数
- 从服务器端调用javascript函数
- php函数向XMPP服务器发送消息,不提供日志;不起作用
- Angular:将对象存储在服务中以在视图中显示和将crud函数存储到服务器的最佳方式是什么
- 我上传了客户's服务器一个js函数不起作用
- Ajax没有给我服务器端函数调用
- 从服务器端添加jQuery函数
- "对象不是函数“;当将Node.js HTTP服务器对象传递给Socket.IO时
- 如何使用javascript代码调用服务器端函数;标记函数“>
- Meteor:定义可用于方法的服务器函数
- 你能从同一个UserControls Javascript调用一个UserControl服务器函数吗
- 我有输入按钮动态创建的javascript应该调用服务器c#函数,但他们没有
- Google Apps脚本中的成功处理程序从服务器函数接收null
- 单击调用服务器函数并返回模板化的 html