jdabramson
jdabramson

Reputation: 1

What needs to be added to this google script to mark the emails as read

I'm looking to see how to mark the messages read after executing this script. I've read through Class GmailApp, but I am too much of a novice to know what to do.

This script would be successful for me if it pulled the email data into google sheets based on the search criteria and then marked those messages as read. Currently, it will only pull the emails into google sheets.

Thank you for your time.

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

Upvotes: 0

Views: 1492

Answers (3)

user10261544
user10261544

Reputation: 1

The reason for the length error is that array2d is zero. The appendData_ function fails.

The array2d contents need to be evaluated before the appendData_ function is executed in an if() statement.

I do not know yet how that will look

Upvotes: 0

jdabramson
jdabramson

Reputation: 1

Here's the updated code that produces the error.

TypeError: Cannot read property "length" from undefined. (line 21, file "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;
}

Upvotes: 0

Cooper
Cooper

Reputation: 64032

Assuming that the function works as you have it. This should mark the messages as read.

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

Upvotes: 1

Related Questions