
Add AI to Google Sheets with Apps Script (Pay in INR)
Turn any Google Sheet into an AI-powered tool with a custom formula — classify, summarize, or extract data from a cell using a few lines of Apps Script and an INR-billed API key.
Author
AICredits Team
Published
1 Sept 2026
Reading time
5 min read
Why this is worth setting up
A lot of real AI usage in businesses isn't a chatbot or an app — it's someone wanting to run the same prompt across 500 rows of a spreadsheet: classify support tickets, summarize feedback, extract a structured field from messy text. Google Sheets already has the data. Apps Script lets you add a custom formula that calls an LLM directly from a cell, no separate tool required.
Setting up the script
Open your sheet, go to Extensions → Apps Script, and paste:
function AI_ASK(prompt) {
const apiKey = "sk-your-aicredits-key";
const url = "https://api.aicredits.in/v1/chat/completions";
const payload = {
model: "openai/gpt-4o-mini",
messages: [{ role: "user", content: prompt }],
};
const options = {
method: "post",
contentType: "application/json",
headers: { Authorization: "Bearer " + apiKey },
payload: JSON.stringify(payload),
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response.getContentText());
return data.choices[0].message.content;
}Save the script, return to the sheet, and use it as a formula:
=AI_ASK("Classify this customer feedback as Positive, Neutral, or Negative: " & A2)
Drag the formula down any number of rows — each cell triggers its own API call.
A more useful version: passing context from another column
function AI_CLASSIFY(text, categories) {
const apiKey = "sk-your-aicredits-key";
const url = "https://api.aicredits.in/v1/chat/completions";
const payload = {
model: "openai/gpt-4o-mini",
messages: [
{
role: "system",
content: "Classify the input into exactly one of these categories: " + categories + ". Respond with only the category name.",
},
{ role: "user", content: text },
],
};
const options = {
method: "post",
contentType: "application/json",
headers: { Authorization: "Bearer " + apiKey },
payload: JSON.stringify(payload),
};
const response = UrlFetchApp.fetch(url, options);
return JSON.parse(response.getContentText()).choices[0].message.content;
}=AI_CLASSIFY(B2, "Billing, Technical, Feature Request, Other")
Watch the rate limit on large sheets
Google Sheets recalculates all formulas together when the sheet loads or a source cell changes, which can fire dozens of API calls within seconds on a large dataset. Two things to keep in mind:
- Rate limits: your API key has an RPM cap (60 by default) — a 500-row sheet recalculating at once can exceed it, returning errors in some cells. Stagger large batch jobs or raise your key's RPM limit if you need to process a big sheet in one go.
- Cost: at GPT-4o-mini pricing, classifying 1,000 rows of typical feedback (50 input tokens, 10 output tokens each) costs well under ₹1 total. This is genuinely cheap even at spreadsheet scale.
Frequently Asked Questions
Will this work in Google Sheets on a personal Gmail account, or only Workspace?
Apps Script and UrlFetchApp work on both personal Gmail accounts and Google Workspace — no admin approval needed for a script you write and run yourself.
Can I use this for images, not just text?
The AI_ASK pattern is for text/chat completions. For image generation, you'd call /v1/images/generations instead and return a URL, though Sheets cells can't render an inline image from a formula — you'd need a script that inserts the image into the sheet separately.
Does the API key get exposed if I share the sheet?
Anyone with edit access to the script can see the key. For shared sheets, use Apps Script's PropertiesService to store the key outside the visible script body, or restrict edit access to the script itself.
Get started
- Create a free account and generate a key for your script.
- Read the API reference for the full request format.
- Related: How to Get Structured JSON Output from Any LLM — useful if you want a formula that returns multiple fields at once.
Related Articles
Continue in Docs
Need implementation commands and endpoint details? Go to quickstart or API reference.