Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

javascript - When passing variable to function I get 'Invalid argument', but when I hard code it works in Apps Script

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...