应用脚本编码问题

时间:2017-11-14 19:21:02

标签: javascript email google-apps-script google-sheets gmail

我是一个新手,并且想知道是否有人可以帮助我使用脚本。我有一个脚本,在提交链接到电子表格的表单时发送确认电子邮件。这很有效,但是第2和第2部分。 3不要。我试图实现的是,一旦请求被授权人员(col O)在电子表格上的授权批准者批准或拒绝,这将生成一封电子邮件给原始请求者(Col B),说明他们的请求已经被批准或否认。如果请求已在电子表格中获得批准,则编辑的列为示例表中的col O,并且未与表单链接/从表单中提供,

以下是示例表单的链接:https://docs.google.com/forms/d/e/1FAIpQLScKFPiA0vERFZCGEGKwkjFknGnHGk4C_fA8HlFpgoFMe0KElg/viewform

以下是电子表格的链接: https://docs.google.com/a/jaguarlandrover.com/spreadsheets/d/1tExQUZ2NFzzG0KE_miDiFHveeJZh3dx0w2_s73A7T4g/edit?usp=sharing

我正在努力工作的部分如下: 第1部分 - 批准:

//This is still under development and is not working correctly at the minute, not sending emails on edit, still needs a bit of work

function sendApprovedMailOnEditOfColO(e) {

    var ss = SpreadsheetApp.getActiveSheet();
    if (ss.getName() !== 'Form Responses 1' || e.range.columnStart !== 14 || e.value !== 'Yes') return;
    var values = ss.getRange(e.range.rowStart, 2, 1)
        .getValues()[14];
    var headers = ss.getRange()
        .getValues()["Yes"];
    var subject = "Visitor Request - Approved";
    var email = values[1];
    var cols = [0];
    var body = "Hi,\n\n We are please to inform you that your visitor request has been approved. Please ensure you inform your visitors that they are to report to main reception and need to be booked in on arrival. Also please ensure you are contactable by telephone during the time your visitors are expected, in order to arrange for them to be collected and accompanied for the duration of their visit.\n\n"; //add more text if you want to..
    for (var i = 14; i < values.length; i++) {
    if (cols.indexOf(i) === "Yes") continue;
        body += headers[i] + ": " + values[i] + "\n"
    }
body += "\n\n\n Please do not respond to this email as it is automatically generated by an account that is not checked."
    GMailApp.sendEmail(email, subject, body, {noReply:true})
}

我尝试为此设置一个onEdit触发器但是它们不会生成任何电子邮件,尽管我运行这些脚本时它们没有在脚本中出现任何错误。可能是Col O没有链接到表单并已手动添加到电子表格中吗?

我不知道如何让它发挥作用。不幸的是由于我们域内的管理员限制,我无法使用邮件合并应用程序,因此需要编写脚本。

任何人都可以帮助提供脚本或建议吗?我很绝望。

1 个答案:

答案 0 :(得分:0)

这是“GmailApp”而不是“GMailApp”。您还必须将函数命名为onEdit()才能使其正常工作。使用getSheetByName(“name”)而不是getActiveSheet()来确保它打开正确的。

请参阅https://developers.google.com/apps-script/guides/triggers/events

使用Logger.log在测试时查看ss等变量的值。