How to run a free LLM API from Google Sheets, one row at a time
Use a custom Apps Script menu to send one selected row to your free LLM API and write the result only into an empty output cell. Run it in a private sheet, keep the key in Script Properties, and leave completed rows untouched. This gives you a deliberate one-call action while you check the provider's quota.
Set up a two-column scratch sheet
Start with a private sheet named LLM scratch. Put Input in A1 and Result in B1. Use short, synthetic examples such as Classify: green cotton tote in A2 and Classify: black rain jacket in A3. Select one input cell before running the menu. Each empty result cell is eligible for a call. An existing result stops a second call on that row.
In Extensions → Apps Script, open Project Settings and set three Script Properties. LLM_CHAT_URL is the complete HTTPS Chat Completions URL supplied by your provider. LLM_MODEL is its exact model ID. LLM_KEY is its existing API key. Check that the model is on the free route you intend to use. Apps Script and your provider enforce separate limits. Script Properties are shared within the script, and editors of a bound spreadsheet can edit its code. Keep this scratch sheet private and send only text you can disclose to the selected provider.
Run a selected row
Paste this example into the bound Apps Script project and reload the sheet. Select A2 and choose LLM scratch → Run selected row. Google may ask you to authorize spreadsheet and external-request access the first time. The example performs one non-streaming request when the row is ready and leaves the result blank on HTTP errors. It does not automatically retry.
function onOpen() {
SpreadsheetApp.getUi().createMenu('LLM scratch')
.addItem('Run selected row', 'runSelectedLlmRow').addToUi();
}
function runSelectedLlmRow() {
const lock = LockService.getDocumentLock();
if (!lock.tryLock(1000)) throw new Error('A row is already running.');
try {
const sheet = SpreadsheetApp.getActiveSheet();
const cell = sheet.getActiveRange();
if (sheet.getName() !== 'LLM scratch' || !cell || cell.getNumRows() !== 1 ||
cell.getNumColumns() !== 1 || cell.getColumn() !== 1 || cell.getRow() < 2) {
throw new Error('Select one input cell below A1 on LLM scratch.');
}
const prompt = cell.getDisplayValue().trim();
if (!prompt) throw new Error('The input is empty.');
const result = sheet.getRange(cell.getRow(), 2);
if (!result.isBlank()) return;
const settings = PropertiesService.getScriptProperties();
const url = settings.getProperty('LLM_CHAT_URL');
const model = settings.getProperty('LLM_MODEL');
const key = settings.getProperty('LLM_KEY');
if (!/^https:\/\//i.test(url || '') || !model || !key) {
throw new Error('Set the HTTPS chat URL, model, and key in Script Properties.');
}
const reply = UrlFetchApp.fetch(url, {
method: 'post',
contentType: 'application/json',
headers: {Authorization: 'Bearer ' + key},
followRedirects: false,
muteHttpExceptions: true,
payload: JSON.stringify({model, messages: [{role: 'user', content: prompt}]}),
});
const status = reply.getResponseCode();
if (status !== 200) throw new Error('API returned HTTP ' + status + '. Row left blank.');
const answer = JSON.parse(reply.getContentText()).choices?.[0]?.message?.content;
if (typeof answer !== 'string' || !answer.trim()) throw new Error('No text result.');
if (cell.getDisplayValue().trim() !== prompt || !result.isBlank()) {
throw new Error('Input or result changed during the request. Review this row.');
}
result.setValue('Model: ' + answer);
SpreadsheetApp.flush();
} finally {
lock.releaseLock();
}
}
Rehearse a repeat click before adding more rows
Here is a synthetic acceptance case. Start with A2 and A3 populated and both result cells blank. A successful run on A2 writes Model: apparel in B2. Select A2 again. The filled result makes the function return before it reads the key or calls the provider. If the first attempt on A3 returns 429, B3 stays blank. Stop and check the provider's error and limits before deciding whether to run A3 again.
The same sequence takes three menu clicks and two requests. It spends one attempt on the failed row even though that row has no result. Whether the provider counts that failure toward a free limit depends on its rules. Keep the working table separate from the provider's counters. A blank B cell tells you a result is missing. It cannot tell you whether a request was sent before an error or interruption.
If you edit the input or fill the result while a request is running, the example stops before writing the returned text. Review that row before trying again. If you edit A2 after a successful run, B2 still records the old answer. Add a new row for the new prompt so you can keep both results. If you intentionally want to rerun the same row, preserve the original result and clear B2 yourself before selecting A2. The Model: prefix makes even a result starting with an equals sign plain cell text.
FAQ
Can I select a whole column? The sample accepts one cell. Before expanding it, choose a batch size, keep an explicit completion marker for each row, and plan for a run that stops midway. A worked job budget shows how to translate a provider's remaining requests into work items.
Why stop instead of retrying 429 immediately? A single failed row remains visible without starting a second request. Read the provider's retry guidance and separate a short limit from a depleted allowance before manually retrying.
Can I share the live sheet? Keep the credential-bearing sheet private. Share a separate values-only copy without the bound script or its settings. Decide whether the output itself is appropriate to share.
Sources
Checked 7 November 2026. The rows and responses above are synthetic. Apps Script Properties Service · Script collaboration · Custom functions and menus · URL Fetch · Range write behavior · Lock Service.