Gmail AutomationDifficulty: Beginner⚡ Hourly Trigger

Auto-Label & Archive Financial Receipts in Gmail

Scans inbox for emails from Stripe, AWS, Google Cloud, and PayPal, applies a "Tax / Receipts" label, and archives them out of your primary inbox.

Required OAuth Scopes: https://www.googleapis.com/auth/gmail.modify
Code.gs (Apps Script V8 Runtime)
function autoLabelReceipts() {
  const query = 'from:(stripe.com OR aws.amazon.com OR google.com OR paypal.com) subject:(receipt OR invoice OR statement) is:inbox';
  const label = GmailApp.getUserLabelByName('Receipts') || GmailApp.createLabel('Receipts');
  const threads = GmailApp.search(query, 0, 50);
  
  threads.forEach(thread => {
    thread.addLabel(label);
    thread.moveToArchive();
  });
}

Line-by-Line Code Explanation

Line 2: Searches incoming billing emails from common SaaS and payment providers.
Line 3: Resolves or automatically creates the "Receipts" label.
Line 6-9: Applies label and removes from primary inbox view.

Step-by-Step Setup & Deployment

  1. Paste into Apps Script and set an hourly trigger.

Best Practices for Quota & Rate Limit Management

When deploying this automation in Google Workspace, adhere to these operational principles:

  • Batch Reads & Writes: Never execute getValue() or setValue() inside loops. Read ranges into memory in 1 call, process arrays in V8 memory, and commit changes with a single setValues() call.
  • Execution Timeouts: Apps Script terminates scripts exceeding 6 minutes. For multi-thousand row datasets, implement batch pagination with resume timestamps stored in PropertiesService.
  • OAuth Permissions: Scope your manifest (appsscript.json) narrowly to prevent security prompts from alarming external team members.

Frequently Asked Questions

Is Google Apps Script completely free to use?

Yes. Google Apps Script is built into all free Google accounts and Google Workspace editions. There are no server fees or subscription costs for running scripts within daily quotas.

Can I run this script on an automated schedule without keeping my computer on?

Yes. Time-driven triggers execute on Google's cloud infrastructure. Your script will run reliably even when your computer is closed or offline.

More Gmail Automation Snippets