Home > database >  How to attach a calculated CSV with MailApp in Google Apps Script
How to attach a calculated CSV with MailApp in Google Apps Script

Time:11-15

  • I have a matrix array with some values that I need to parse to a CSV file.

  • I have the following code:

    let array = [["name1", 2, 3], ["name2", 4, 5], ["name3", 6, 7], ["name4", 8, 9], ["name5", 10, 11]];
    let lineArray = []
    array.forEach((fila, index) => {
         let filaComas = fila.join(',')
         lineArray.push(
            index == 0 ? "data:text/csv;charset=utf-8,"   filaComas: filaComas
         )
     })
     
    let csvContent = lineArray.join("\n");

The result is a string like data:text/csv;charset=utf-8,name1,2,3\nname2,4,5\nname3,6,7\nname4,8,9\nname5,10,11, then I need to send an email with MailApp of Google Apps Script that has this 'CSV' as an attachment for download.

How can I do this?

CodePudding user response:

Attach CSV File

Just add this to your code:

    let file = DriveApp.createFile('filename',csvContent);
    MailApp.sendEmail('recipient','subject','body',{attachments:[file]});
  • Related