获取对象 DriveApp 上的方法或 属性 getFileById 时出现意外错误

Unexpected error while getting the method or property getFileById on object DriveApp

我一直在尝试解决这个问题,但我是编程新手。我想从 sheet 发送带有发票附件的电子邮件。 sheet 包含所有必要的信息,例如发票编号、发票文件名、文件驱动器 ID、客户电子邮件等。

我为此使用的代码是从另一个有效的应用程序脚本中回收的。它似乎无法经受住我在这里尝试的修改。

function invoiceDispatcher() {

const invoiceDispatcherSheet = SpreadsheetApp.openById("SPREADSHEETID").getSheetByName("invoicedispatcherlog");
let emailTemplate = HtmlService.createTemplateFromFile("invoiceEmailTemplate");
let invoiceNumber = 0;
let invoiceDate = 9;
let customerEmail = 8;
let invoiceName = 1;
let invoiceFileID = 2;


let transactionData = invoiceDispatcherSheet.getRange(2,1,invoiceDispatcherSheet.getLastRow()-1,13).getValues();
let cleanData = transactionData.filter(function (cases){ return cases[11] === true});

cleanData.forEach(function(row){
   emailTemplate.rn = row[invoiceNumber];
   emailTemplate.rd = Utilities.formatDate(row[invoiceDate], "CEST" , "dd.MM.yyyy");
   //let invoiceFileName = String(row[1]);
   let invoiceFile = DriveApp.getFileById(invoiceFileID).getAs(MimeType.PDF);
   //let invoiceFile = DriveApp.getFilesByName([invoiceFileName]);
   let invoiceMessage = emailTemplate.evaluate().getContent();
   GmailApp.sendEmail(
      row[customerEmail], "Your Invoice Document "+ row[invoiceNumber],
      "Please open this message in an HTML-compatible email client. Thank you",
      {name: "Company Name", htmlBody: invoiceMessage, attachments: [invoiceFile]});
})

}

此外,我想在 sheet 的 K 列(第 10 列)中添加时间戳。

我确信我这里有一些冗余,我还不能完全理解函数和方法如何相互关联的所有方面,但非常感谢您提供一些指导。如果有任何不清楚的地方以及我是否可以提供更多信息,请告诉我。

试试这个:

function invoiceDispatcher() {
  const sh = SpreadsheetApp.openById("SPREADSHEETID").getSheetByName("invoicedispatcherlog");
  let t = HtmlService.createTemplateFromFile("invoiceEmailTemplate");
  let transactionData = sh.getRange(2, 1, sh.getLastRow() - 1, 13).getValues();
  let cleanData = transactionData.filter(function (cases) { return cases[11] == true });
  cleanData.forEach(function (row) {
    t.rn = row[0];
    t.rd = Utilities.formatDate(row[9], "CEST", "dd.MM.yyyy");
    let invoiceFile = DriveApp.getFileById(row[2]).getAs(MimeType.PDF);
    let invoiceMessage = t.evaluate().getContent();
    GmailApp.sendEmail(row[8], "Your Invoice Document " + row[0],
      "Please open this message in an HTML-compatible email client. Thank you",
      { name: "Company Name", htmlBody: invoiceMessage, attachments: [invoiceFile] });
  })
}