簡體   English   中英

將線程中的所有消息從 gmail 提取到電子表格中

[英]Extract all messages in thread from gmail into a spreadsheet

我正在嘗試利用此代碼從我的收件箱中提取求職者數據並填充谷歌表格。 這很容易,但問題是應用程序首先由我們的網站插件處理,然后發送到我的收件箱。 這會將發件人更改為相同的地址。 谷歌將每個收件人添加到同一個線程,然后阻止代碼從每個到達的應用程序中提取信息。

目前,正在檢索每條消息的第一條。 我知道我需要實現一個循環來遍歷線程,但到目前為止我還沒有運氣。

此處記入我的原始代碼源

這是到目前為止的代碼:

這個 function 正在將過濾器、解析的內容傳送到活動表。

function gather() {
    var messages = getGmail();
    var curSheet = SpreadsheetApp.getActive();
    messages.forEach(message => {curSheet.appendRow(parseEmail(message))});
}

它在這些功能之一的某個地方我認為需要合並一個循環來收集線程中的所有消息

    function getGmail() {
        const query = "from:noreply-careers@smooth.ie ";
    
        let threads = GmailApp.search(query);
    
        //let label = GmailApp.getUserLabelByName("done");
        //if (!label) {label = GmailApp.createLabel("done")}
    
        let messages = [];
    
        threads.forEach(thread => {
            messages.push(thread.getMessages()[0].getPlainBody());
            
        });
    
        return messages;
    }

具體來說,我一直在關注這一點,以包括通過線程的循環和消息:

    threads.forEach(thread => {
        messages.push(thread.getMessages()[0].getPlainBody());

我確實嘗試過這樣的事情,但我無法讓它工作。

function getGmails_(query) {
    var emails = [];
    var threads = GmailApp.search(query);
    for (var i in threads) {
        var msgs = threads[i].getMessages();
        for (var j in msgs) {
            emails.push([msgs[j].getPlainBody().replace(/\n*\s.+:/,',')
            .replace(/\n*.+:/g,',')
            .replace(/^,/,'')
            .replace(/\n/g,'')
            .split(',')
          ]);
        
        }
    } 
  
    return result;
}

剩下的代碼正在運行一些正則表達式,以從消息正文中提取申請人的答案,並封裝在一個索引中以傳遞到工作表中的列。

function parseEmail(message){
    var parsed = message.replace(/,/g,'')
        .replace(/\n*.+:/g,',')
        .replace(/^,/,'')
        .replace(/\n/g,'')
        .split(',');

    var result = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22].map(index => parsed[index]);

    return result;
    
}

如果您只想檢索單個數組中所有線程中所有消息的普通正文,您可以這樣做:

  • 對於GmailThread.getMessages()中返回的每條消息,使用map返回一個數組,其中包含每條消息的相應純正文。
  • 使用push將當前線程消息普通正文添加到主messages數組。

代碼片段:

threads.forEach(thread => {
  const threadMessages = thread.getMessages().map(message=>message.getPlainBody());
  messages.push(...threadMessages);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM