Attach multiple files - Google Sheets/Gmail/Drive

▼魔方 西西 提交于 2021-01-29 08:19:27

问题


I currently have a script to create draft emails and also attach a file to them. Currently, the script does not work for multiple file attachments and also does not work if there are no attachments.

Any tips on how to get (1) and (2) [below]

Updated for more info:

Main objective is to add a multiple file names in Col[3] and attaching the files to the draft using the files names provided in Col[3]. Example: if I type the following into Col[3]: "test.pdf, test2.pdf" it will attach the files "test.pdf" & "test2.pdf" for that particular draft. At the moment, the script does attach a single file to a draft, but if I enter multiple file names separated by comas it does not create a draft at all.

I've attach a screenshot of how the spreadsheet looks like below when I run the script (I currently cannot embed pictures):

https://imgur.com/a/sVoHO3Q

When I run the script using the info from the screenshot, the 2nd row does not create a draft at all, the 3rd row creates a draft with the attachment and the 4th row does not create a draft at all.

Would it be possible to create a draft and attach multiple files using filenames separated by comas and also create drafts when some cells in Col[3] is empty?

TLDR:

  1. How to attach multiple files to individual drafts

  2. How to create a draft without any attachments

function SaveDrafts() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;
  var rows = 1000;
  var dataRange = sheet.getRange(startRow, 1, rows, 5);
  var data = dataRange.getValues();

  for (i in data) {
    var row = data[i];
    var emailAddress = row[0];
    var subject = row[1];
    var message = row[2];
    var pdfName = row[3];
    var cc = row[4];
    var list = DriveApp.getFilesByName(pdfName);

    if (list.hasNext()) {
      var File = list.next();
      GmailApp.createDraft(emailAddress, subject, message, {
        cc: cc,
        attachments: [File]
      });
    }
  }
}

回答1:


Update: First off, I performed a split() on the values of the pdfName cell so I could search for and add one file at a time.

The draft creation would only run if there were files because it was inside the list if statement, no files meant it would not run.

I pulled it out of that and created an interator for list and a files array that can be given to the createDraft() function. The iterator allows multiple Files to be added to the files array if the DriveApp.getFilesByName(pdfName) returns multiple Files.

function SaveDrafts() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;
  var rows = 1000;
  var dataRange = sheet.getRange(startRow, 1, rows, 5);
  var data = dataRange.getValues();

  for (i in data) {
    var row = data[i];
    var emailAddress = row[0];
    var subject = row[1];
    var message = row[2];
    var pdfName = row[3].split(','); //split the values taken from cell into array
    var cc = row[4];

    var files = []; //initialize files as empty array.

    for(var j in pdfName){ //run through cell values and perform search
      var results = DriveApp.getFilesByName(pdfName[j]); //Perform the search,results is a FileIterator

      while(results.hasNext()) { //Interate through files found and add to attachment results
        files.push(results.next()); //Add files to array
      }
    }

    GmailApp.createDraft(emailAddress, subject, message, {
      cc: cc,
      attachments: files //the attachments option takes our files array
    });

  }
}


来源:https://stackoverflow.com/questions/54948974/attach-multiple-files-google-sheets-gmail-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!