将 Google 云端硬盘文件作为附件添加到之前在 Google Apps 脚本中创建的草稿中

问题描述 投票:0回答:0

我目前正在开发一个 Apps 脚本项目,我需要将两个 Google Drive 文件作为附件添加到现有的 Gmail 草稿中,而不修改草稿的其他部分,例如其主题或正文。然而,当前检索草稿、更新其内容并添加附件的实现似乎会破坏草稿的结构,甚至导致草稿 ID 的更改。

我想知道是否有一种更高效、更可靠的方法可以将这些 Google Drive 文件添加到草稿中,而不改变其现有内容或影响草稿的 ID。理想情况下,我想保留草稿的结构,仅将新附件附加到现有附件中。

这是我当前使用的代码片段:

var fileId1 = 'xxxxxxxxxxxxxxxxxxxxxx';
var fileId2 = 'yyyyyyyyyyyyyyyyyyyyyy';

// Retrieve the existing draft
var draft = GmailApp.getDraft(draftId);
var recipient = draft.getMessage().getTo();
var subject = draft.getMessage().getSubject();
var body = draft.getMessage().getBody();

// Get the existing attachments of the draft
var existingAttachments = draft.getMessage().getAttachments();

// Attach the first file to the email
var file1 = DriveApp.getFileById(fileId1);
var attachmentBlob1 = file1.getAs(MimeType.PDF);

// Attach the second file to the email
var file2 = DriveApp.getFileById(fileId2);
var attachmentBlob2 = file2.getAs(MimeType.PDF);

// Combine existing attachments with new attachments
var allAttachments = existingAttachments.concat(attachmentBlob1, attachmentBlob2);

// Update the draft with recipient, subject, body, and attachments
draft.update(recipient, subject, body, {
  attachments: allAttachments
});

我很感激任何能帮助我实现所需功能,同时保持草案结构和完整性的见解或替代方法。谢谢!

google-apps-script google-drive-api gmail gmail-api email-attachments
© www.soinside.com 2019 - 2024. All rights reserved.