Skip to content

Instantly share code, notes, and snippets.

@fumikito
Created January 5, 2026 17:14
Show Gist options
  • Select an option

  • Save fumikito/cab6b27301e90ac73d21bb13b0de0ed7 to your computer and use it in GitHub Desktop.

Select an option

Save fumikito/cab6b27301e90ac73d21bb13b0de0ed7 to your computer and use it in GitHub Desktop.
Gmailの特定の条件にあうメールを定期的に既読にする
/**
* Gmailの特定の条件にあうメールを定期的に既読にする
*
* なんでもいいからスプレッドシートを作って、拡張機能 > App Scriptにこれをコピペしてください。
* で、「トリガー」から注意欠陥を設定すると、自動でお掃除してくれます。
* 私はこれで「プロモーション」のメールが30万件から100ぐらいになり、大変快適です。
* シート名は暫定で”archive-log”としています。
* 1日7000件ぐらいが限度らしいです。
*/
/**
* Gmailの受信トレイ以外のメールのうち、古いものを既読にする
*
*/
function markOldPromotionsAsRead() {
for ( const category of ['forums', 'promotions', 'social)'] ) {
markOldMailAsRead( category );
}
}
/**
* Gmailの指定したカテゴリーにあるメールのうち、古いものを既読にする
*
*/
function markOldMailAsRead(category, days=7, batchSize=200) {
const query = `category:${category} is:unread older_than:${days}d`;
const threads = GmailApp.search(query, 0, batchSize);
let done = 0;
let fail = 0;
threads.forEach( t => {
// todo: 失敗したらカウントアップしない
try {
t.markRead();
done++;
} catch ( e ) {
fail++;
}
});
// ログをスプレッドシートに残す。
//
logToSpreadSheet( 'archive-log', category, done, fail );
}
function logToSpreadSheet( sheetName, category, length, fail ) {
const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
sheet.appendRow([new Date(), category, length, fail ]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment