Introduction

Managing emails efficiently can be a game-changer in your daily workflow.

With Google Apps Script (GAS), you can automate the process of finding emails that contain specific keywords. Whether you’re tracking important messages or organising your inbox, this simple automation can save you time and effort.

This article will guide you through creating a script to search for emails with specific keywords using Generative AI (Gen AI) tools.

The sample codes provided in this article are just starting points to show you what is possible with GAS. We recommend that you dive deeper into the capabilities of GAS to fully leverage its potential and customise scripts to suit your specific needs.

If you are a complete beginner to Google Apps Script, we recommend to take a read on our Beginner’s Guide to Google Apps Script first before diving into this post.

Why Automate Email Searches?

Automating email searches can provide several benefits:

  • Efficiency: Quickly find important emails without manually sifting through your inbox.
  • Productivity: Focus on high-priority tasks by automating repetitive searches.
  • Accuracy: Ensure no critical email is missed by using precise keyword searches.
  • Convenience: Streamline your email management process with automated tools.

Steps to Automate Email Searches

1. Open Google Sheets:

2. Open Script Editor:

  • In the Google Sheets document, click on Extensions in the menu.
  • Select Apps Script. This will open the Apps Script editor in a new tab.

3. Create a New Project:

  • In the Apps Script editor, delete any existing code.
  • Copy and paste the following script into the code editor
Copy Script
Copy Script

4. Save the Project:

  • Give your project a relevant name, for example, “Find Emails With Keywords”.
  • Click on the floppy disk icon to save the project.

5. Refresh Google Sheets:

  • Close the Apps Script editor tab.
  • Refresh the Google Sheets document to see the new custom menu.

6. Run the Script from the Custom Menu:

  • In your Google Sheets document, you will see a new menu item called “Email Utilities” in the menu bar, refresh your Google Sheets document again if you don’t see it.
  • Click on Email Utilities and select Find Emails by Keyword.
  • Enter the keyword when prompted and confirm.
Find Email Utilities in Google Sheet by Refreshing the Page

7. View the Results:

  • The script will create a new sheet named “Email Search Results” (or clear the existing one) and save the search results there, including the subject, a snippet of the body (first 100 characters), and the date of each email.
  • If the search results exceed the limit, a warning message will be displayed indicating only the first set of results is shown.

Handling Errors with Gen AI

If you encounter errors while running the script, you can leverage Gen AI tools like ChatGPT to help troubleshoot and fix the issue.

  • Describe the Error: Provide a detailed description of the error you encountered.
  • Paste Error Code: Copy and paste the error code or message into your prompt.
  • Request a Fix: Ask the AI to help resolve the error, providing context and any relevant code snippets.

Example Prompt for Troubleshooting:

I encountered the following error while running my Google Apps Script: “TypeError: Cannot read property ‘getMessages’ of undefined”. Here is the relevant part of my code:

Copy Script
Copy Script

Please help me troubleshoot and fix this error.

Conclusion

Automating the search for emails with specific keywords using Google Apps Script can significantly enhance your email management efficiency.

By following the steps outlined in this article, you can set up your own automated email searches, ensuring you never miss important messages.

If you encounter any issues, Gen AI tools are there to help you troubleshoot and refine your scripts. Happy automating!

By Ali Reza Azmi

Founder & Consultant @ Twenty-Four Consulting

By Ali Reza Azmi

Founder & Consultant @ Twenty-Four Consulting

Automation can be Super EASY, and FREE.

With Google Apps Script & Gen AI, you don't need to be a coder to automate anymore!

Watch our demo, download our free cheat sheet, and join the waitlist today.

Google Apps Script Waitlist Image

Related Posts