Roll Scope 1/2 emissions, waste, and water into a monthly sustainability KPI dashboard sheet. The workbook becomes the system of record; Apps Script owns the joins and business rules so analysts are not pasting monthly formulas.
Entry point `refreshSustainabilityKpis` reads typed columns with SpreadsheetApp batch APIs. SpreadsheetApp 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 refresh a sustainability kpi dashboard. No consulting pitch—just the pattern you can paste and harden.
Need this built? Hire a Google Apps Script developer →
Sheet / project setup
| Tab | Columns | Notes |
|---|---|---|
| Emissions | Month, Scope1, Scope2, Waste, Water, Revenue | Inputs |
| KpiDash | KPI labels + values | Dashboard |
What this script does
refreshSustainabilityKpis() roll Scope 1/2 emissions, waste, and water into a monthly sustainability KPI dashboard sheet.
- Month filter via yyyy-MM
- Scope1+Scope2 total
- Intensity = tCO2e / revenue
Prerequisites
Container-bound script on the workbook that contains the tabs in the setup table. Authorize SpreadsheetApp scopes on first run.
- V8 runtime
- Exact sheet names
- Script timezone = ops timezone
Walkthrough
Filter Emissions to the current yyyy-MM, sum Scope1/2/waste/water/revenue, write KPI block including intensity per revenue dollar.
Paste the code, set Config/Properties, run refreshSustainabilityKpis once, verify the output tab, then attach a Time-driven trigger.
Edge cases
Rows outside the current month are ignored; revenue 0 yields intensity 0 rather than Infinity.
Testing
Use a sandbox copy with 3–5 known rows. Compute expected outputs for refresh a sustainability kpi dashboard offline, run refreshSustainabilityKpis, 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 refreshSustainabilityKpis succeeds.
Operations notes
Assign an owner for the output tab, document regenerate steps, and treat `refreshSustainabilityKpis` as source of truth over ad-hoc cell formulas.
Full code: refreshSustainabilityKpis()
Run refreshSustainabilityKpis when source tabs are current. Edit sheet names and Config/Properties first.
function refreshSustainabilityKpis(){
const ss=SpreadsheetApp.getActive();
const tz=Session.getScriptTimeZone();
const month=Utilities.formatDate(new Date(), tz,'yyyy-MM');
const rows=ss.getSheetByName('Emissions').getDataRange().getValues().slice(1);
let s1=0,s2=0,waste=0,water=0,rev=0;
rows.forEach(r=>{
if(Utilities.formatDate(new Date(r[0]),tz,'yyyy-MM')!==month) return;
s1+=Number(r[1])||0; s2+=Number(r[2])||0;
waste+=Number(r[3])||0; water+=Number(r[4])||0; rev+=Number(r[5])||0;
});
const total=s1+s2;
const intensity=rev? total/rev : 0;
const dash=ss.getSheetByName('KpiDash');
dash.getRange('B2:B7').setValues([[month],[s1],[s2],[total],[waste],[intensity]]);
dash.getRange('A2:A7').setValues([['Period'],['Scope1_tCO2e'],['Scope2_tCO2e'],['Total_tCO2e'],['Waste_t'],['tCO2e_per_$']]);
}- Line 1: Entry point — bind triggers to refreshSustainabilityKpis.
- Line 5: Batch read; avoid per-cell getValue in loops.
- Line 15: Batch write output rows in one setValues call.
- Line 5: Rename sheet constants before production.
- Line 6: Rename sheet constants before production.
Deploy this example
- 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.
- 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.
- 03
Authorize once
Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.
- 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: refresh a sustainability kpi dashboard
- 1Setup tabs exist with headers matching refreshSustainabilityKpis
- 2Dry-run on a copy workbook
- 3Timezone verified
- 4SpreadsheetApp 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 Refresh a Sustainability KPI Dashboard. 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.
Rows outside the current month are ignored; revenue 0 yields intensity 0 rather than Infinity.
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; refresh a sustainability kpi dashboard 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.