Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

655
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda