IntegrationsBeginner

How to Translate a Google Sheets Column With an AI Custom Function

Create a =TRANSLATEAI() function in Google Sheets that uses an AI model to translate any cell into a target language.

7 minBeginner

Translating a column of product names or support replies one by one is tedious. This guide builds a =TRANSLATEAI() custom function that sends each cell to an AI model and returns the translation, so you can translate a whole column by dragging one formula.

  • A Google Sheet with text to translate
  • An OpenAI API key
  • Access to the sheet's Apps Script editor
  • About 7 minutes

Step 1: Open Apps Script and add the function

From the sheet choose Extensions then Apps Script. Paste the function below. It takes the text and a target language code so the same formula handles any language.

Code.gs
const OPENAI_KEY = "sk-PASTE_YOUR_KEY";

function TRANSLATEAI(text, lang) {
  if (!text) return "";
  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: "Translate the text to " + lang + ". Return only the translation." },
        { role: "user", content: text },
      ],
    }),
  });
  return JSON.parse(res.getContentText()).choices[0].message.content.trim();
}

Step 2: Save and return to the sheet

Click Save in the editor, then switch back to your spreadsheet tab. The new function is available immediately as if it were built in.

Step 3: Use the formula with a language argument

In an empty cell type the formula referencing your source cell and a language. The first run asks you to authorize external requests; approve it once.

formula
=TRANSLATEAI(A2, "French")
Google Sheets - catalog
A B
1 English French
2 Fast and reliable =TRANSLATEAI(A2, "French")
3 Free shipping Livraison gratuite
Column B translates column A into French via the custom function.

Step 4: Fill the column

Grab the small square at the bottom right of the cell and drag it down to translate the rest of the column. Each row fires its own call, so give large columns a moment to finish.

Freeze your results
Custom functions recalculate and re-call the API whenever the sheet changes. Once translations look right, copy the column and use Paste special, Values only, to lock them and stop further API usage.

Result: a reusable translation function lives in your sheet. Change the second argument to Spanish, German, or any language and the same formula adapts instantly.

Watch related tutorials

Tags
#google-sheets#translation#apps-script#ai