javascript - "Script function not found" - Google App Scripts - Stack Overflow

I'm starting to use Google App Scripts, and with the little I know of JavaScript I tried the follo

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
 |  Show 2 more ments

2 Answers 2

Reset to default 3

Google 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744186254a4562218.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信