简体   繁体   English

通过名称将CSV导入Google表格

[英]Import CSV by name into google sheets

I have been using the following function to load CSV attachments into google sheets: 我一直在使用以下功能将CSV附件加载到Google表格中:

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. 我想更改功能,使其仅选择名为“ data.CSV”的附件,并忽略所有其他附件。 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() 只查看.getAttachments()返回的附件数组中的第一个附件

Instead use var attachments = message.getAttachments(); 而是使用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.. 编辑:更多澄清以根据请求选择性地处理不同的CSV文件。

I'm pretty sure attachments is not an associative array so you cant do something like attachments["csv"]. 我很确定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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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