Skip to main content
tool-tweaks beginner 6 min read

Wire ChatGPT into Google Sheets via Apps Script — one paste, zero billing

Summary. Most "GPT for Sheets" extensions charge $10–$20/mo per user and lock the formula behind their backend. A single Apps Script function lets you call =GPT(prompt) in any cell, billed straight to your OpenAI account. Free script, $5/mo of API usage instead of $240/mo across the team.

Tools needed: Google Workspace, an OpenAI API key, 8 minutes.

The problem

"GPT for Sheets" by Talarian is excellent but $19/user/mo. Add 12 users and you are at $228/mo for what is essentially a wrapper around the OpenAI API. The Apps Script version is 25 lines and bills per token.

Step-by-step

  1. Open a Google Sheet. Click Extensions → Apps Script.
  2. Replace the default code with:
    const OPENAI_KEY = PropertiesService.getScriptProperties().getProperty('OPENAI_KEY');
    
    function GPT(prompt, model) {
      if (!prompt) return '';
      const m = model || 'gpt-4o-mini';
      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: m,
          messages: [{ role: 'user', content: prompt }],
          temperature: 0.3
        }),
        muteHttpExceptions: true
      });
      const json = JSON.parse(res.getContentText());
      return json.choices && json.choices[0]
        ? json.choices[0].message.content.trim()
        : 'ERROR';
    }
  3. Click Project SettingsScript Properties → add OPENAI_KEY with your key. (Never paste the key in the code itself — anyone with sheet access can read it.)
  4. Save the script. Authorize it on first run.
  5. Back in the sheet, type =GPT("Summarise this in 1 sentence: " & A2). Drag down. Done.

Expected outcome

The whole team uses =GPT() in any cell. Bill goes to one OpenAI account. A 12-user team running ~5,000 prompts/mo on gpt-4o-mini spends ~$5/mo total instead of $228 on a per-seat extension.

Gotchas

  • Apps Script has a 6-min execution limit per function call — fine for single cells, but if you bulk-fill 1,000 rows at once, batch them.
  • Google caches custom function results aggressively. To force a re-run, change the prompt slightly (add a space) or wrap in =GPT(A1 & NOW()) for testing.
  • Share the sheet with Editor permission only — anyone with edit can run the function and burn your tokens.

Time to set up: 8 min. Estimated savings: $200+/mo vs paid Sheets extensions for a 10-person team.