Automating Google Sheets means writing JavaScript code in Apps Script to handle repetitive tasks like sending emails and logging data. You stop manually copying rows and start letting the spreadsheet run your operations automatically. This guide shows you exactly how to build these workflows.

Google Sheets interface displaying Apps Script editor code window
The Apps Script editor is your command center for building these automated workflows directly inside your spreadsheet.

Setting Up Your App Script Environment

First, ensure your spreadsheet structure is clean before coding. You need specific columns for client names, email addresses, and invoice totals. Go to the Extensions menu, select Apps Script, and create a new file. This is where all your automation logic lives. Keep your code modular for easier debugging.

  • Define Constants: Store sheet names and email subjects in variable declarations at the top of your script.
  • Use Strict Mode: Add "use strict"; to catch undefined variable errors early in your development cycle.
Professional invoice PDF attached to automated email in inbox view
Automated attachment generation ensures your clients receive formatted PDF invoices without manual file handling.

Sending Emails Via MailApp Service

MailApp is the native service for sending transactional emails directly from your script. You build a message object and call the send method. This eliminates the need for third-party API keys for basic outreach. It is crucial for follow-up sequences and client notifications. However, note that daily quota limits apply to your account tier.

  1. Create an email object with MailApp.newEmail().
  2. Set the recipient, subject, and body using chain methods.
  3. Call .send() to execute the delivery.
  4. Wrap the logic in a trigger function that loops through active rows.

Formatting HTML Email Templates

Plain text emails can look less polished for business operations. Use HTML formatting to insert your logo and structured data. Construct the body as a string of HTML tags. You can insert dynamic variables like the client name and invoice total directly into the HTML. This creates a personalized look without manual editing. Test your templates in a safe environment before going live. Consistency in styling helps build trust with your clients.

  • Inline Styles: Use style attributes for font sizes and colors to ensure compatibility.
  • Table Layouts: Use HTML tables for invoice line items to maintain alignment.

Attaching PDF Invoices Automatically

Generating a PDF from a sheet row requires a two-step process. First, save the specific data as a CSV or use a report generator. Then, convert that file to a PDF blob. Apps Script can attach this blob to the email. You must handle the file naming convention carefully to avoid duplicates. The attachment size should stay under the email service limits. This ensures the invoice arrives as a professional document.

  1. Retrieve the row data and format it into a printable HTML string.
  2. Convert the HTML to a PDF using the Sheets API or a library.
  3. Create a Blob object from the PDF bytes.
  4. Attach the Blob to the MailApp message using .addAttachment().

Logging Sent Timestamps in Cells

You must track which emails have been sent to prevent duplicates. Add a new column in your sheet called "Last Sent". After the email dispatches, write the current timestamp to that cell. This creates an audit trail for your operations team. Use Utilities.formatDate() to display readable times. This simple log helps you debug failed sends quickly. It also provides data for performance reporting.

  • Timestamp Format: Use ISO 8601 or a localized date format for clarity.
  • Error Handling: Log failures to a separate sheet so you can retry them later.
TaskApps Script MethodQuota LimitUse Case
Send Simple EmailMailApp.sendEmail()50/day (Free)Notifications
Attach FileMailApp.addAttachment()50/day (Free)Invoices
Generate PDFSheets APIAPI LimitsReports
Write TimestampSpreadsheetAppNoneLogging

Frequently Asked Questions

Can Apps Script run without a computer?

Yes, the scripts execute on Google servers. You set up triggers that run automatically on time or spreadsheet events. Your local computer does not need to be on for the automation to work.

How do I prevent duplicate emails?

Check the timestamp column before sending. If the last send was within 24 hours, skip that row. This logic stops the script from resending to the same client.

Do I need to pay for this?

Basic usage is free within Google's quota limits. You only pay for premium storage if you exceed standard Google Workspace limits. Many small business operations stay well within the free tier.