Last active
June 23, 2021 16:50
-
-
Save 1cadumagalhaes/33aa1d888e2fab9843e2abe278be5409 to your computer and use it in GitHub Desktop.
List emails using apps script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates and runs a search query on gmail. | |
* @param {Object} [$0] - params. | |
* @param {String} [$0.from=null] - email sender. | |
* @param {String} [$0.to=null] - email receiver. | |
* @param {String} [$0.label=null] - email label. | |
* @param {String} [$0.period="id"] - period to search for. | |
* @returns {{Assunto: "Subject of the thread", Emails: "Quantity of emails on the thread", lastMessageDate: "Date of the last message", lastMessageSender: "Contact that sent the last message on the thread"}} | |
*/ | |
function searchEmails({from = null, to = null, label=null, period='1d'}){ | |
let query = `{ | |
${from?`from:(${from})`:''} | |
${to?`to:(${to})`:''} | |
${label?`label:${label.replace(/([^\w\[\]])/g,"-")}`:''} | |
} newer_than:${period}`; | |
let getThread = (thread) => { | |
let count = thread.getMessageCount(); | |
return { | |
Assunto: thread.getFirstMessageSubject(), | |
Emails: count, | |
lastMessageDate: thread.getLastMessageDate(), | |
lastMessageSender: thread.getMessages()[count-1].getFrom() | |
} | |
}; | |
let threads = GmailApp.search(query); | |
console.log(threads.length) | |
let emails = threads.map(getThread); | |
return emails; | |
} | |
function main(){ | |
let emails = searchEmails({ | |
label: "[Strike]/Banco Carrefour", | |
period: "7d" | |
})/**/ | |
console.log(emails); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment