Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

664
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda