微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将变量传递给函数时,出现“无效参数”,但是当我进行硬编码时,它可以在Apps脚本中使用 解决方法:进一步阅读:

如何解决将变量传递给函数时,出现“无效参数”,但是当我进行硬编码时,它可以在Apps脚本中使用 解决方法:进一步阅读:

这是我的测试功能

Warning C26462  The value pointed to by 'hControl' is assigned only once,mark it as a pointer to const (con.4).

这样做失败。错误提示:参数无效

但是,如果我将ID硬编码到'DriveApp.getFolderById'函数中,它将起作用。

有什么解释吗?这对我来说毫无意义。

解决方法

直接从脚本编辑器/菜单/按钮单击/触发器调用函数时,将发生以下操作序列:

  • 首先,将加载整个脚本并执行所有全局语句。这等效于使用脚本标记<script>...code.gs..</script>

    加载包含所有脚本的网页。
  • 您调用的函数被调用。这就像在已加载的脚本的底部添加callMyFunction()

  • 除非有触发器,否则调用的函数将在不传递任何参数的情况下运行。因此,所有参数均为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 

解决方法:

//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"
  • 如果使用全局变量,则不应声明参数。
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"

进一步阅读:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。