且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Sheets 自定义菜单可以将变量传递给函数吗?

更新时间:2023-01-10 15:20:47

当您使用

.addItem('Nathaniel MacIver', menuItem2('nm@emailaddress.com'))

使用参数nm@emailaddress.com"调用函数 menuItem2.这会导致您看到警报.函数的返回值是未定义的(因为你没有从中返回任何东西).所以你最终得到了与

the function menuItem2 is called with the parameter 'nm@emailaddress.com'. This results in the alert that you see. The return value of the function is undefined (as you don't return anything from it). So you end up with the same menu item as if it was

.addItem('Nathaniel MacIver', undefined)

这显然不会做任何事情.

which clearly isn't going to do anything.

方法 addItem 只需要一个函数名,它不允许向该函数传递参数.要执行您想要的操作,您需要为每个人设置不同的功能,每个功能都在该功能内硬编码了一封电子邮件.

The method addItem takes only a function name, it does not allow for passing parameters to that function. To do what you want, you'll need separate functions for each person, each with an email hardcoded inside that function.