SerpBase
Back to Blog

Google Search Results in Google Sheets: a =SERP() Function With Apps Script

A 25-line Apps Script custom function that fills a sheet with Google results: rank, title, link, snippet. Key kept in script properties; tested 5 December 2026.

December 5, 2026
By SerpBase Team3 min read

Short answer: add a small Apps Script custom function to your spreadsheet that POSTs the query to a search API and returns a two-dimensional array; then =SERP("coffee grinder reviews") fills the cells below it with rank, title, link and snippet. Google Sheets has no built-in function for Google search results, so the script calls https://api.serpbase.dev/google/search with UrlFetchApp.

1. Store your API key

In the sheet, open Extensions → Apps Script, then Project Settings → Script properties → Add script property: name SERPBASE_API_KEY, value your key. The key stays out of the code and out of the cells.

2. Paste the function

/**
 * Google results for a query, one row per organic result.
 * =SERP("coffee grinder reviews")  or  =SERP(A2, "gb", "en")
 * @customfunction
 */
function SERP(query, gl, hl) {
  if (!query) return "";
  const key = PropertiesService.getScriptProperties().getProperty("SERPBASE_API_KEY");
  const res = UrlFetchApp.fetch("https://api.serpbase.dev/google/search", {
    method: "post",
    contentType: "application/json",
    headers: { "X-API-Key": key },
    payload: JSON.stringify({ q: String(query), gl: gl || "us", hl: hl || "en", timeout: 25 }),
    muteHttpExceptions: true,
  });
  const data = JSON.parse(res.getContentText());
  if (data.status !== 0) {
    return [["error " + data.status + ": " + (data.error || "")]];
  }
  const rows = [["rank", "title", "link", "snippet"]];
  (data.organic || []).forEach(function (r) {
    rows.push([r.rank, r.title, r.link, r.snippet || ""]);
  });
  return rows;
}

Save, go back to the sheet and type =SERP("coffee grinder reviews") in an empty cell. The result spills into the cells to the right and below, so leave room: 4 columns and about 11 rows.

What it returned

We sent this search on 5 December 2026 and ran the function against the recorded answer. It took 4.5 seconds and returned 7 organic results (the page also had an AI Overview, 4 People Also Ask questions and 4 videos, which this function skips). The first rows:

ranktitlelink
1Best Coffee Grinders Reviewed 2024 | Gearweare.nethttps://gearweare.net/review/best-coffee-grinders/
2THE TOP BEST COFFEE GRINDER MODELS AS TESTED BY EXPERTShttps://www.homeessentialsblog.com/post/the-top-best-coffee-grinder-models-as-tested-by-experts
311 Best Espresso Grinders in 2026: Review & #1 Buyer's Guidehttps://coffeegeek.tv/best-espresso-grinders/

Things that trip people up

  • 30-second limit. Google's documentation says a custom function call must return within 30 seconds, or the cell shows #ERROR!. The function sends timeout: 25, so a slow search ends with an error 1504 row (not charged) instead of an error cell.
  • Every recalculation is a new search. Sheets re-runs a custom function when you edit it or change a cell it references. Each run costs 1 credit, so keep the queries in cells (=SERP(A2)) and avoid volatile inputs such as NOW(). To keep a result, copy and Paste special → Values only.
  • One call per cell. Each =SERP() is a separate request to Google's script servers. For hundreds of keywords, a script that loops over a column and writes values once (run from a menu or a trigger) is easier to control than hundreds of formulas.
  • The spill area must be empty. If a cell the result needs already has content, the function shows an error instead of overwriting it.
  • Some links are Google redirect links. In this run 3 of the 7 link values were https://www.google.com/goto?url=…; the free Google link resolver returns the real address.
  • HTTP 200 does not mean success. The function checks status; failed requests are not charged.

Want other columns? date, display_link and position are on each organic result; people_also_ask and related_searches are separate arrays. The field list is in the docs below.

Related

Cost and docs

A Google search is 1 credit, $0.25–$0.50 per 1,000 depending on the pack. New accounts get 500 free credits.