发送带有附件的Google表格电子邮件ERROR

问题描述

我想通过电子邮件发送附件,但出现错误

SyntaxError:无效或意外的令牌(第17行,文件“ try.gs”)

我不明白为什么?名为“ i.pdf”的pdf文件在我的Google驱动器中

这是我的代码

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process

// Fetch the range of cells A3:E3
// Column A = Name,Column B = Email,Column C = Message,Column D = Message1,Column E = Message2
var datarange = sheet.getRange(startRow,1,numRows,4)

// Fetch values for each row in the Range.
var data = datarange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2]; // First column of selected data
var message = "hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName(‘i.pdf’);
if (file.hasNext())
MailApp.sendEmail(emailAddress,subject,message,{
attachments: [file.next().getAs(MimeType.PDF)],name: ‘Simple mail’});

}
}

有人可以帮我吗

解决方法

该错误是由印刷/大写/单引号字符= ‘’引起的。

用单引号或双引号字符''""代替。

相关

,

在声明var file时和在MailApp.sendEmail()函数中,您有两种错别字。尝试以下方法:

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process

// Fetch the range of cells A3:E3
// Column A = Name,Column B = Email,Column C = Message,Column D = Message1,Column E = Message2
var dataRange = sheet.getRange(startRow,1,numRows,4)

// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2]; // First column of selected data
var message = "Hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName("i.pdf");
if (file.hasNext())
MailApp.sendEmail(emailAddress,subject,message,{
attachments: [file.next().getAs(MimeType.PDF)],name: "Simple mail"});

}
}