3

This is my test function:

var testFolderId = 'di98kjsdf9...';
function testGetFolder(testFolderId){
  folder = DriveApp.getFolderById(testFolderId);
  Logger.log("folders: " + folder);
}

It fails when I do this. The error says: INVALID ARGUMENT

However, if I hardcode the id into the 'DriveApp.getFolderById' function, it works.

Any explanation? This makes no sense to me.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
AmericanMade
  • 453
  • 1
  • 9
  • 22
  • How are you calling this method? – Taplar Aug 26 '20 at 21:16
  • I'm just running it in my google apps script editor. I select that function and then run. – AmericanMade Aug 26 '20 at 21:17
  • 2
    So, something other than code you have written is causing that function to execute? You don't have `testGetFolder(testFolderId);` any where? – Taplar Aug 26 '20 at 21:18
  • 2
    If you only select that function and run it, probably your variable 'testFolderId' isn't set. – Tim van Lint Aug 26 '20 at 21:22
  • 4
    My assumption at this point is that whatever runner is being used to execute that function, is not passing any variables in. And naming the function argument the same as the global variable, will cause the method to not be able to use the global variable, and the value of the argument will be undefined. – Taplar Aug 26 '20 at 21:25
  • 2
    Taplar is right on money. Solution is to simply remove `testFolderId` from `function testGetFolder(testFolderId){`(i.e, `function testGetFolder(){`) OR set the argument with a default parameter: `function testGetFolder(testFolderId="blabka"){` – TheMaster Aug 26 '20 at 21:30
  • Yep, that makes plenty of sense. I had suspicions of that, but there were a few times where passing the variable actually worked (no idea why it was hit and miss like that). – AmericanMade Aug 26 '20 at 21:39
  • 1
    Just so you all know, I removed the parameter from the function, and it works every time. Thanks a lot! – AmericanMade Aug 26 '20 at 21:39
  • I definitely will...but if @Taplar or yourself would like to do it, I'll accept it. If not, I'll make one. – AmericanMade Aug 26 '20 at 21:46
  • Since my comment was a guess, I would encourage you to self answer with the solution you came up with. – Taplar Aug 26 '20 at 21:53

1 Answers1

4

When a function is called directly from the script editor/menu/button click/triggers, the following sequence of actions happens:

  • First, Entire script is loaded and All global statements are executed. This is equivalent to loading a web page with all your script in script tags: <script>...code.gs..</script>

  • The function you called is called. This is like adding callMyFunction() at the bottom of the already loaded script.

  • Except in case of triggers, The function you called is run without passing any arguments. Thus all arguments are undefined

Caution ⚠️: If the function is called by a trigger, the first parameter passed is usually the event object, while the rest of the parameters are undefined.

var testFolderId="1dhhddci6";
//When this function is called by IDE, it called without passing any arguments
function testGetFolder(testFolderId){//<=same as calling `testGetFolder()` or `testGetFolder(null)`
  //testFolderId is declared in local scope , but is undefined
  folder = DriveApp.getFolderById(testFolderId);//<= testFolderId is undefined 

Workarounds:

//When this function is called by IDE, it called without passing any arguments
function testGetFolder(testFolderId="dhhddci6"){//<=same as calling `testGetFolder()`, but `testFolderId` is passed a value. Also same as calling `testGetFolder("dhhddci6")`
  //testFolderId is declared in local scope and is defined(declared and intialized with a value)
  folder = DriveApp.getFolderById(testFolderId);//<= testFolderId is "dhhddci6"
  • If global variables are used, Then the arguments should not be declared.
var testFolderId="1dhhddci6";
//When this function is called by IDE, it called without passing any arguments
function testGetFolder(){//<=same as calling `testGetFolder()`
  //testFolderId is NOT declared in local scope, so variable is looked up in global scope(where it is defined)
  folder = DriveApp.getFolderById(testFolderId);//<= testFolderId is "dhhddci6"

Further reading:

TheMaster
  • 45,448
  • 6
  • 62
  • 85