Apps Script example · 11 min read

Flag Expiring Training Certificates: Copy-Paste Apps Script Pattern

Working flag expiring training certificates example in Apps Script—copy-paste code, common mistakes, and when to get it built professionally.

MailAppTime-drivenTraining

Find training certificates expiring inside a configurable window and email managers a digest. The workbook becomes the system of record; Apps Script owns the joins and business rules so analysts are not pasting monthly formulas.

Entry point `flagExpiringCerts` reads typed columns with SpreadsheetApp batch APIs. MailApp is the primary service; Time-driven describes how you usually invoke it after authorization.

Keep thresholds (grace days, bands, alert emails, API keys) in Config cells or Script Properties so the same .gs file promotes from sandbox to production without code edits.

This page is technical only: sheet layout, edge cases, runnable code, deploy steps, and FAQs for flag expiring training certificates. No consulting pitch—just the pattern you can paste and harden.

Need this built? Hire a Google Apps Script developer →

Sheet / project setup

TabColumnsNotes
CertificationsEmpId, Course, Issued, ExpiresSource
EmployeesEmpId, Name, ManagerEmailLookup

What this script does

flagExpiringCerts() find training certificates expiring inside a configurable window and email managers a digest.

  • EXPIRY_DAYS property
  • Manager digest email
  • Skips already expired

Prerequisites

Container-bound script on the workbook that contains the tabs in the setup table. Authorize MailApp scopes on first run.

  • V8 runtime
  • Exact sheet names
  • Script timezone = ops timezone

Walkthrough

Scan Certifications for expiry in window, group by manager email from Employees, send one MailApp digest per manager.

Paste the code, set Config/Properties, run flagExpiringCerts once, verify the output tab, then attach a Time-driven trigger.

Edge cases

Already-expired certs are excluded—only future expiries inside the window are mailed.

Testing

Use a sandbox copy with 3–5 known rows. Compute expected outputs for flag expiring training certificates offline, run flagExpiringCerts, and diff the output tab including one intentional bad row.

Hardening

Add LockService around multi-sheet writes if triggers can overlap. Log run timestamps. Keep API keys and alert emails in PropertiesService—not in shared cells.

Variations

Fork ideas: filter to one business unit, change grain (daily→weekly), or POST a summary payload after flagExpiringCerts succeeds.

Operations notes

Assign an owner for the output tab, document regenerate steps, and treat `flagExpiringCerts` as source of truth over ad-hoc cell formulas.

Full code: flagExpiringCerts()

Run flagExpiringCerts when source tabs are current. Edit sheet names and Config/Properties first.

function flagExpiringCerts(){
  const days=Number(PropertiesService.getScriptProperties().getProperty('EXPIRY_DAYS')||30);
  const ss=SpreadsheetApp.getActive();
  const tz=Session.getScriptTimeZone();
  const today=new Date();
  const cutoff=new Date(today.getTime()+days*86400000);
  const rows=ss.getSheetByName('Certifications').getDataRange().getValues().slice(1);
  const mgr={};
  ss.getSheetByName('Employees').getDataRange().getValues().slice(1).forEach(r=>{
    mgr[String(r[0])]={name:r[1], managerEmail:r[2]};
  });
  const byMgr={};
  rows.forEach(r=>{
    const exp=new Date(r[3]);
    if(exp<today||exp>cutoff) return;
    const emp=mgr[String(r[0])]||{name:r[0],managerEmail:PropertiesService.getScriptProperties().getProperty('DEFAULT_MGR')};
    const key=emp.managerEmail||'ops@example.com';
    if(!byMgr[key]) byMgr[key]=[];
    byMgr[key].push(emp.name+' — '+r[1]+' expires '+Utilities.formatDate(exp,tz,'yyyy-MM-dd'));
  });
  Object.keys(byMgr).forEach(email=>{
    MailApp.sendEmail(email,'Training certs expiring in '+days+' days', byMgr[email].join('\\n'));
  });
}
  1. Line 1: Entry point — bind triggers to flagExpiringCerts.
  2. Line 2: Script Properties for thresholds/secrets.
  3. Line 7: Batch read; avoid per-cell getValue in loops.
  4. Line 16: Script Properties for thresholds/secrets.
  5. Line 22: Digest email; authorize mail scopes once.

Deploy this example

  1. 01

    Open Apps Script

    In the bound spreadsheet: Extensions → Apps Script. For standalone projects, create one at script.google.com and link your Sheet by ID.

  2. 02

    Paste and save

    Add a .gs file, paste the code below, rename constants at the top (sheet names, column letters, API property keys), then save.

  3. 03

    Authorize once

    Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.

  4. 04

    Add the trigger

    Triggers → Add trigger → choose the handler function and event (time-driven, on edit, or on form submit). Delete test triggers before production.

Before you run: flag expiring training certificates

  • 1Setup tabs exist with headers matching flagExpiringCerts
  • 2Dry-run on a copy workbook
  • 3Timezone verified
  • 4MailApp authorization completed
  • 5Output spot-checked against hand calc
  • 6Time-driven trigger added only after validation
  • 7Owners + alert recipients documented

Frequently asked questions

At minimum the tabs listed in the setup table for Flag Expiring Training Certificates. Output tabs may be cleared each run—do not store source-of-truth data there.

Run from the Apps Script editor for dry runs. Production usually uses a Time-driven trigger after OAuth succeeds once.

Already-expired certs are excluded—only future expiries inside the window are mailed.

Per-cell calls burn quota and wall time. One read + one write keeps this pattern under the 6-minute execution cap longer.

PropertiesService (Script Properties) for API keys and alert inboxes. Config sheet is fine for non-secret thresholds.

Simple ratios maybe; flag expiring training certificates needs joins, branching, or side effects (email/Docs/API) that Apps Script handles cleanly.

Keep the .gs in clasp/git. Avoid divergent copies of formulas on the output tab—regenerate from the script.

Related examples

Want this wired into your real workflow?

I adapt these patterns to your Sheet structure, APIs, and triggers — deployed in your Google account. Fixed-scope quotes from $500 · free 30-min consult · quote within 24 hours.