π Hi AI Break family! Luis and Rui here with another AI tutorial!
Imagine having the power of ChatGPT right inside your Google Sheets - yes, inside the spreadsheet itself!
In this tutorial, youβll learn exactly how to bring ChatGPT into Google Sheets using Apps Script and OpenAIβs API.
(full script at the end!)
Why Integrate ChatGPT into Google Sheets?
Rapid Content Generation: Draft emails, generate product descriptions, or brainstorm ideas directly within a cell.
Data Analysis Aid: Use ChatGPT to summarize text data, explain complex information, or assist with research queries.
Custom Solutions: Tailor prompt and response logic to your unique workflowβno more copying and pasting between browser tabs.
What Youβll Need
Google Workspace Account: Ensure you have access to Google Sheets.
OpenAI API Key: Sign up at OpenAIβs platform and retrieve your API key.
Donβt forget to add credits to your Open API account ($5 should be more than enough for testing this)
A Blank Spreadsheet: Start fresh or use an existing spreadsheet that youβd like to add ChatGPT.
Step-by-Step Tutorial
Step 1: Obtain Your OpenAI API Key
Go to the OpenAI API Keys page.
Click "Create new secret key" and copy the generated key. Keep it confidential.
Step 2: Open Your Google Sheets and Access the Script Editor
Open a new or existing Google Sheet.
In the top menu, navigate to Extensions > Apps Script. This will open the Apps Script editor in a new tab.
Step 3: Add the Custom Apps Script
In the Apps Script editor, youβll see a code window with a default
function myFunction()
. Delete it.Paste the code snippet (provided at the end of this tutorial) into the editor.
Important: Replace
"YOUR_OPENAI_API_KEY_HERE"
in the code with the API key you obtained earlier.If you want you can change the model here (in this example we are using the chatgpt 4o mini)
Step 4: Save and Deploy the Script
Click the Save icon in the toolbar, or press Ctrl+S (Windows) / Cmd+S (Mac).
This should be enough for you to be able to use the function. If not then run the script once to trigger the authorization process. If prompted, grant the script the necessary permissions.
Step 5: Using the Custom Function in Your Sheet
Return to your Google Sheet.
In any cell, type the following formula:
=GPT("Your Prompt")
Press Enter and wait a moment. The cell will populate with ChatGPTβs response.
If you want to use ChatGPT with information from other cells just add β& CELL NUMBERβ after the prompt like the example below.
And here is the result:
Step 6: Experiment with Different Prompts
Try
=GPT("Summarize the content of cellβ & A1)
to quickly summarise the information.Experiment with different topics, tones, and instructions to see what fits best into your workflow.
Donβt forget you can easily drag the formula across multiple cells in order to use ChatGPT at scale π
Step 7: Refining Your Requests
Adjust
max_tokens
to limit the length of replies.Explore additional parameters of the OpenAI API for more control over ChatGPTβs behavior.
ChatGPT Script To Copy and Paste Into GSheets
To integrate ChatGPT, simply paste the following Apps Script code into the Script Editor (which you can access through Extensions β Apps Script):
Keep reading with a 7-day free trial
Subscribe to The AI Break to keep reading this post and get 7 days of free access to the full post archives.