Power Outage Data in Google Sheets

To get power outage data into Google Sheets, add an Apps Script function that calls the PowerOutage.us v2 REST API with UrlFetchApp.fetch, reads your key from Script Properties, and writes one row per county to a sheet, then run it every 15 minutes with a time-driven trigger. The trigger runs on Google's servers, so the sheet stays current with nobody signed in. The built-in IMPORTDATA function can't do this, because it can't send the Authorization header the API requires. This guide covers the key, the script, authorizing it, the trigger, and formulas that build on the rows.

What you'll have

  • A County outages sheet with customers out, customers tracked, FIPS code and last update time for every county in the states you choose.
  • Rows rewritten every 15 minutes by a time-driven trigger, with no browser or computer left running.
  • The API key in Script Properties, out of the code and out of the cells.
  • FIPS codes stored as plain text with their leading zeros, and room for your own formula columns beside the data.

What you need

An API key (request one with the form below) and these endpoints. Full details are in the API reference.

EndpointMethodUsed forPackage
/outages/countiesGETCounty customersOut, customersTracked and lastUpdatedAt, with include=fips for FIPS codes and meta.generatedAt for staleness checks.Core

Step 1: Open Apps Script and store the key

In the spreadsheet, choose Extensions > Apps Script. In the editor, open Project Settings (the gear icon), scroll to Script Properties, choose Add script property, enter POWEROUTAGE_API_KEY as the property and your key as the value, and choose Save script properties. Script Properties keep the key out of the code and the cells, but not from anyone who can open the script. This script is bound to the spreadsheet and shares its sharing list, so editors can read the key in Project Settings, Google's documentation says view-only collaborators can view a bound script too, and anyone who makes a copy of the spreadsheet gets a copy of the script. If the spreadsheet is shared with anyone who shouldn't see the key, or published, use the standalone-script setup in the tips below. Either way, use a separate key restricted to the states this sheet covers.

Step 2: Add the script

Replace the contents of Code.gs with this script and save. refreshCountyOutages requests county counts and writes them to a sheet named County outages, creating it on the first run. The mapping is id to County ID, name to County, stateId to State ID, fips.full to FIPS (blank for the Unknown county row), customersOut and customersTracked to the two counts, lastUpdatedAt to Last Updated and meta.generatedAt to Generated At. muteHttpExceptions lets the script read the status itself: on a 429 it waits the Retry-After seconds with Utilities.sleep and tries again, three times at most, without waiting after the last try. It fetches before clearing anything, so a failed run leaves the previous rows in place, and it clears only columns A to H before writing the header and rows with setValues.

javascript
// 44 is Florida and 45 is Georgia. List ids with GET /outages/states.
const STATE_IDS = '44,45';
const SHEET_NAME = 'County outages';
const HEADER = [
  'County ID', 'County', 'State ID', 'FIPS', 'Customers Out',
  'Customers Tracked', 'Last Updated', 'Generated At',
];

function fetchJson(url) {
  const key = PropertiesService.getScriptProperties().getProperty('POWEROUTAGE_API_KEY');
  if (!key) throw new Error('Add POWEROUTAGE_API_KEY under Project Settings > Script Properties');
  for (let attempt = 1; attempt <= 3; attempt++) {
    const response = UrlFetchApp.fetch(url, {
      headers: { Authorization: 'Bearer ' + key },
      muteHttpExceptions: true,
    });
    const status = response.getResponseCode();
    if (status === 200) return JSON.parse(response.getContentText());
    if (status !== 429) throw new Error('HTTP ' + status + ': ' + response.getContentText());
    if (attempt < 3) {
      const headers = response.getHeaders();
      const seconds = Number(headers['Retry-After'] || headers['retry-after'] || 10);
      Utilities.sleep(seconds * 1000);
    }
  }
  throw new Error('Still rate limited after 3 tries: ' + url);
}

function refreshCountyOutages() {
  // Fetch before touching the sheet, so a failed run leaves the last good rows in place
  const payload = fetchJson(
    'https://poweroutage.us/api/v2/outages/counties?include=fips&stateId=' + STATE_IDS
  );
  const generatedAt = new Date(payload.meta.generatedAt);
  const rows = payload.data.map((county) => [
    county.id,
    county.name,
    county.stateId,
    county.fips ? county.fips.full : '',
    county.customersOut,
    county.customersTracked,
    county.lastUpdatedAt ? new Date(county.lastUpdatedAt) : '',
    generatedAt,
  ]);

  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getSheetByName(SHEET_NAME) || spreadsheet.insertSheet(SHEET_NAME);
  sheet.getRange('A:H').clearContent(); // columns I onward are yours and survive each run
  // A new sheet has 1,000 rows, and writing past the last row fails, so add rows when needed
  const missingRows = rows.length + 1 - sheet.getMaxRows();
  if (missingRows > 0) sheet.insertRowsAfter(sheet.getMaxRows(), missingRows);
  sheet.getRange('D:D').setNumberFormat('@'); // after adding rows: plain text keeps FIPS leading zeros
  sheet.getRange(1, 1, 1, HEADER.length).setValues([HEADER]);
  if (rows.length > 0) sheet.getRange(2, 1, rows.length, HEADER.length).setValues(rows);
}

function createTrigger() {
  // Remove an earlier copy first, so running this twice never doubles the schedule
  for (const trigger of ScriptApp.getProjectTriggers()) {
    if (trigger.getHandlerFunction() === 'refreshCountyOutages') ScriptApp.deleteTrigger(trigger);
  }
  ScriptApp.newTrigger('refreshCountyOutages').timeBased().everyMinutes(15).create();
}

Step 3: Run it once and authorize it

Choose refreshCountyOutages in the function list and select Run. The first time, Google asks you to review permissions: the script needs to edit your spreadsheets and to connect to an external service, and if your Google Workspace administrator restricts Apps Script, they may need to allow it. On a personal Google account you may then see a Google hasn't verified this app screen, because nobody has reviewed your own script; choose Advanced, then Go to followed by your project's name (unsafe), and allow access. Check the County outages sheet. Times show in the spreadsheet's time zone, which you can change under File > Settings.

Step 4: Schedule it every 15 minutes

Choose createTrigger in the function list and run it once. It creates a time-driven trigger with ScriptApp.newTrigger(...).timeBased().everyMinutes(15), after removing any earlier copy. everyMinutes accepts only 1, 5, 10, 15 or 30. Every 15 minutes is enough for most uses; the data updates every minute, so change it to 1 or 5 if you need the latest numbers. Google may shift each run slightly from the exact quarter hour. The trigger runs under your account, and you can see it, with every run and its errors, under Triggers and Executions on the left of the editor.

Step 5: Build on the rows with formulas

Put your own formulas in column I onward, or on other sheets, because each run rewrites columns A to H. The first formula fills column I with the minutes between each county's last update and the time the API generated the data; records are not zeroed when a utility stops reporting, so a large value means the count may be out of date. The second totals customers out for one state. Sum and look up by State ID and County ID rather than names, which repeat across states.

text
I1, minutes since each county was last reported:
={"Minutes since report"; ARRAYFORMULA(IF(G2:G = "", , ROUND((H2:H - G2:G) * 1440)))}

On another sheet, customers out in state 44 (Florida):
=SUMIF('County outages'!C:C, 44, 'County outages'!E:E)

Tips for Google Sheets

  • Apps Script quotas allow 20,000 URL Fetch calls a day on a consumer account and 100,000 on Google Workspace, 6 minutes per run, and 90 minutes of trigger runtime a day on a consumer account or 6 hours on Workspace. A 15-minute trigger makes 96 calls a day, and each run takes a few seconds.
  • To share the spreadsheet widely without exposing the key, create the script as a standalone project at script.google.com instead, and replace SpreadsheetApp.getActiveSpreadsheet() with SpreadsheetApp.openById() and the spreadsheet's ID. The script, and its Script Properties, then have their own sharing list.
  • Apps Script emails the trigger owner when runs fail. A wrong or revoked key shows up there as HTTP 401, and the sheet keeps its last good rows until you fix it.
  • Each state can include a county named Unknown with a blank FIPS, holding customers a utility could not place in a county. Keep it in state totals, and filter it out of county lists and maps.

Used by

Related integrations

FAQ

Here are some commonly asked questions

Request API access

Tell us what you're building and we'll set you up with a trial key.