问题
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:
How to attach multiple files to individual drafts
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