惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
小众软件
小众软件
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
L
LangChain Blog
博客园 - 司徒正美
腾讯CDC
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
S
Schneier on Security
T
Tailwind CSS Blog
S
Securelist
P
Proofpoint News Feed
A
Arctic Wolf
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Privacy & Cybersecurity Law Blog
爱范儿
爱范儿
G
GRAHAM CLULEY
F
Full Disclosure
T
Threat Research - Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
T
Tor Project blog
T
Threatpost
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
Simon Willison's Weblog
Simon Willison's Weblog
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网
I
Intezer
GbyAI
GbyAI
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
J
Java Code Geeks
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
罗磊的独立博客

Colorful

《精读源码》- GoT Javascript Sandbox 从编程范式及实时 Web 应用的另类角度看向 ChatGPT 的 AI 大脑 面向未来的认知优化之《爆裂》启示录 Make your own Dark Mode with mbm.js v-circle - 漂亮的 Vue.js 圆形进度组件集合 KOA 源码阅读系列(一) - 理解 KOA 中间件的执行 前端工程师的工具包 Github 上那些炙手可热的技术手册
How to clean email for gmail via app script ?
Archer · 2022-12-29 · via Colorful

分享一枚非常实用的 google app script

2 分钟阅读

分享一枚非常实用的 google app script,需要的同学可以将其安装于 https://script.google.com/; 它支持全自动调度清理任务,清理任务主要根据 gmail 中的指定搜索条件。

Feature

  • 指定 gmail 搜索条件
  • 错误 / 边界条件等自动触发新调度
  • trigger 唯一

Content of App Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//search string to delete
var SEARCH_STRING = "category:forums";

//tigger function name
var TRIGGER_NAME = "cleanWithScheduler";

//first time job delay (min)
var FIRST_TIME_DELAY_MIN = 1;

// FREQUENCY of scheduler (min)
var RESUME_FREQUENCY = 10;

//batch size
var BATCH_SIZE = 500;

//intialize
function intialize() {
  return;
}

//install
function install() {
  ScriptApp.newTrigger(TRIGGER_NAME)
    .timeBased()
    .at(new Date(new Date().getTime() + 1000 * 60 * FIRST_TIME_DELAY_MIN))
    .create();
}

//clean triggers
function cleanTriggers() {
  var triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }
}

//clean immediately
function cleanImmediately() {
  var NOW = new Date();
  Logger.log("SEARCH: " + SEARCH_STRING + " BATCH_SIZE: " + BATCH_SIZE);

  try {
    var threads = GmailApp.search(SEARCH_STRING, 0, BATCH_SIZE);

    Logger.log("Processing " + threads.length + " threads...");
    for (var i = 0; i < threads.length; i++) {
      var thread = threads[i];
      var lastMessageDate = thread.getLastMessageDate();

      if (lastMessageDate < NOW) {
        Logger.log("lastMessageDate " + lastMessageDate);
        thread.moveToTrash();
      } else {
        var messages = GmailApp.getMessagesForThread(threads[i]);
        for (var j = 0; j < messages.length; j++) {
          var email = messages[j];
          if (email.getDate() < NOW) {
            Logger.log("Start to move this mail to trash.");
            email.moveToTrash();
            Logger.log("Mail has been moved to trash.");
          }
        }
      }
    }
  } catch (e) {
    Logger.log("error: " + e.message);
  }
}

//schedule new job
function scheduleNewJob() {
  //before new job, we clear all triggers first
  Logger.log("clean triggers...");
  cleanTriggers();

  Logger.log("Scheduling one new job...");
  ScriptApp.newTrigger(TRIGGER_NAME)
    .timeBased()
    .at(new Date(new Date().getTime() + 1000 * 60 * RESUME_FREQUENCY))
    .create();
}

//clean with scheduler
function cleanWithScheduler() {
  var NOW = new Date();
  Logger.log("SEARCH: " + SEARCH_STRING + " BATCH_SIZE: " + BATCH_SIZE);

  try {
    var threads = GmailApp.search(SEARCH_STRING, 0, BATCH_SIZE);

    if (threads.length == BATCH_SIZE) {
      scheduleNewJob();
    }

    Logger.log("Processing " + threads.length + " threads...");
    for (var i = 0; i < threads.length; i++) {
      var thread = threads[i];
      var lastMessageDate = thread.getLastMessageDate();

      if (lastMessageDate < NOW) {
        Logger.log("lastMessageDate " + lastMessageDate);
        thread.moveToTrash();
      } else {
        var messages = GmailApp.getMessagesForThread(threads[i]);
        for (var j = 0; j < messages.length; j++) {
          var email = messages[j];
          if (email.getDate() < NOW) {
            Logger.log("Start to move this mail to trash.");
            email.moveToTrash();
            Logger.log("Mail has been moved to trash.");
          }
        }
      }
    }
  } catch (e) {
    Logger.log("error: " + e.message);
    scheduleNewJob();
  }
}