IntegrationsIntermediate

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.

9 minIntermediate

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.

Google Sheets - reviews
A B
1 Review Sentiment
2 Shipping was fast and great
3 Product broke after one day
4 It is fine, nothing special
Source text in A, an empty label column in B.

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.

Code.gs
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.

Apps Script - Execution log
$Run -> classifyRows
Execution started
Execution completed (3 rows updated)
$
Google Sheets - reviews
A B
1 Review Sentiment
2 Shipping was fast and great positive
3 Product broke after one day negative
4 It is fine, nothing special neutral
Column B now holds a sentiment label for each review.
Define your own labels
Swap the categories in the system message for anything you need, such as bug, feature request, or question. Keeping the allowed set short makes results consistent.

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

Tags
#google-sheets#chatgpt#openai#classification