James Cropcho
James Cropcho

Reputation: 3322

Why do functions run even when they are not called?

I hope this question finds you well.

Here is a Google Apps Script in its entirety:

function myFunction() {
  console.log("hello");
}

When I "Run" this script from https://script.google.com/, I am returned the following:

Execution log

5:23:46 PM Notice Execution started
5:23:46 PM Info hello
5:23:46 PM Notice Execution completed

As I have not called myFunction I would not expect to see "hello" in the Execution Log. Yet it is there. Why? And is there a way to make Google Apps Script behave like all other imperative programming environments in the world?

Upvotes: 2

Views: 210

Answers (1)

andrewJames
andrewJames

Reputation: 22032

When you run a script in the AppsScript runner, I assume you are hitting the Run menu item (or perhaps the Debug item). When you do this, you also need to look at which function name is selected in the function name drop-down list, to the right of that Run menu item (after the Debug menu item).

Whichever function is selected (and saved) will be executed. If you only have one function in the entire script, it will be selected by default.

enter image description here

There is no way to select "no function" in the script runner.

(And if you have no functions in your script, then the drop-down is greyed out and simply displays No functions.)

Upvotes: 3

Related Questions