问题
I use the following code to send reminder emails to all the emails that are in a certain cell in column 7. But, it gives an error that "Cannot find SendEmail function". Would anyone please help me?
function myfunction() {
var sheet = SpreadsheetApp.getActiveSheet();
// figure out what the last row is
var lastRow1 = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 5
var startRow1 = 5;
// grab column 6 (the 'days left' column)
var range = sheet.getRange(5,6,lastRow1-startRow1+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Now, grab the reminder name column
range = sheet.getRange(5, 3, lastRow1-startRow1+1, 1);
var reminder_info_values = range.getValues();
range = sheet.getRange(5, 7, lastRow1-startRow1+1, 1);
var emails_info_values = range.getValues();
var warning_count = 0;
var msg = "";
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left == 1) {
// if it's exactly 1, do something with the data.
var reminder_name = reminder_info_values[i][0];
msg = msg + "Reminder: "+reminder_name+" is due in "+days_left+" days.\n";
warning_count++;
}
var emails= emails_info_values [i][0];
if(warning_count) {
MailApp.sendEmail("emails", "msg");
}
}
}
回答1:
As Cooper mentioned in his comment The MailApp.sendEmail()
function has inputs of either a mail object or if you wanted to send a simple string text as: MailApp.sendEmail(recipient, subject, body)
where recipient
is a string, subject
is a string, and body
is a string.
The error that your function returns is stating exactly:
Cannot find method sendEmail(string,string).
Which is meaning to say that it can't find the sendEmail function that handles two string inputs, when it's actually looking for sendEmail(string,string,string)
回答2:
Question
Why my sendEmail is not working?
Answer
Your sendEmail syntax is wrong, you are missing subject
- Check the correct syntax for sendEmail()
function sendEmail(){
MailApp.sendEmail(recipient, subject, body)
}
Example:
function sendEmail(){
MailApp.sendEmail("user4172070@gmail.com", "testing sendEmail via Apps Script", "It's working")
}
- Adapting to your code
MailApp.sendEmail(emails, "Email Subject", msg);
Final version
function myfunction() {
var sheet = SpreadsheetApp.getActiveSheet();
// figure out what the last row is
var lastRow1 = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 5
var startRow1 = 5;
// grab column 6 (the 'days left' column)
var range = sheet.getRange(5, 6, lastRow1 - startRow1 + 1, 1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Now, grab the reminder name column
range = sheet.getRange(5, 3, lastRow1 - startRow1 + 1, 1);
var reminder_info_values = range.getValues();
range = sheet.getRange(5, 7, lastRow1 - startRow1 + 1, 1);
var emails_info_values = range.getValues();
var warning_count = 0;
var msg = "";
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var days_left = days_left_values[i][0];
if(days_left == 1) {
// if it's exactly 1, do something with the data.
var reminder_name = reminder_info_values[i][0];
msg = msg + "Reminder: " + reminder_name + " is due in " + days_left + " days.\n";
warning_count++;
}
var emails = emails_info_values [i][0];
if(warning_count) {
MailApp.sendEmail(emails, "Email Subject", msg);
}
}
}
Reference:
- MailApp.sendEmail()
来源:https://stackoverflow.com/questions/59941059/cannot-find-google-sendemail-function