Apps Script - Can't use google.script.run from within a js function in an html template?

前端 未结 2 1490
醉话见心
醉话见心 2021-02-11 04:00

UPDATED: This is totally possible. I just was closing the host window before the async method completed. My interpretation was that the running context would persist, even i

相关标签:
2条回答
  • 2021-02-11 04:20

    Instead of using HtmlService.createTemplateFromFile you could use:

     HtmlService.createHtmlOutputFromFile('simpleDialog');
    

    Here you can find more information about this function https://developers.google.com/apps-script/guides/html/

    0 讨论(0)
  • 2021-02-11 04:32

    The culprit here is this line in onSave() function:

    google.script.host.close();
    

    Remove/comment this line and the script runs fine. The error you see is because you close the sidebar with that line, severing connection to the server-side script.

    If you do need to close the sidebar after onSave() has finished executing, move that line to the success handler callback:

    function onSave(){          
      google.script.run
        .withSuccessHandler(onSuccess)
        .withFailureHandler(onFailure)
        .doWorkInAppsScriptFile();        
    }
    
    function onSuccess(data) 
    {
      // do something with returned data
      // close the sidebar
      google.script.host.close();
    }
    
    function onFailure(error)
    {
      alert("onFailure: " + error);
    }
    
    0 讨论(0)
提交回复
热议问题