How to Classify Spreadsheet Rows With ChatGPT in Google Sheets
Use an OpenAI call inside Apps Script to tag each row in Google Sheets with a category like positive, neutral or negative.
Tagging hundreds of rows by hand is slow. This guide loops through a column of text in Google Sheets and asks ChatGPT to label each one with a category you define. It is ideal for sorting survey replies, support tickets, or product reviews.
- A Google Sheet with a text column to classify
- An OpenAI API key
- Edit access to the sheet and its Apps Script
- About 9 minutes
Step 1: Lay out your columns
Put the text you want labeled in column A starting at row 2. Leave column B empty for the label the script will fill in. A header row keeps things readable.
Step 2: Add the classification script
Open Extensions then Apps Script and paste the function below. It reads every non-empty cell in column A, sends it to the Chat Completions API with a strict instruction to reply with one word, and writes the label to column B.
const OPENAI_KEY = "sk-PASTE_YOUR_KEY";
function classifyRows() {
const sheet = SpreadsheetApp.getActiveSheet();
const rows = sheet.getRange("A2:A").getValues();
for (let i = 0; i < rows.length; i++) {
const text = rows[i][0];
if (!text) continue;
const label = askOpenAI(text);
sheet.getRange(i + 2, 2).setValue(label);
}
}
function askOpenAI(text) {
const res = UrlFetchApp.fetch("https://api.openai.com/v1/chat/completions", {
method: "post",
contentType: "application/json",
headers: { Authorization: "Bearer " + OPENAI_KEY },
payload: JSON.stringify({
model: "gpt-5-mini",
messages: [
{ role: "system", content: "Reply with one word: positive, neutral, or negative." },
{ role: "user", content: text },
],
}),
});
return JSON.parse(res.getContentText()).choices[0].message.content.trim();
}Step 3: Run the function once
Pick classifyRows from the function dropdown in the toolbar and click Run. Approve the authorization prompt on the first run. The script processes each row in order.
Result: a single run fills column B with consistent one-word labels. Add a filter on that column to slice your data instantly.
Watch related tutorials
12:38
14:09
17:53
15:00
12:00
30:00