Blog

Trend data in Google Sheets with Apps Script

Pull trend data into Google Sheets with one Apps Script function: a POST to the Trends API, the double JSON parse, and a Black Friday example with real numbers.

Trends API team Published Data as of 2026-11-23

Daily Google Search index for Black Friday from November 9 to 22, 2026, climbing from 40 to a peak of 100 on Nov 20

One Apps Script function pulls a live trend series into Google Sheets: POST a JSON body to https://api.trendsmcp.ai/api, parse the response twice, and write the date and value columns. The worked example below fetches the daily Google Search series for "black friday" and returns 92 points from Aug 23 to Nov 22, 2026, peaking at 100 on Nov 20 and reading 91 on Nov 22. The call costs 1 request. The one step that trips people up is the parse: the response body is a JSON string, so it needs parsing again before the rows exist.

The call: one POST, one key

The endpoint is https://api.trendsmcp.ai/api. The request is a POST with a JSON body where mode picks the tool. For a series, the body names get_time_series, a keyword, a source and a resolution:

{
  "mode": "get_time_series",
  "keyword": "black friday",
  "source": "Google Search",
  "resolution": "daily"
}

Auth is a Bearer header with the API key. The raw call was verified on Nov 23, 2026: it returned statusCode 200 with a body holding 92 daily points, the first dated 2026-08-23 with value 4. Note the shape. The outer object carries statusCode and body, and body itself is a JSON string holding the array. A single JSON.parse leaves a string, not rows. The function below parses twice, which is the whole trick. Key setup is covered in the API docs.

The function: paste, set the key, run

Open a sheet, then Extensions, then Apps Script, and paste this. Store the key first under Project Settings, then Script Properties, as TRENDS_API_KEY, so the key never sits in the code.

function fetchTrend(keyword, source) {
  var props = PropertiesService.getScriptProperties();
  var key = props.getProperty('TRENDS_API_KEY');
  var url = 'https://api.trendsmcp.ai/api';
  var payload = {
    mode: 'get_time_series',
    keyword: keyword,
    source: source || 'Google Search',
    resolution: 'daily'
  };
  var options = {
    method: 'post',
    contentType: 'application/json',
    headers: { Authorization: 'Bearer ' + key },
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };
  var response = UrlFetchApp.fetch(url, options);
  var outer = JSON.parse(response.getContentText());
  if (outer.error) {
    throw new Error('Trends API error: ' + outer.error);
  }
  var rows = JSON.parse(outer.body);
  if (!rows.length) {
    return 'no points returned';
  }
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.clear();
  sheet.getRange(1, 1, 1, 2).setValues([['date', 'value']]);
  var values = rows.map(function (r) {
    return [r.date.slice(0, 10), r.value];
  });
  sheet.getRange(2, 1, values.length, 2).setValues(values);
  return values.length + ' points written';
}

Run fetchTrend('black friday', 'Google Search') from the editor. The active sheet fills with two columns, date and value, newest last. A custom function in a cell cannot call UrlFetchApp with auth, so run it from the editor or a menu item instead of =fetchTrend(...) in a formula.

The worked example: Black Friday in a sheet

The Nov 23 run wrote 92 daily points from Aug 23 to Nov 22, 2026. The last 14 days tell the story: Nov 9 through Nov 15 read 40, 43, 51, 55, 59, 56 and 63, then Nov 16 and 17 held at 63, Nov 18 read 70, Nov 19 read 86, Nov 20 peaked at 100, and Nov 21 and 22 read 86 and 91. Black Friday 2026 is Nov 27, seven days after the peak, and the series read 86 and 91 on the two days after it. The earlier part of the series is quiet: values of 3 to 7 from Aug 23 through Sep 18, a brief bump to 20 on Sep 20, back to 5 or 6 by Sep 23, then the steep November climb. Select the two columns, then Insert, then Chart, and pick a line chart with the date column as the labels. For a second series, open a second sheet tab and run the function there for another keyword, for example "christmas gifts", and the two ramps sit on separate tabs for a direct comparison of deal demand against gift demand. The GTA 6 tutorial shows the same series call in Python and curl for readers who want the same data outside Sheets.

Limits to know before automating

Google Search series return a 0 to 100 index, not absolute search volume, so compare shapes and peaks, not counts. Each get_time_series call costs 1 request; see pricing for the free tier. A daily refresh of one keyword costs about 30 requests a month. Do not put the fetch on a per-minute trigger: a per-minute trigger would burn 43,200 requests a month. A once-a-day time trigger, or a manual run before a meeting, fits the data, which is a daily-resolution series. Cache the result in the sheet itself and re-fetch only when the analysis needs fresh numbers.

Keep the key out of the sheet

The key lives in Script Properties for a reason: it is the one credential the whole function depends on, and it should never appear in a cell, a comment or the code itself. A sheet shared with viewers does not expose Script Properties, so a reader can see the numbers without holding the key. The Apps Script project is a different boundary: anyone with editor access to the project can open Project Settings and read the key, so share the project only with people who should hold it. If a key is ever pasted into a cell, shared in a chat message or committed to a repository, treat it as leaked: revoke it in the Trends dashboard and generate a fresh one, then update the Script Property. The function itself needs no change when the key rotates, since it reads the property on every run. That separation is what makes the sheet safe to share and the key safe to rotate.

A daily trigger keeps the sheet current

A line chart and a once-a-day trigger turn the two columns into a dashboard that keeps itself current once a day. In the Apps Script editor, open Triggers, then Add Trigger, and set the function to run on a day timer at an hour that suits the team. The function reads the key from Script Properties each run, so the trigger needs no further setup. One caution: a trigger runs the function with no arguments, so set defaults in the function signature or wrap it in a small runner that passes the keyword. A runner looks like this:

function refreshBlackFriday() {
  fetchTrend('black friday', 'Google Search');
}

Point the trigger at refreshBlackFriday instead of fetchTrend. If the sheet stops updating, check three things in order: the Script Property still holds a valid key, the execution log for errors, and the request count for the month. An invalid-key error in the response means the key is wrong or missing, and the function throws instead of writing rows. A run that returns no points reports "no points returned" and leaves the sheet untouched. The TikTok hashtags tutorial shows the same error handling pattern on a board call.

Sources