Google app maker 正在尝试发送创建通知电子邮件

Google app maker 正在尝试发送创建通知电子邮件,google-app-maker,Google App Maker,我希望在用户创建包含该条目(或小部件)信息的条目时发出通知电子邮件 例: 致:groupemail@company.com 主题:项目名称 正文:“User1已创建状态为“正在进行”的新项目 我试图查看Project Tracker应用程序以供参考,但我还是迷路了 我假设我需要创建一个客户端脚本,执行类似下面的函数的操作,并将该函数添加到页面片段中“Submit Button”的onClick事件中,以创建新条目,但它不起作用 单击事件时提交按钮: widget.datasource.crea

我希望在用户创建包含该条目(或小部件)信息的条目时发出通知电子邮件

例:

致:groupemail@company.com

主题:项目名称

正文:“User1已创建状态为“正在进行”的新项目


我试图查看Project Tracker应用程序以供参考,但我还是迷路了

我假设我需要创建一个客户端脚本,执行类似下面的函数的操作,并将该函数添加到页面片段中“Submit Button”的onClick事件中,以创建新条目,但它不起作用

单击事件时提交按钮:

widget.datasource.createItem();
sendEmailCreated_(to, subject, body);
app.closeDialog();
    function sendEmailCreated_(to, subject, body) {
      var widgets = widget.root.descendants;     
      try {
        MailApp.sendEmail({
          to: 'groupemail@company.com',
          subject: widgets.ProjectName.value,
          body: 'User1 has created a new project with the Status of' + widgets.ProjectStatus.value,
          noReply: true
        });
       } catch (e) {
        // Suppressing errors in email sending because email notifications
        //   are not critical for the functioning of the app.
        console.error(JSON.stringify(e));
      }
    }
客户端脚本:

widget.datasource.createItem();
sendEmailCreated_(to, subject, body);
app.closeDialog();
    function sendEmailCreated_(to, subject, body) {
      var widgets = widget.root.descendants;     
      try {
        MailApp.sendEmail({
          to: 'groupemail@company.com',
          subject: widgets.ProjectName.value,
          body: 'User1 has created a new project with the Status of' + widgets.ProjectStatus.value,
          noReply: true
        });
       } catch (e) {
        // Suppressing errors in email sending because email notifications
        //   are not critical for the functioning of the app.
        console.error(JSON.stringify(e));
      }
    }
当函数尝试运行时,它会说“to”未定义,并且我确信我没有使用“widgets.ProjectName.value”之类的正确选项

任何帮助都将不胜感激


多谢各位

没有用于发送电子邮件的客户端API。您需要将“sendmailcreated”函数移动到服务器脚本中,并在onCreate model事件中调用它(从安全角度来看,最好是这样):

//onCreate模型事件接收到关于创建记录的消息
//函数名末尾的Underscore表示
//函数是私有的(无法从客户端调用)
sendmailcreated(record.to、record.subject、record.body);
…或在创建回调中使用google.script.run(因为您向最终用户公开了邮件API,所以安全性较低):

widget.datasource.createItem({
成功:功能(记录){
google.script.run
.withSuccessHandler(函数(){
//TODO:处理成功
})
.withFailureHandler(函数(错误){
//TODO:处理失败
})
.sendmailcreated(record.to、record.subject、record.body);
//sendEmailCreated-是一个服务器端功能。
//它是公共的(可以从客户端调用),因为
//它不会以下划线结尾
},
失败:功能(错误){
//TODO:处理失败
}
});
如果您不关心安全性和错误处理,那么代码的客户端可以简化为:

widget.datasource.createItem(函数(记录){
google.script.run.sendmailcreated(record.to、record.subject、record.body);
});
有关该主题的有用链接:


除了不使用服务器端脚本(MailApp实际上可以在服务器端访问)之外(正如Pavel在回答中指出的那样),我还将指出出现此错误的原因

你正在使用

sendEmailCreated_(to, subject, body);
从OnClick事件开始,没有定义to、subject或body。相反,当尝试将某个小部件传递到客户端脚本时,您应该使用如下内容:

doSomething(widget);
(因为您可以从小部件中检查onClick是否允许直接访问当前小部件)

函数应该是这样的

function doSomething(widget) {
  var variable1 = widget.value;
  doSomethingElse(variable1);
}
因此,您需要确保已经定义了要发送给函数的参数

如果您使用了与以下内容类似的内容,您的电子邮件示例将不会出现这些特定错误(但Pavel部分解释了不同的错误)

var to = "example@example.com";
var subject = "Example subject";
var body = "Example text body";
sendEmailCreated_(to, subject, body);