问题
I'm trying to create a script for new google sheets that sends a notification if a form is submitted and contains data in a specific column.
In this example, if the question that populates data in column E is answered in the form, I would like an email notification with a link to the spreadsheet.
In the below script, if I set the trigger to on edit it will work if column E is edited. I cannot get it to work with on form submit, however. Can anyone help with fixing this script to work on edit?
Thanks!
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses");
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = "email@email.com";
var subject = 'subject';
var body = 'body';
if (cell.indexOf('E')!=-1) {
MailApp.sendEmail(recipients, subject, body);
}
}
回答1:
when your script runs on a form submission trigger , there is no such thing as active cell, the form data is inserted in the last row of the sheet.
You only have to look at this last row and check the right column (getLastRow()
and column 5 for "E").
来源:https://stackoverflow.com/questions/24374737/google-sheets-script-that-sends-a-notification-upon-form-submission-if-data-is-r