简体   繁体   中英

Import CSV by name into google sheets

I have been using the following function to load CSV attachments into google sheets:

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);
}
}

the function currently gets ANY attachment sent from that email. I would like to alter the function so that it ONLY select attachments named "data.CSV" and ignores all other attachments. Is there a way to do this?

thank you in advance!

so i have modified this to :

function Exact_CSV_Import() {
    var threads = GmailApp.search("from:cbuffone123@gmail.com");
    var message = threads[0].getMessages()[0];
    var attachment = message.getAttachments()[0];  
    for (var n = 0; n < attachment.length; n++) {
var attachmentName = attachments[n].getName();
if (attachmentName = "data1.csv")  
{
    // do stuff with "data1.csv"        
     attachment.setContentTypeFromExtension();  
  if (attachment.getContentType() === "text/csv") {
    var sheet = SpreadsheetApp.getActiveSheet();
    var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
    sheet.clearContents().clearFormats();
    sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
}

}
}

and nothing seems to happen. i dont get any errors, but it doesnt seem to be doing anything?

This statement:

var attachment = message.getAttachments()[0];

only looks at the first attachment in the array of attachments returned by .getAttachments()

Instead use var attachments = message.getAttachments();

Then you can loop through the attachments and pick the one you want.

This loop will print out the names of the attachments and their size;

for (var n = 0; n < attachments.length; n++) {
  Logger.log( attachments[n].getName(), attachments[n].getSize());
}

You should be able to proceed from there...

EDIT: Some more clarification to selectively process different CSV files as requested..

I'm pretty sure attachments is not an associative array so you cant do something like attachments["csv"]. But you can iterate through the list of attachments and pick out the ones with names that you want and ignore the rest. See below for some code...

for (var n = 0; n < attachments.length; n++) {

    var attachmentName = attachments[n].getName();

    if (attachmentName = "data1.csv")  
    {
        // do stuff with "data1.csv"
    }
    else
    if ((attachmentName = "data2.csv")
    { 
        // do stuff with "data1.csv"
    }
    else 
    {   
        // do nothing - just skip it
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM