4

What I am trying to do, is get the contents of the CSV attachment to output onto Google Sheets. No idea if I am doing this correctly, but I found this online, ran it, and then nothing happens.

function importCSVFromGmail() {
    var threads = GmailApp.search("from:cbuffone123@gmail.com");
    var message = threads[0].getMessages()[0];
    var attachment = message.getAttachments()[0];

    // Is the attachment a CSV file
    if (attachment.getContentType() === "text/csv") {
        var sheet = SpreadsheetApp.getActiveSheet();
        var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");

        // Remember to clear the content of the sheet before importing new data
        sheet.clearContents().clearFormats();
        sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
    }
}
tehhowch
  • 9,645
  • 4
  • 24
  • 42
Carlo B.
  • 119
  • 2
  • 16

1 Answers1

2

In my environment, I confirmed that when the attached *.csv file is retrieved from the email, the mimeType is retrieved as application/vnd.ms-excel. I think that the reason of "nothing happens" is this. So how about these modifications? I think that there are several workarounds for your situation. So please think of these modification as 3 of them.

Modification points:

  • Pattern 1: Set the mimeType from filename.
  • Pattern 2: Use the mimeType as application/vnd.ms-excel
  • Pattern 3: Use the extension.

Pattern 1:

Please add the following script before if (attachment.getContentType() === "text/csv") {.

attachment.setContentTypeFromExtension();

Pattern 2:

Please modify as follows.

From:
if (attachment.getContentType() === "text/csv") {
To:
if (attachment.getContentType() === "application/vnd.ms-excel") {

Pattern 3:

Please modify as follows.

From:
if (attachment.getContentType() === "text/csv") {
To:
var fileName = attachment.getName().toUpperCase().split(".");
if (fileName.length > 1 && fileName[1] == "CSV") {

Note:

  • About pattern 1, the mimeType is set from the extension of the filename by using setContentTypeFromExtension().
  • About pattern 2, if in your environment, CSV files are retrieved as other mimeType, please modify application/vnd.ms-excel.
  • About pattern 1 and 3, if the filename of CSV file has not extension or other extension, it cannot be used under the situation.

Reference:

If these were not what you want, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • hey thanks so much! looks like pattern 1 seemed to work. i didnt check the others, so not sure if they worked also. In terms of functionality, if have this dedicated emails receiving daily CSV files, and the script runs daily, what is sent to the google sheet? is it the most recent email attachment? does it merge them? thanks! – Carlo B. Sep 25 '18 at 18:59
  • @Carlo B. Thank you for replying. I'm glad your issue was solved. About your another question, ``threads[0]`` and ``getMessages()[0]`` mean that the latest thread and the latest message in the latest thread from ``from:cbuffone123@gmail.com``. ``getAttachments()[0]`` means that 1st file of the attachment files. If only one CSV file is included in the latest mail, ``getAttachments()[0]`` is the CSV file. About merging, if you want to merge CSV data to Spreadsheet, please remove ``clearContents()`` and modify ``range``. Is my understanding for your reply correct? – Tanaike Sep 25 '18 at 22:00
  • @Carlo B. Thank you for your response. – Tanaike Sep 27 '18 at 23:16
  • this has been working great. Is it possible to have to have this app script only load a specific file attachment by name? The goal is to have 2 different file names sent to this account; app script A would pull fhe first tile, app script B the second file? – Carlo B. Oct 23 '18 at 21:14
  • @Carlo B. I cannot understand what you want to do from your comment. So can you post it as a new question including the detail information? It will help users including me think of your solution. If my English skill was high, I might have been able to resolve your issue soon. I'm really sorry for my poor English skill. – Tanaike Oct 23 '18 at 21:51
  • apologies, let me attempt to re-phrase. The function at the beginning of the thread, 'importCSVFromGmail' imports an attachment from email: cbuffone123@gmail.com and places that attachment into a google sheets. The function takes the LAST attachment, of any name, sent to that email, and attaches it to google sheets. this is BAD for me because i occasionally get OTHER attachments sent to my email, which i DONT want imported to google sheets. i want only the specific attachment named "data.csv" to import, all other attachments to be ignored. – Carlo B. Oct 25 '18 at 19:13
  • I'd like to edit the function to import the last attachment named "data.csv" into google sheets & ignore all other attachments. I dont know how to do this. – Carlo B. Oct 25 '18 at 19:23
  • @Carlo B. You want to retrieve the attachment file with the filename of ``data.csv`` from each email. Is my understanding correct? But I cannot understand about ``the last attachment named "data.csv"``. I'm sorry. – Tanaike Oct 25 '18 at 22:00
  • Every day a file named "data.csv" is sent to cbuffone123@gmail.com. The inbox therefore contains hundreds of attachments named "data.csv". I only want to grab the most recent attachment named "data.csv" not the old stuff in the inbox. – Carlo B. Oct 26 '18 at 13:42
  • @Carlo B. You want to retrieve the attachment file (data.csv) of the latest email. Is my understanding correct? And also can you post it as new question? – Tanaike Oct 26 '18 at 23:03
  • I have re submitted as a new question – Carlo B. Oct 30 '18 at 04:56
  • @Carlo B. Thank you for your response. – Tanaike Oct 30 '18 at 10:56