Google apps script 如何将PDF文件从Google Drive附加到附件阵列?

Google apps script 如何将PDF文件从Google Drive附加到附件阵列?,google-apps-script,Google Apps Script,我已经修改了以下代码片段,尝试用谷歌脚本将来自谷歌硬盘的PDF文件附加到电子邮件中 var optionalAttachmentCell = ss.getSheets()[6].getRange('L2').getValue(); //Here is where the File ID is var optionalAttachment = DriveApp.getFileById(optionalAttachmentCell); I believe this should get the

我已经修改了以下代码片段,尝试用谷歌脚本将来自谷歌硬盘的PDF文件附加到电子邮件中

 var optionalAttachmentCell = ss.getSheets()[6].getRange('L2').getValue(); //Here is where the File ID is
 var optionalAttachment = DriveApp.getFileById(optionalAttachmentCell); I believe this should get the file
几行之后,我将optionalAttachment[0]作为函数的参数

 sendSpreadsheetToPdf2(8, shName, ss.getRange('Constants!G25').getValue(), "Pay for the week ending "+date, body, date, driverArray[i],ss.getRange('Constants!G26').getValue(), scheduleBlob, i, Week2Blob, optionalAttachment[0]);
我觉得这里出了问题

if(blob){ //this is a blob that prints out a sheet
  attachmentArray.push(blob); // I attach the sheet to the attachmentArray which starts blank
  if (send1st === true) {  // these are optional spreadsheet blobs I attach
    attachmentArray.push(schedulePDF);
  }
  if (send2nd === true) {  // another optional blob
    attachmentArray.push(twoWeeksOut);
  }
  if(optional){
  attachmentArray.push(optional) // This is where I have the opitionalAttachment being put into the array
  }}
然后我使用

  mailOptions.attachments = attachmentArray;
 
  if(htmlbody){
  mailOptions.htmlBody = htmlbody;
  }
  
  if(email){
  GmailApp.createDraft(email, subject,"",mailOptions); 
如果可能的话,我想保持附件的原样,但只需添加可选附件,它是一个PDF文件。我不知道哪里出了问题;我认为这与blob/PDF格式有关。请提供建议。谢谢。

说明: 来自的高级参数
attachments
接受一个数组。因此,必须通过检索相应的
Blob

解决方案: 我通过修改这一行获得了要附加的PDF:

 var optionalAttachment = DriveApp.getFileById(optionalAttachmentCell).getBlob();

这样,文件将像我在附件数组中推送的其他blob一样成为blob。

请添加一个。我编辑了您的答案,以澄清问题所在,并修复错误。