需要添加到此Google脚本以将电子邮件标记为已读的内容

时间:2017-07-21 21:31:12

标签: google-sheets google-docs google-apps google-email-settings-api

我正在寻找在执行此脚本后如何标记读取的消息。我已经阅读了Class GmailApp,但我不知道该怎么做。

如果根据搜索条件将电子邮件数据拉入Google工作表,然后将这些邮件标记为已读,则此脚本对我来说会很成功。目前,它只会将电子邮件拉入Google表格。

感谢您的时间。

var SEARCH_QUERY = "label:aggieworks is:unread to:me subject:*WO:*";

// Credit: https://gist.github.com/oshliaer/70e04a67f1f5fd96a708

function getEmails_(q) {
    var emails = [];
    var threads = GmailApp.search(q);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) {
            emails.push([msgs[j].getBody().replace(/<.*?>/g, '\n')
                .replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')
            ]);
        }
    }
    return emails;
}

function appendData_(sheet, array2d) {
    sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}

function saveEmails() {
    var array2d = getEmails_(SEARCH_QUERY);
    if (array2d) {
        appendData_(SpreadsheetApp.getActiveSheet(), array2d);
    }
}

3 个答案:

答案 0 :(得分:1)

假设该功能可以正常工作。这应该将消息标记为已读。

function getEmails_(q) 
{
    var emails = [];
    var threads = GmailApp.search(q);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) 
        {
            emails.push([msgs[j].getBody().replace(/<.*?>/g, '\n')
                .replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')]);
            msgs[j].markRead();   
        }
    }
    return emails;
}

答案 1 :(得分:0)

这是产生错误的更新代码。

  

TypeError:无法从undefined中读取属性“length”。 (第21行,   文件“GetEmailRecieved”)

var SEARCH_QUERY = "label:aggieworks is:unread";

// Credit: https://gist.github.com/oshliaer/70e04a67f1f5fd96a708


function getEmails_(q) {
    var emails = [];
    var threads = GmailApp.search(q);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) {
            emails.push([msgs[j].getBody().replace(/<.*?>/g, '\n')
                .replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')
            ]);
        }
    }
    return emails;
}

function appendData_(sheet, array2d) {
    sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}

function saveEmails() {
    var array2d = getEmails_(SEARCH_QUERY);
    if (array2d) {
        appendData_(SpreadsheetApp.getActiveSheet(), array2d);
    }
}

function getEmails_(q) 
{
    var emails = [];
    var threads = GmailApp.search(q);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) 
        {
            emails.push([msgs[j].getBody().replace(/<.*?>/g, '\n')
                .replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')]);
            msgs[j].markRead();   
        }
    }
    return emails;
}

答案 2 :(得分:0)

长度错误的原因是array2d为零。 appendData_函数失败。

array2d语句中执行appendData_函数之前,需要先评估if()的内容。

我还不知道会怎样