使用 Google 应用程序脚本在 Gmail 草稿中附加来自 PC 的文件

Attach file from PC in Gmail Draft using Google App Script

目前,我正在使用 Google App 脚本从电子表格创建 Gmail 草稿,最后缺少的是我想直接从计算机附加“Gmail 草稿”中的文件,而不是上传 G -开车。

下面是我当前的代码。

在这里我想添加一个名为“文件路径”的列,我将在其中输入我需要在所述电子邮件中附加的文件的路径。那么 GS 应该会自动从我的电脑中选择文件路径并将其附加到 Gmail 草稿中。

提前致谢。

function Loop_Email_Drafts_with_Signature() {

const signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var index = 2;
var lastRow = sheet.getLastRow();

for (;index <= lastRow; index++){
var emailAddress = sheet.getRange(index, 1, 1, 1).getValue();
var ccmail = sheet.getRange(index, 2, 1, 1).getValue();
var subject = sheet.getRange(index, 3, 1, 1).getValue();
var message = sheet.getRange(index, 4, 1, 1).getValue();

  
const html = "<div> </div><br>" + signature;
const options = {
  htmlBody: message + html,
  cc: ccmail
}  
GmailApp.createDraft(emailAddress, subject, '', options);



}// End of For Loop
}// End of Function

PS:我是 Google 应用程序脚本/Java 脚本的新手。所以请通过编写/修改我当前的代码来详细帮助。

经过多次尝试后我得到了这个代码:)

为社区利益与他人分享。

function DownloadAndSendWithSignature123() {
  
  
  //DECLARATIONS
  const signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
  var sheetName = "Test email GAS";
  var sourceSpreadsheet = SpreadsheetApp.getActive();
  var sourceSheet = sourceSpreadsheet.getActiveSheet();
  var to = sourceSheet.getRange("B8").getValue();
  var ccmail = sourceSheet.getRange("B9").getValue();
  var body = sourceSheet.getRange("B13").getValue();
  var filename = sourceSheet.getRange("K5").getValue();
  var pdfName = filename;
  var subject = sourceSheet.getRange("B11").getValue();
  var link = sourceSheet.getRange("K3").getValue();
  
  
  //DOWNLOAD FILE
  var response = UrlFetchApp.fetch(link);
  var fileBlob = response.getBlob()
  //var folder = DocsList.getFolder('Whosebug');
  var folders = DriveApp.getFolderById("Folder ID Goes here");
  
  var file =  folders.createFile(response);
   
  debugger;  // Stop to observe if in debugger
  
  
  
  
  

  //SAVE FILE TO DRIVE
  var listOfFiles = DriveApp.getFilesByName(filename); //returns FileIterator of files that match name given.
  if(listOfFiles.hasNext()){ //We should check if there is a file in there and die if not.
    var file = listOfFiles.next(); //gets first file in list.
        
    // SEND EMAIL THAT FILE
    
    const html = "<div> </div><br>" + signature;
    const options = {
      htmlBody: body + html,
      cc: ccmail,
      attachments: file
      
    }  
    GmailApp.createDraft(to, subject, '', options);

  }else{
    console.log("Error no file in listOfFiles. Email not sent.");
  }
 }