2

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.

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? 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] 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? 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? 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 Commented Jun 1, 2015 at 17:31

2 Answers 2

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.com/apps-script/guides/triggers/

A time trigger which you can set based on a certain h/d/m condition. installable triggers: https://developers.google.com/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.com/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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.