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

293
Visualizações
getting TypeError: Cannot read property 'getAs' of undefined , in google app script

i am trying to write a script to send mail and a pdf as attachment using app script but i keep getting this error

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = SpreadsheetApp.getActiveSheet().getRange('I1').getValue(); // getting number of rows by fetching value from a count function in sheet
  var dataRange = sheet.getRange(startRow, 1, numRows, 2);
  // 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 link = row[1];// Fetching ID by using mid fuction in sheet
    var file = DriveApp.getFileById(link);
    var subject = 'Sending emails from a Spreadsheet';
    var htmlBody = DriveApp.getFileById("14KhceDa2lg7ojnwzFwpfiJCCx-xldPPj")
    MailApp.sendEmail(emailAddress, subject, {htmlBody: htmlBody}, {attachments: [file[0].getAs(MimeType.PDF)]})
  }
}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You get only file object using DriveApp.getFileById so use file.getAs() directly as shown in below code

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = SpreadsheetApp.getActiveSheet().getRange('I1').getValue(); // getting number of rows by fetching value from a count function in sheet
  var dataRange = sheet.getRange(startRow, 1, numRows, 2);
  // 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 link = row[1];// Fetching ID by using mid fuction in sheet
    var file = DriveApp.getFileById(link);
    var subject = 'Sending emails from a Spreadsheet';
    var htmlBody = DriveApp.getFileById("14KhceDa2lg7ojnwzFwpfiJCCx-xldPPj")
    MailApp.sendEmail(emailAddress, subject, {htmlBody: htmlBody}, {attachments: [file.getAs(MimeType.PDF)]})
  }
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

 MailApp.sendEmail(emailAddress,subject,'',{htmlBody: htmlBody,attachments: [file.getAs(MimeType.PDF)]})
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