admin管理员组文章数量:1400097
I'm starting to use Google App Scripts, and with the little I know of JavaScript I tried the following below. I get an error, but the script does seem to be working.
Why does it give an error? It seems that it is specifically looking to run a function when I press the little play button.. Would it be bad practice to structure a google app scripts doc as I have done below?
(function (id) {
var sheet = SpreadsheetApp.openById(id).getSheets()[0];
var data = sheet.getDataRange().getValues();
Logger.log(data);
sheet.appendRow(['this','is','another', 'bigger','test','of','stuff']);
})('1BXDicksYS19jre0tZKHEjqqyvdzbhbtFfSm053q2sZ0')
=== EDIT ===
Google Apps Scripts shouldn't be run from an IIFE, since it wants to explicityly trigger a function. So yes. I think, in my still limited experience a year later, that this would be a poor way to write a Google Apps Script.
I'm starting to use Google App Scripts, and with the little I know of JavaScript I tried the following below. I get an error, but the script does seem to be working.
Why does it give an error? It seems that it is specifically looking to run a function when I press the little play button.. Would it be bad practice to structure a google app scripts doc as I have done below?
(function (id) {
var sheet = SpreadsheetApp.openById(id).getSheets()[0];
var data = sheet.getDataRange().getValues();
Logger.log(data);
sheet.appendRow(['this','is','another', 'bigger','test','of','stuff']);
})('1BXDicksYS19jre0tZKHEjqqyvdzbhbtFfSm053q2sZ0')
=== EDIT ===
Google Apps Scripts shouldn't be run from an IIFE, since it wants to explicityly trigger a function. So yes. I think, in my still limited experience a year later, that this would be a poor way to write a Google Apps Script.
Share Improve this question edited Sep 1, 2018 at 20:13 Wicket 38.7k9 gold badges80 silver badges194 bronze badges asked Jun 1, 2015 at 14:48 Zach SmithZach Smith 8,99115 gold badges74 silver badges149 bronze badges 7- 1 Under the VIEW menu, choose "Execution Transcript". Look at the bottom of the list. Is there an error message? If, so, what is it, and what line? – Alan Wells Commented Jun 1, 2015 at 14:55
- yes there is an error: 15-06-01 16:55:53:632 SAST] Execution failed: Script function not found: createReport [0.0 seconds total runtime] – Zach Smith Commented Jun 1, 2015 at 14:57
- But there are no named functions in the script. I don't know why it keeps trying to run one (at first I had a function called createReport. However, it is working exactly as it should so it's almost a bug? – Zach Smith Commented Jun 1, 2015 at 14:58
- 1 You're using a JavaScript syntax that many people refer to as a "Self Invoking" function. The function runs without being called, under certain conditions. It's also an anonymous function. (It has no name) So there is no name listed in the function names field. If you click the Run button, the function probably runs because it is in the global scope. I'm not sure how you want to trigger this function? – Alan Wells Commented Jun 1, 2015 at 15:11
-
2
yes, like Sandy says. use the "regular" function syntax as in
function myFunction() { var id='xxxxx'; etc; }
then it will appear in the functions list and can be used in triggers – Zig Mandel Commented Jun 1, 2015 at 17:31
2 Answers
Reset to default 3Google App Scripts files ending with a .gs do not run all functions in the file like in JavaScript when you load the file all the IIFE's will run.
In order to run a function in a .gs file which is what you are using above you need to either add a trigger onOpen or onEdit of the spreadsheet. on open/on edit: https://developers.google./apps-script/guides/triggers/
A time trigger which you can set based on a certain h/d/m condition. installable triggers: https://developers.google./apps-script/guides/triggers/installable
If you want to connect this to a google web app you need to use google.scripts.run.functionName() running .gs function on the client side: https://developers.google./apps-script/guides/html/reference/run
Do not use immediately invoked functions in .gs files. It's also good to have very specific names for your .gs functions because of how the Google App Script file tree works.
All the functions are accessible in any file within the google app script file tree meaning you can call any function in any file within the google app script.
Having a function without a name in this structure would make it impossible to call the nameless function. Hope that helps.
Google Apps Scripts shouldn't be run from an IIFE, since it wants to explicityly trigger a function. So yes. I think, in my still limited experience a year later, that this would be a poor way to write a Google Apps Script.
本文标签: javascriptquotScript function not foundquotGoogle App ScriptsStack Overflow
版权声明:本文标题:javascript - "Script function not found" - Google App Scripts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744186253a2594303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论