如何检查工作表中是否存在电子邮件ID,以及在线程中转到下一个电子邮件ID?

时间:2018-04-17 16:31:03

标签: google-apps-script google-sheets gmail

首先关闭:JavaScript新手。我们非常感谢您提供的任何帮助/指导。

我有一个我正在Google表格中处理商业案例的应用脚本。我正在阅读一个带标签的电子邮件主题,并且正在从中获取附件。但是,这封电子邮件每天来自供应商4次。我不需要导入其中的每个文件,而只需处理我未在previousIDs中记录的电子邮件ID。

下面是我的代码的开头部分,我需要执行其余的操作,因为它正常工作。

function functionName() {

    // Google Sheet Record Details
    var ss = SpreadsheetApp.openById('GoogleSheetName');
    var initialSheet = ss.getSheetByName('ExportInitial');
    var downloadSheet = ss.getSheetByName('ExportForDownload');
    var historicalSheet = ss.getSheetByName('ExportHistorical');
    var historicalAttachments = ss.getSheetByName('HistoricalAttachmentNames');

    // GMail Details
    var gmailLabel = ('_Projects/Doc Cancel/New Requests');
    var label = GmailApp.getUserLabelByName(gmailLabel);
    var unreadCount = label.getUnreadCount();
    var gmailThreads = GmailApp.getUserLabelByName(gmailLabel).getThreads();
    var messages = GmailApp.getMessagesForThreads(gmailThreads);

    // Get Previous Email IDs to prevent duplicates
    var previousIDs = historicalAttachments.getRange("D2:D").getValues()
        .filter(function(row) {
            return row[0];
        })
        .sort();

    // Begin loop through Messages
    for (var i = 0; i < messages.length; i++) {

        // Begin loop through individual emails within Message
        for (var j = 0; j < messages[i].length; j++) {
            var attachments = messages[i][j].getAttachments();
            var emailDate = messages[i][j].getDate();
            var emailID = messages[i][j].getId();

            for (p in previousIDs) {
                if (emailID == previousIDs[p][0]) {
                    // stop this loop, and go to the next message[i]
                }
            }

            // if emailID does not exist in list of previousIDs
            if (attachments != '') {
            // rest of code goes here

2 个答案:

答案 0 :(得分:0)

尝试使用继续

Reference

    // Begin loop through Messages
    for (var i = 0; i < messages.length; i++) {

        // Begin loop through individual emails within Message
        for (var j = 0; j < messages[i].length; j++) {
            var attachments = messages[i][j].getAttachments();
            var emailDate = messages[i][j].getDate();
            var emailID = messages[i][j].getId();
  

从这里试试这个:

            var found=false;
            for (p in previousIDs) {
                if (emailID == previousIDs[p][0]) {
                    found=true;
                }
            }
            if(found){continue;}
  

到这里:

            // if emailID does not exist in list of previousIDs
            if (attachments != '') {
            // rest of code goes here

答案 1 :(得分:0)

我相信我有一个解决方案,但绝对欢迎任何见解。

以下代码似乎有效:

function functionName() {

    // Google Sheet Record Details
    var ss = SpreadsheetApp.openById('GoogleSheetName');
    var initialSheet = ss.getSheetByName('ExportInitial');
    var downloadSheet = ss.getSheetByName('ExportForDownload');
    var historicalSheet = ss.getSheetByName('ExportHistorical');
    var historicalAttachments = ss.getSheetByName('HistoricalAttachmentNames');

    // GMail Details
    var gmailLabel = ('_Projects/Doc Cancel/New Requests');
    var label = GmailApp.getUserLabelByName(gmailLabel);
    var unreadCount = label.getUnreadCount();
    var gmailThreads = GmailApp.getUserLabelByName(gmailLabel).getThreads();
    var messages = GmailApp.getMessagesForThreads(gmailThreads);

    // Get Previous Email IDs to prevent duplicates
    var previousIDs = historicalAttachments.getRange("D2:D").getValues()
        .filter(function (row) {
            return row[0];
        })
        .sort();

    // Begin loop through Messages
    for (var i = 0; i < messages.length; i++) {

        // Begin loop through individual emails within Message
        var j = 0;
        emailIDLoop:
            for (j; j < messages[i].length; j++) {
                var attachments = messages[i][j].getAttachments();
                var emailDate = messages[i][j].getDate();
                var emailID = messages[i][j].getId();

                for (var p = 0; p < previousIDs.length; p++) {
                    if (emailID == previousIDs[p][0]) {
                        continue emailIDLoop;
                    }
                }

                // if emailID does not exist in list of previousIDs
                if (attachments != '') {
                // rest of code goes here