问题
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:
- 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
- Select “doGet” as the function to be run.
- 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
- 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.
- Open the current web app URL you obtain after updating your version, to open your html output.
- 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