【问题标题】:google.script.run.withSuccessHandler() returns Undefinedgoogle.script.run.withSuccessHandler() 返回未定义
【发布时间】:2019-12-03 08:10:32
【问题描述】:

我使用下面提供的代码在单独的 GS 文件中创建了一个数组。我尝试在我的 HTML 文件中调用它。我的目标是将数组的内容与参数email 进行比较。但是google.script.run.withSuccessHandler()返回的值是undefined

//in GS
function mailGetter()
{
  //open sheet
  var sheet = SpreadsheetApp.openByUrl("https://sheet.url").getSheetByName("Email Sheet").activate();
  //get size of given row range
  var row_data_email = sheet.getRange("C2:C").getValues();
  var emailArray = row_data_email.join().split(',').filter(Boolean);
  Logger.log(emailArray);
  
  return emailArray;
}
//in HTML
function checkEmail(email) 
    {
      var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
      var arraySize = google.script.run.withSuccessHandler(misc).sizeGetter(); 
      console.log(arraySize);
      var emailArray = new Array(arraySize);
      emailArray = google.script.run.withSuccessHandler(misc).mailGetter();
      console.log(emailArray);
      
      if (reg1.test(email) == false) 
      {
        emails.style.border = "1px solid red";
        document.getElementById('submitBtn').disabled = true;
      } 
      else if (reg1.test(email) == true) 
      {
        emails.style.border = "1px solid green";
        document.getElementById('submitBtn').disabled = false;
      }
      
      for (var row = 0; row < arraySize; row++)
      {
        if (emailArray[row][0] == email)
        {
          emails.style.border = "1px solid green";
          document.getElementById('submitBtn').disabled = false;
          break;
        }
        else if (emailArray[row][0] != email)
        {
          emails.style.border = "1px solid red";
          document.getElementById('submitBtn').disabled = true;
        }
      }
    }

    function misc()
    {
      console.log("Pass");
    }

【问题讨论】:

  • 函数misc在哪里?
  • 我忘了包含它,因为它只是在控制台日志中写入“通过”。很抱歉
  • 函数misc 将收到arraySizeemailArray。客户端调用服务器函数sizeGetter()> 服务器占用自己的时间> 客户端不等待服务器返回并在此期间执行它所做的任何事情> 一段时间后,服务器返回arraySize 给客户端> 客户端接收它在传递给successHandler的函数中(即misc函数)。 TLDr:客户端要求服务器做某事,并在服务器完成客户端要求它做的工作后用这个数字(成功处理函数)回调它。
  • 我刚刚使用 console.log() 进行了检查,这些值实际上存储在其中。我有办法获取 function checkEmail() 而不是 function misc() 中的值吗?

标签: google-apps-script google-sheets web-applications


【解决方案1】:

问题:

  • 使用异步函数的 (google.script.run) 返回值,该值将始终为 undefined

解决办法:

  • 使用另一个答案中提到的successHandler,或者我们可以将promises与async/await一起使用。

片段:

/*Create a promise around old callback api*/
const p = func => 
  new Promise(resolve=>
    google.script.run.withSuccessHandler(resolve)[func]()
  );

async function checkEmail(email) //modified
    {
      var arraySize = await p('sizeGetter');//Wait to resolve
      console.log(arraySize);
      //var emailArray = new Array(arraySize);
      var emailArray = await p('mailGetter');//Wait to resolve
      console.log(emailArray);
      //....
    }

注意:

  • 最好减少对服务器的调用次数。如果您可以将两个Getters 合并到一个服务器函数中,那就更好了。
  • 上面是一个sn-p,展示了如何使用async/await。但是,如果您如上所示等待来自服务器的每个响应,您的前端/UI 将会很慢。仅在绝对必要时等待。对服务器的调用应该是非阻塞/异步的。

参考资料:

【讨论】:

    【解决方案2】:

    问题出在以下几行:

    emailArray = google.script.run.withSuccessHandler(misc).mailGetter();
    console.log(emailArray);
    

    您正在尝试执行mailGetter() 并期望它返回您存储在emailArraythis method is asynchronous and does not return directly 中的值

    相反,您将在回调中获得您定义为SuccessHandler的值

    建议的解决方案:

    1. 从模板调用 Apps 脚本函数:https://developers.google.com/apps-script/guides/html/templates#apps_script_code_in_scriptlets
    2. 直接调用 Apps Script API:https://developers.google.com/apps-script/guides/html/templates#calling_apps_script_apis_directly
    3. 将变量推送到模板:https://developers.google.com/apps-script/guides/html/templates#pushing_variables_to_templates

    参考:https://developers.google.com/apps-script/guides/html/reference/run#myFunction(...)

    【讨论】:

    • 有没有办法绕过这个问题?还是我必须重新编写代码才能解决这个问题?
    • 您将需要编写两个函数:一个调用服务器,另一个作为回调来执行您尝试执行的其余操作。您可能可以重用当前代码的功能部分。
    • 调用服务器是指可以访问电子表格并从那里获取内容的代码吗?
    猜你喜欢
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2016-11-18
    • 2019-12-24
    相关资源
    最近更新 更多