Last active
June 7, 2023 00:12
-
-
Save bikramkawan/44806c50fe2bea703123217e6dfb9753 to your computer and use it in GitHub Desktop.
Automate Gmail with Google Apps Script to archive emails from custom label.
This file contains hidden or 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
function archieveReadMail() { | |
const labels = ['Support', 'Notifications']; | |
for (const label of labels) { | |
const getUserLabelByName = GmailApp.getUserLabelByName(label); | |
const threads = getUserLabelByName.getThreads(); | |
const hasThreads = Array.isArray(threads) && threads.length > 0; | |
if (hasThreads) { | |
threads.forEach(thread => { | |
if (!thread.isUnread()) { | |
GmailApp.moveThreadToArchive(thread) | |
getUserLabelByName.removeFromThread(thread); | |
} | |
}) | |
} | |
} | |
} | |
// Based on https://medium.com/@bikramkawan/86526aae2429 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment