Reputation: 314
So here is my google apps script without the block comment on top:
function GET_POSITION(game,position) {
defaultsheets=['Stats','Games','Calculations'];
var sheets=SpreadsheetApp.getActiveSpreadsheet().getSheets();
sheets.forEach(function(sheet) {
name=defaultsheets.indexOf(sheet.getName())==-1;
return false;
if(name)
{
name='test';
}
});
return name;
}
Yes I know, this is a weird script and it does not do anything.That is because I kept changing it trying to find out why it would not work.
But that is not the problem. I would expect this script to return false. It does not, it returns true. But when i remove the "return false" it still wont run the if statement (it just keeps returning True).
Meaning:
return false gets ignored?
name is equal to true, but when doing if(name), the if statement is not executed.
Putting name="test" at the beginning of the sheet will make the function return "test".
I have no idea why this script is functioning this way.
Upvotes: 0
Views: 779
Reputation: 2189
Well, haha, this is a weird script. When I first looked at it, I thought, how could this even run without a compiling error? The variable name
is being called outside the scope of a closure, the rest of the function shouldn't know anything about it, it would give an undeclared variable error.
Then I saw that you're not using var
to initialize your variable name
. That makes name
behave like a global constant. That will give you unexpected behavior certainly. There is just never a good reason to do that.
Declaring variables without var keyword
Another thing is that only that anonymous function is returning false. Then it jumps out and returns name
, which has been set to true.
What are Closures and Callbacks?
And, I have to ask, why are you using ALL_CAPS to name a function?
Upvotes: 1