Google sheets: Class google.script.run not working

喜欢而已 提交于 2020-12-10 11:55:45

问题


My problem is simple. All the possible solutions I searched for online did not address my question.

Google's developer website for Class google.script.run (https://developers.google.com/apps-script/guides/html/reference/run#withSuccessHandler) showcased the method myFunction(...) (any server-side function).

I have copied their exact code and html code and deduced that the function doSomething() does not execute. Nothing gets logged.

I intend to use this to execute an HTML file so that I could play a sound file. I could do this so far with a sidebar popping up from the side, as discussed in this thread: Google Script: Play Sound when a specific cell change the Value.

However, this code provided by Google does not work. Why?

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function doSomething() {
  Logger.log('I was called!');
}

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      google.script.run.doSomething();
    </script>
  </head>
  <body>
  </body>
</html>

回答1:


By using google.script.run you are calling a server-side Apps Script function. https://developers.google.com/apps-script/guides/html/reference/run Please double-check that you follow the following steps to do it correctly:

  1. Please make sure that you put the html part of the code in a separate HTML file (which you create through File->New->HTML file) with the name corresponding to the one you are calling in HtmlService.createHtmlOutputFromFile() - in your case Index.html
  2. Select “doGet” as the function to be run.
  3. Deploy the script as a web app - this is the requirement for using Apps Script HTML service. Please find the instructions here: https://developers.google.com/apps-script/guides/web
  4. Make sure that every time after you implement changes in your code, you deploy the script as a NEW project version. This is necessary to update the changes.
  5. Open the current web app URL you obtain after updating your version, to open your html output.
  6. In your case only an empty HTML file will be opened, to test functionality - insert some text in your HTML body, to test the correct functionality. The latter can be confirmed by viewing the Logs after running the code.


来源:https://stackoverflow.com/questions/56844507/google-sheets-class-google-script-run-not-working

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