Gmail filters help you automatically sort email messages based on rules. So if your boss has sent an email message, the filter can mark it as important. If the email has the word “Unsubscribe” somewhere in the message body, it can be marked as a promotional message and so on.

How to Create Advanced Gmail Filters

While the built-in Gmail filters are powerful, they do have certain limitations.

For instance, you cannot have a Gmail filter that does case-sensitive search. It will treat WHO and who as same. Gmail filters won’t do pattern matching (regular expressions) so you cannot have a filter for messages that contain phone numbers.

We often get spam messages that have a few dozen addresses in the TO and CC fields but there’s no filter to automatically redirect such messages to the SPAM folder of Gmail. That’s where Google Scripts can help. You can setup advanced filters that aren’t available in the native version of Gmail.

What you see above is a set of 10 Gmail filters that were created with Apps Script. You can have a filter to process messages that contain tons of links. Or messages that have too many attachments. Or messages that have just a word or two in the message body.

The best part is that you don’t have to know scripting to use either of these filters. Just follow these 3 easy steps:

Click here to copy the Gmail Filters sheet into your Google Drive. You can write OFF to deactivate any of the available rules.

Go to the Gmail Filters menu in the sheet, choose Initialize and grant the necessary permissions to the script.

Now choose Turn-on Gmail Filter to activate your filters. You may close the Google sheet now.

Here’s what happens behind the scenes. The script will run in the background every 10 minutes and monitor any new unread messages in your Gmail inbox. It will then run the various rules against these messages. The native filters in Gmail take precedence and then your custom rules specified in the sheet are applied.

You can also look at the source code to understand how the various rules were created.