Google Sheets Integration
Add AI-powered formulas to Google Sheets with a custom Apps Script function. Classify, summarize, extract, and generate data directly in your spreadsheet cells.
What this enables
- Custom
=AI()formula for any cell - Batch processing rows with AI-powered classification and extraction
- Sentiment analysis, translation, and summarization in spreadsheets
- Works with any Google Sheets document
Quick Start
1. Open Apps Script
In your Google Sheet, go to Extensions > Apps Script.
2. Add the AI function
Replace the contents of Code.gs with:
const ANTBASE_API_KEY = 'your-ant-api-key';
const ANTBASE_URL = 'https://antbase.ai/v1/chat/completions';
/**
* Call Antbase AI from a cell.
* @param {string} prompt The prompt to send.
* @param {string} model The model to use (default: ant:auto).
* @return {string} The AI response.
* @customfunction
*/
function AI(prompt, model) {
model = model || 'ant:auto';
const payload = {
model: model,
messages: [{ role: 'user', content: prompt }],
max_tokens: 512,
temperature: 0,
};
const options = {
method: 'post',
contentType: 'application/json',
headers: {
Authorization: 'Bearer ' + ANTBASE_API_KEY,
},
payload: JSON.stringify(payload),
muteHttpExceptions: true,
};
const response = UrlFetchApp.fetch(ANTBASE_URL, options);
const json = JSON.parse(response.getContentText());
if (json.error) {
return 'Error: ' + json.error.message;
}
return json.choices[0].message.content.trim();
}
/**
* Classify text into a category.
* @param {string} text The text to classify.
* @param {string} categories Comma-separated categories.
* @return {string} The category.
* @customfunction
*/
function AI_CLASSIFY(text, categories) {
const prompt = 'Classify the following text into exactly one of these categories: '
+ categories + '. Respond with only the category name.\n\nText: ' + text;
return AI(prompt, 'ant:fast');
}
/**
* Analyze sentiment of text.
* @param {string} text The text to analyze.
* @return {string} positive, negative, or neutral.
* @customfunction
*/
function AI_SENTIMENT(text) {
const prompt = 'Analyze the sentiment of this text. Respond with exactly one word: positive, negative, or neutral.\n\n' + text;
return AI(prompt, 'ant:fast');
}3. Save and use
Save the script (Cmd+S), go back to your sheet, and use the formulas:
=AI("Summarize: " & A1)
=AI("Translate to French: " & A1, "ant:best")
=AI_CLASSIFY(A1, "support, sales, billing, spam")
=AI_SENTIMENT(A1)Configuration
For better security, store the API key in Script Properties instead of hardcoding:
// In Apps Script, go to Project Settings > Script Properties
// Add: ANTBASE_API_KEY = your-ant-api-key
// Then in your code:
const ANTBASE_API_KEY = PropertiesService.getScriptProperties()
.getProperty('ANTBASE_API_KEY');Usage Examples
Batch processing with formulas
| A (Input) | B (Formula) | C (Result) |
|---|---|---|
| Customer review text | =AI_SENTIMENT(A2) | positive |
| Support ticket text | =AI_CLASSIFY(A3, "bug, feature, question") | bug |
| Long article | =AI("Summarize in 1 sentence: " & A4) | Summary text... |
JSON extraction
=AI("Extract {name, email, company} as JSON from: " & A1, "ant:fast")Virtual Models
| Model | Best for | Notes |
|---|---|---|
| ant:auto | General use | Default for AI() formula |
| ant:fast | Classification, sentiment | Fast and cheap for batch processing |
| ant:best | Complex generation | Quality output for important data |
| ant:free | Testing | Zero cost while building formulas |
| ant:reasoning | Analysis | Multi-step reasoning |
| ant:coder | Code tasks | Formula generation |
See Virtual Models for the full list.
Troubleshooting
Formulas show "Loading..."
Google Sheets custom functions have a 30-second timeout. Use ant:fast for batch processing to keep response times short. Avoid applying AI formulas to hundreds of cells at once.
Authorization required
The first time you use the formula, Google will ask you to authorize the script to make external network requests. Click "Allow" when prompted.