Google Apps脚本中的成功处理程序从服务器函数接收null

Success handler in Google Apps script receives null from server function

本文关键字:服务器 函数 null 程序 处理 脚本 Apps 成功 Google      更新时间:2023-09-26

我正试图在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;
}