Problem with self-invoking functions calling other functions

﹥>﹥吖頭↗ 提交于 2019-12-24 12:35:04

问题


In my index.html file (before closing body tag), I want a function to self-invoke itself when the page is loaded. However, I am having issues when the function (here, setUp) is defined in an external file.

If I copy-paste the setUp function in Index.html then everything works as expected.

I am new to JS: am I incorrectly linking script file? Thanks!

Index.html

    <script src="Code.gs">
        window.onload=setUp;
    </script>

Code.gs

        function setUp() {
            dateHelper_();
        }


回答1:


  • You want to run Google Apps Script when HTML is loaded.

If my understanding is correct, how about this modification? The flow of this modified script is as follows.

  1. When the HTML is opened, google.script.run is run and setUp() of Google Apps Script is run.
  2. When setUp() is finished, "ok" from setUp() is returned and the returned value is shown using console.log() at withSuccessHandler().
    • In this modified script, you can see Done: ok at the console of browser.

Modified script:

Please modify HTML and Google Apps Script on your script editor as follows.

HTML & Javascript: Index.html
<script>
  window.onload = google.script.run.withSuccessHandler((e) => {console.log("Done: " + e)}).setUp();
</script>
Google Apps Script: Code.gs
function setUp() {
//    dateHelper_();
  return "ok"; // In this modification, as a sample, "ok" is returned.
}

Note:

  • Although I'm not sure about your whole situation, HTML can be opened by a dialog, sidebar and Web Apps.

Reference:

  • Class google.script.run

If I misunderstood your question and this was not the result you want, I apologize.



来源:https://stackoverflow.com/questions/56303180/problem-with-self-invoking-functions-calling-other-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!