DEVSAIO

Add an =AI() function to Google Sheets with Apps Script

Write a custom Google Sheets function that sends a prompt to any model, perfect for bulk writing, tagging and cleaning data.

DEVSAIO team · 2 min read
Share on X

With a few lines of Apps Script you can call AI from a spreadsheet cell: =AI("Write a product title for", A2).

1. Store your key safely

In your sheet, open Extensions → Apps Script, then Project Settings → Script properties and add DEVSAIO_API_KEY with your key. Script properties aren't visible to people who only view the sheet.

2. Add the function

js
/**
 * Ask an AI model. Usage: =AI("Summarise:", A2)
 * @customfunction
 */
function AI(instruction, input) {
  const key = PropertiesService.getScriptProperties().getProperty("DEVSAIO_API_KEY");
  const res = UrlFetchApp.fetch("https://devsaio.lol/v1/chat/completions", {
    method: "post",
    contentType: "application/json",
    headers: { Authorization: "Bearer " + key },
    muteHttpExceptions: true,
    payload: JSON.stringify({
      model: "claude-haiku-5-5",
      max_tokens: 300,
      messages: [{ role: "user", content: instruction + "\n\n" + (input || "") }],
    }),
  });
  const data = JSON.parse(res.getContentText());
  if (res.getResponseCode() !== 200) return "Error: " + (data.error ? data.error.message : res.getResponseCode());
  return data.choices[0].message.content.trim();
}

3. Use it

text
=AI("Classify this review as positive, neutral or negative. Reply with one word.", B2)

Drag down to process a whole column.

Things to know

  • Each cell is a request. 1,000 rows is 1,000 requests, so use a cheap model and short instructions.
  • Sheets recalculates formulas. Once results look right, copy the column and paste values only so you don't pay again.
  • Custom functions have an execution time limit; for very long outputs, use a menu-triggered script that writes values instead.
Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading