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

前端 未结 2 1492
醉话见心
醉话见心 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: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);
    }
    

提交回复
热议问题