I have file names separated by commas (no spaces) in a google sheets cell 'filez1.pdf,filez2.pdf'.

If I assign a variable as a string;

flist = "filez1.pdf,filez2.pdf"

or an array

flist = ["filez1.pdf", "filez2.pdf"]

I cannot send multiple files as below.

I have tried numerous syntax for XXXX e.g. flist[], [flist], flist[0]


MailApp.sendEmail(emailAddress,subject ,message, {attachments: XXXX});


and keep getting the same error 'Invalid argument: attachments at SendMail(SendEmail:15)'

This works but I don't know how to address multiple files;

var flist = DriveApp.getFilesByName('filez1')

MailApp.sendEmail(emailAddress,subject ,message, {attachments:


Any help would be great.


1 Answer 1


Since no other answer was found anywhere I pursued the problem till I found something that works for me.

function SendMail() {
     var spreadsheet = SpreadsheetApp.getActive();
     spreadsheet.setActiveSheet(spreadsheet.getSheetByName('SheetName'), true);
     var sheet = spreadsheet.getActiveSheet();  
     var subject = spreadsheet.getRange("B2").getValue();
     var emailAddress = spreadsheet.getRange("D2").getValue();
     var message = spreadsheet.getRange("C2").getValue();
     var filesarray = []
     var files = []
     var startRow = 1;
     var lastRow = spreadsheet.getRange("A1:A").getValues().filter(String).length;

       for (var i = 3; i <= lastRow; i++)
       {    filesarray[i-3] = sheet.getRange(i, 1).getValue(); //load column values, file names, into filesarray

       for (var i = 0; i <= lastRow-3; i++)

       {var file = DriveApp.getFilesByName(filesarray[i]); // assign gdrive name to new array files
  while(file.hasNext()){var tfly=file.next();}

  MailApp.sendEmail(emailAddress,subject ,message, {attachments: files});

  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Back to calling sheet'), true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.