You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This document provides instructions for Gemini on how to work with this project.
4
+
5
+
## Project Overview
6
+
7
+
This is a Google Apps Script project for cleaning old emails in Gmail based on regular expressions.
8
+
9
+
-**Source Code**: The main logic is in `src/code.js`. Usage examples are in `src/examples.js`.
10
+
-**Management**: The project is managed using `@google/clasp`. See `package.json` for commands.
11
+
-**Language**: The project is written in JavaScript.
12
+
13
+
## Development Guidelines
14
+
15
+
1.**Caution with Deletion Logic**: The script can permanently delete emails. Any changes to the core logic in `src/code.js` must be tested carefully. When running tests or new example functions, always default to `isDryRun = true`.
16
+
2.**Testing**: This project uses [Jest](https://jestjs.io/) for automated testing. Run `npm test` to execute the test suite.
17
+
3.**Code Style**: The project is configured with ESLint and Prettier for code quality and consistent formatting. Run `npm run lint` to check for any linting issues. Please adhere to the existing code style.
18
+
4.**TypeScript Migration**: The project is a good candidate for migration to TypeScript for improved type safety. If significant changes are made, consider proposing this migration.
19
+
5.**Commit Messages**: Use the GitMoji convention for commit messages (e.g., `✨ feat: ...`, `🐛 fix: ...`).
@@ -12,48 +12,127 @@ This is a Google Apps Script project that helps you delete old emails in Gmail t
12
12
13
13
## How to use
14
14
15
-
1. Create a new Google Apps Script project in Google Drive
16
-
2. Copy and paste the code from `src/code.js` into the script editor
17
-
3. Edit the variables at the top of the script to suit your needs
18
-
4. Create a new void function to set the variables and call the `main()` function in the code.js file. For example:
15
+
1. Create a new Google Apps Script project in Google Drive.
16
+
2. Copy and paste the code from `src/code.js` and `src/examples.js` into the script editor.
17
+
3. From the `examples.js` file, choose a function that matches your needs, or create a new one. You can then run this function from the Apps Script editor.
18
+
19
+
For example, to run one of the pre-made functions, you would select it in the editor's function list and click **Run**.
20
+
21
+
The `main` function, which does the core work, has been updated to be more flexible. Here is how you would call it inside a custom function:
Your code to extract and format the date part to a date string
36
+
that can be parsed by the Date constructor.
37
+
*/
38
+
// This is just an example, a real implementation is needed here.
39
+
returnnewDate().toISOString();
40
+
}
41
+
});
31
42
}
32
43
```
33
44
34
45
> [!NOTE]
35
-
> `query`: A string that specifies the search criteria for the emails you want to delete. It should follow the same syntax as the Gmail search box. For example, `from:(Google Alerts <googlealerts-noreply@google.com>)` will match all emails from Google Alerts. You can find more examples and tips on how to use the Gmail search operators [here](https://developers.google.com/codelabs/apps-script-fundamentals-1).
46
+
> **`query`**: A string that specifies the search criteria for the emails you want to delete. It should follow the same syntax as the Gmail search box. For example, `from:(Google Alerts <googlealerts-noreply@google.com>)` will match all emails from Google Alerts. You can find more examples and tips on how to use the Gmail search operators [here](https://developers.google.com/codelabs/apps-script-fundamentals-1).
36
47
>
37
-
> `regex`: A regular expression that matches the date part of the email subject or body. It should be enclosed in slashes and follow the JavaScript regex syntax. For example, `/[0-9]{1,2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/m` will match a date like “8 Feb” or “23 Nov” in the email.
48
+
> **`regex`**: A regular expression that matches the date part of the email subject or body. It should be enclosed in slashes and follow the JavaScript regex syntax. For example, `/[0-9]{1,2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/m` will match a date like “8 Feb” or “23 Nov” in the email.
38
49
>
39
-
> `dryRun`: Optional. A boolean value that indicates whether to run the script in test mode or not. If set to `true`, the script will only log the emails that match the query and the regex, but will not delete them. If set to `false`, the script will delete the emails permanently. It is recommended to run the script with dryRun set to true first to make sure it works as expected.
50
+
> The third parameter to `main`is now an **options object**with the following optional properties:
40
51
>
41
-
> `dateFormatter`: Optional. A function that takes two parameters: `textWithDate` and `lastMessageDate`. The `textWithDate` is a string that contains the date part extracted from the email body. The `lastMessageDate` is a date object that represents the latest date of the email thread. The function should return a date string like `yyyy-MM-dd`. For example, if the `textWithDate` is “8 Feb” and the `lastMessageDate` is “2021-02-10”, the function should return a date object for “2021-02-08”.
52
+
> -**`tresholdInDays`**: The number of days to keep emails. Defaults to `60`.
53
+
> -**`isDryRun`**: A boolean value that indicates whether to run the script in test mode or not. If `true` (the default), the script will only log the emails that match the query and the regex, but will not delete them. If `false`, the script will delete the emails permanently. It is recommended to run the script with `isDryRun` set to `true` first to make sure it works as expected.
54
+
> -**`mode`**: A string that specifies whether to process the email body as plain text or HTML. Can be either `'plain'` (default) or `'html'`. If set to `'html'`, the script will strip all HTML tags from the email body before searching for the regex pattern.
55
+
> -**`dateFormatter`**: A function that takes two parameters: `textWithDate` and `lastMessageDate`. The `textWithDate` is a string that contains the date part extracted from the email body. The `lastMessageDate` is a date object that represents the latest date of the email thread. The function should return a date string like `yyyy-MM-dd` that can be parsed by `new Date()`.
42
56
43
-
5. Save and run the script. You can use the Run menu or the Run button in the toolbar.
57
+
4. Save and run the desired function from the script editor. You can use the **Run** menu or the **Run** button in the toolbar.
44
58
45
59
> [!IMPORTANT]
46
60
> When running the script for the first time, you may need to authorize it to access your Gmail account.
47
61
48
-
6. Optionally, set up a trigger to run the script periodically.You can do this by clicking the Triggers icon in the left sidebar, then clicking the Add a trigger button, and choosing the options you want. For example, you can set the script to run every day, week, or month.
62
+
5. Optionally, set up a trigger to run a function periodically.You can do this by clicking the **Triggers** icon in the left sidebar, then clicking the **Add a trigger** button, and choosing the options you want. For example, you can set the script to run every day, week, or month.
49
63
50
64
> [!WARNING]
51
-
> This script will delete your emails permanently, without moving them to the trash. Please use it with caution and make sure you have a backup of your important emails. You can run the script with the dryRun option set to true first to see what emails will be deleted.
65
+
> This script will delete your emails permanently, without moving them to the trash. Please use it with caution and make sure you have a backup of your important emails. You can run the script with the `isDryRun` option set to `true` first to see what emails will be deleted.
66
+
67
+
## Development
68
+
69
+
This project uses [ESLint](https://eslint.org/) for code linting and [Prettier](https://prettier.io/) for code formatting. It is recommended to run the linter before committing any changes.
70
+
71
+
To run the linter, use the following command:
72
+
73
+
```bash
74
+
npm run lint
75
+
```
76
+
77
+
This project uses [Jest](https://jestjs.io/) for automated testing.
78
+
79
+
To run the tests, use the following command:
80
+
81
+
```bash
82
+
npm test
83
+
```
84
+
85
+
### Build Process
86
+
87
+
This project uses [Rollup](https://rollupjs.org/) to bundle the source files from `src/` into the `dist/` directory, which is ready for deployment with `@google/clasp`.
88
+
89
+
To build the project, run:
90
+
91
+
```bash
92
+
npm run build
93
+
```
94
+
95
+
This command bundles `src/code.js` and processes `src/examples.js` into the `dist/` directory. It also removes a Node.js-specific code block from `code.js` that is used for testing with Jest.
96
+
97
+
#### Excluding Example Functions
98
+
99
+
You can prevent specific functions from `src/examples.js` from being included in the final `dist/examples.js` file. This is useful if you only need a subset of the example functions for your deployment.
100
+
101
+
To exclude functions, add their names to the `config.examples.exclude` array in your `package.json` file:
102
+
103
+
```json
104
+
{
105
+
"name": "gmail-regex-cleaner-apps-script",
106
+
"version": "1.0.0",
107
+
// ...
108
+
"config": {
109
+
"examples": {
110
+
"exclude": [
111
+
"removeAffiliatesOne",
112
+
"removeCorelAffiliateInfo"
113
+
]
114
+
}
115
+
},
116
+
// ...
117
+
}
118
+
```
119
+
120
+
When you run `npm run build`, the functions listed in the `exclude` array will be omitted from the output.
121
+
122
+
#### Timezone Update
123
+
124
+
Optionally, you can update the timezone in the `dist/appsscript.json` manifest to match your local machine's timezone. This is useful to ensure that scheduled triggers run at the correct time.
125
+
126
+
To update the timezone, run the following command **after** the build command:
127
+
128
+
```bash
129
+
npm run update-timezone
130
+
```
52
131
53
132
## Disclaimer
54
133
55
134
This script is provided as is, without any warranty or liability. Use it at your own risk. Make sure to test the script before using it on your Gmail account. The script may delete emails that you want to keep, or fail to delete emails that you want to remove. The script may also exceed the quota limits of Google Apps Script or Gmail API, resulting in errors or partial execution. The author is not responsible for any loss or damage caused by the use of this script.
56
135
57
136
## License
58
137
59
-
This project is distributed under the AGPL-3.0 license. You can use, modify, and distribute this project, as long as you comply with the terms and conditions in the [LICENSE](/LICENSE) file.
138
+
This project is distributed under the AGPL-3.0 license. You can use, modify, and distribute this project, as long as you comply with the terms and conditions in the [LICENSE](/LICENSE) file.
0 commit comments