Skip to content

Instantly share code, notes, and snippets.

@watahani
Last active February 23, 2018 04:00
Show Gist options
  • Save watahani/050855cd41e0d919c8890fed5d9dc70b to your computer and use it in GitHub Desktop.
Save watahani/050855cd41e0d919c8890fed5d9dc70b to your computer and use it in GitHub Desktop.
mail labeled move to archive for gmail
var labels = PropertiesService.getScriptProperties().getProperty('GMAIL_ARCHIVE_LABELS');
function mail_to_archive() {
var labellist = labels.split(',');
labellist.forEach(
function to_archive(value) {
if (value) {
var label = GmailApp.getUserLabelByName(value);
if (label == null) {
GmailApp.createLabel(value);
}
else {
var delayDays = 2 // Enter # of days before messages are moved to archive
var maxDate = new Date();
maxDate.setDate(maxDate.getDate() - delayDays);
var keyword = 'label:' + value + ' label:inbox'
var threads = GmailApp.search(keyword);
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate() < maxDate && !threads[i].isUnread()) {
threads[i].moveToArchive();
}
Utilities.sleep(1000);//呼び出し回数超過のために1秒待機
}
}
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment