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.

10
  • 2
    So, something other than code you have written is causing that function to execute? You don't have testGetFolder(testFolderId); any where? Commented Aug 26, 2020 at 21:18
  • 2
    If you only select that function and run it, probably your variable 'testFolderId' isn't set. Commented Aug 26, 2020 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. Commented Aug 26, 2020 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"){ Commented Aug 26, 2020 at 21:30
  • 1
    Just so you all know, I removed the parameter from the function, and it works every time. Thanks a lot! Commented Aug 26, 2020 at 21:39

1 Answer 1

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:

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

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.