Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

654
Views
Google App Script : Error when attaching multiple images (Unexpected error while getting the method or property getFileById on object DriveApp)

I am writing an automated script to send mails from my Google sheets and I want to attach multiple Images. With only one image everything works perfectly fine but when I want to attach two or more, it gives me this Error message:

Exception: Unexpected error while getting the method or property getFileById on object DriveApp.

This is my code right now:

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1; // First row of data to process
  var numRows = 3; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i in data) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = row[1]; // Second column
    var image = DriveApp.getFileById(row[2]).getBlob();
    var image2 = DriveApp.getFileById(row[3]).getBlob();
    var subject = 'TAYDO 2021 Certificate';
  }

  MailApp.sendEmail({
    to:emailAddress, 
    subject:subject,   
    body:message,  
    attachments: [image.next(),image2]
  });
}

The App script has all permissions needed to run this script, and sheets, drive and everything is under the same Google Acc. Can anyone help? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This works for me:

function attachimages() {
  let fldr = DriveApp.getFolderById(gobj.globals.imagesfolderid);
  let files = fldr.getFiles();
  let imgA = [];
  while(files.hasNext()) {
    let file = files.next();
    if(file.getName().slice(0,3) == 'die') {
      imgA.push(file.getBlob().getAs('image/jpeg'))
    }
  }
  MailApp.sendEmail({to:'jimesteban@jimesteban.com',subject:'Test Images Attachment',body: 'See Attached Images', attachments:imgA});
}

My images happened to be named die1.jpg, die2.jpg .... and so on they are images of the faces of a dice.

This also worked for me:

function sendEmails() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const rg = sh.getRange(2, 1, 3, 4);
  const vs = rg.getValues();
  vs.forEach((r, i) => {
    let emailAddress = r[0];
    let message = r[1];
    let image1 = DriveApp.getFileById(r[2].toString()).getBlob();
    let image2 = DriveApp.getFileById(r[3].toString()).getBlob();
    let subject = 'Test Attach Images';
    MailApp.sendEmail({ to: emailAddress, subject: subject, body: message, attachments: [image1, image2] });
  });
}

Sheet0:

Email Message Image1 Image2
Redacted Message1 Redacted Redacted
Redacted Message2 Redacted Redacted
Redacted Message3 Redacted Redacted
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!