【问题标题】:how to pass current user's email address to Google Script如何将当前用户的电子邮件地址传递给 Google Script
【发布时间】:2014-12-12 05:35:22
【问题描述】:

我在 Google 电子表格后面有一个脚本,一旦完成某行的某些单元格,它就会发送一封电子邮件。该脚本通过发送到多个硬编码的电子邮件地址来工作。如何将当前用户的电子邮件地址添加为第二个抄送?其中一个 CC 是硬编码的,但第二个会根据更新电子表格的人而变化。我知道如何获取电子邮件地址,但如何将它作为变量传递,以便它真正被抄送?

var currentemail = Session.getActiveUser().getEmail();

var options = {cc: 'Session.getActiveUser().getEmail(), someotheremail@domain.com'}; 

var options = {cc: 'currentemail, someotheremail@domain.com'};

GmailApp.sendEmail(email, subject, body, options);

显然这些不起作用:)

在此先感谢

【问题讨论】:

    标签: email google-apps-script google-sheets carbon-copy


    【解决方案1】:

    你的例子应该修改如下:

    var options = {cc: Session.getActiveUser().getEmail()+', someotheremail@domain.com'};
    

    或者

    var options = {cc: currentemail+', someotheremail@domain.com'};
    

    您不能从字符串中调用函数或引用变量。相反,使用 + 运算符将变量的值(或函数调用的返回值)与字符串连接起来。

    请注意,根据使用此代码的上下文,脚本可能无权访问用户身份。请在此处查看 GetActiveUser() 下的注释:https://developers.google.com/apps-script/reference/base/session#getActiveUser()

    【讨论】:

      【解决方案2】:

      这可以像下面这样完成:

      function sendWithCc(){
         var currentemail = Session.getActiveUser().getEmail();
         var options = {};// create object
         options['cc'] = currentemail+',someotheremail@domain.com';// add key & values (comma separated in a string)
         GmailApp.sendEmail('somemail@domain.com', 'subject', 'body', options);
         // I stringified 'subject' and 'body' to make that test work without defining values for it
      }
      

      【讨论】:

        猜你喜欢
        • 2014-01-28
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        • 1970-01-01
        • 2013-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多