使用Google App脚本在Gmail草稿中从PC附加文件

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

下面是我当前的代码。

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

提前谢谢。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
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脚本的新手。因此,请通过编写/修改我当前的代码来提供详细帮助。


解决方案

我经过多次尝试才得到此代码:)

为他人分享社区利益。

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
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('StackOverflow');
  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.");
  }
 }

相关文章