0
這裏是我下面的代碼,如果我改變功能getFilesByName()來getFileById(),那麼這工作完全正常,什麼是不同的,這是爲什麼不工作的這應該?谷歌電子表格錯誤不發送文件
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 4; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[0];
var emailAddress = row[1]; // First column
var filename = DriveApp.getFilesByName(row[2]); // Second column
var message = row[3]; // Third column
var emailSent = row[4]; // Fourth column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "TEST" + name;
MailApp.sendEmail(emailAddress, subject, message, {
name: 'TEST',
attachments: [filename.getAs(MimeType.PDF)]
});
sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
好使用的.next()工作得很好:)對不起我是有點新的谷歌的腳本,但是從我的理解,如果你不getFileByName後指定的.next(),因爲名稱可以是重複和id的不能,我是對? – thechrishaddad
這是正確的,你可以看到[DriveApp文件]中的返回類型(https://developers.google.com/apps-script/reference/drive/drive-app),getFileById返回一個文件,而getFilesByName返回FileIterator ,並排文檔存放一邊編碼,讓你知道你有什麼方法avaible,什麼是返回類型。 – Kriggs
會做,感謝隊友,感謝它。 – thechrishaddad