2

This question already has an answer here:

I have a Google Spreadsheet with a list of emails. I want to build a routine that send email automatically to those email addresses. I also want to attach a PDF to this email. The PDF file is located in my Google Drive.

When running the following script, I'm getting the following error:

TypeError: Cannot find function getAs in object FileIterator. (line 21, file "Code")

I'm new to Google Spreadsheets, please can you help me out?

This is the code:

// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 2)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0];  // First column
    var message = row[1];       // Second column
    var emailSent = row[2];     // Third column
    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      var subject = "Sending emails from a Spreadsheet";
      var file = DriveApp.getFilesByName('test123.pdf')
      MailApp.sendEmail(emailAddress, subject, message, {
     attachments: [file.getAs(MimeType.PDF)],
     name: 'Automatic Emailer Script'
 });
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

marked as duplicate by user79865, Alex, Rubén, Vidar S. Ramdal, codingbadger Sep 25 '15 at 7:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

The method DriveApp.getFilesByName returns a file iterator, not an individual file. This is why you can't just grab the contents of file. A more correct version is

  var file = DriveApp.getFilesByName('test123.pdf');
  if (file.hasNext()) {
    MailApp.sendEmail(emailAddress, subject, message, {
    attachments: [file.next().getAs(MimeType.PDF)],
    name: 'Automatic Emailer Script'
  }

If there is no file with given name, no email is sent. If there is more than one file with the given name (which can easily happen in Google Drive), you'll send whatever is the first file that comes up in the iterator.

If you want a more reliable way of grabbing files, use DriveApp.getFileById

0

Looking at the MailApp code sample, on the Google Developers Apps Script website, I noticed the following difference:
enter image description here

I suggest that you add the blob.

Reference: SendMail with advanced options

  • 1
    This code sample has "blob" because it is sending two attachments; one from a file, and one from a blob created on the fly. This isn't related to the issue the OP had. – user79865 Sep 19 '15 at 21:13
0

In your code you wrote:

var file = DriveApp.getFilesByName('test123.pdf')

file is now an object containing all files with the name 'test123.pdf'
The file in the object you want is file[0]

In order to get the file you want, you can use

MailApp.sendEmail(emailAddress, subject, message, {
     attachments: [file[0].getAs(MimeType.PDF)],
     })
  • This is incorrect; file is an instance of FileIterator which is not an array. file[0] would be undefined. Use file.next(). – user79865 Sep 19 '15 at 20:55

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