Search code examples
csvgoogle-apps-scriptemail-attachments

Attempting to Attach a CSV, Converting to PDF


I have a code where I am attempting to attach a CSV saved in Drive (automatically replaced) to an email. At the moment, I can get it to add using fileBlob - DriveApp.getFileById(...).getBlob() but, it's attaching as a PDF.

Per Mogsdad's answer on this question there is a way to attach a CSV, but looking through the documentation I can't seem to find anything that will allow using a CSV. In fact, according to the documentation for getAS() it literally says: "For most blobs, 'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of 'image/bmp', 'image/gif', 'image/jpeg', or 'image/png' are also valid."

Does this mean in is impossible to attach a CSV (or, really, anything other than PDF) from Drive to an email using Apps Scripts?


Solution

  • Assuming the CSV file is in your Drive, you can use this snippet to attach it to an email.

    function sendCSV() {
      var blob = DriveApp.getFileById(FILE_ID).getBlob();
      MailApp.sendEmail("email@example.com", "Subject", "CSV", {attachments:[blob]});
    }