How to Use GPT-4o on Google Sheets Without Any Paid Add-ons

Said Sürücü
3 min readMay 27, 2024

--

In this guide, I’ll show you how to integrate GPT-4o into Google Sheets without using any paid add-ons. This integration leverages Google Apps Script and the OpenAI API, allowing you to utilize the powerful GPT-4o model directly within your spreadsheets. Follow the steps below to set up your custom Google Sheets formula to call the GPT-4o API.

Step-by-Step Guide

1. Get Your OpenAI API Key

First, you’ll need an API key from OpenAI. If you don’t have one, sign up at OpenAI and get your API key. You can generate and manage your API keys at OpenAI API Keys. Ensure that you keep this key secure and do not share it publicly. Note that to use GPT-4o, you should ensure your API account is topped up, as these models may require a higher quota or a specific subscription plan. You can top up your account at OpenAI Billing Overview.

2. Open Google Sheets

Go to Google Sheets and create a new spreadsheet or open an existing one where you want to use the GPT-4 functionality.

3. Open Google Apps Script Editor

Click on Extensions > Apps Script to open the Apps Script editor. This is where you'll write the script to integrate with the OpenAI API.

4. Write the Script

Delete any existing code in the script editor and paste the following script. This script will define a custom function to call the GPT-4o API and handle the response.


// Custom function to call OpenAI API
function GPT4oQuery(prompt) {
var apiKey = 'YOUR_OPENAI_API_KEY'; // Replace with your OpenAI API key
var url = 'https://api.openai.com/v1/chat/completions';

var payload = {
"model": "gpt-4o", // Use the gpt-4o model
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7
};

var options = {
"method": "post",
"contentType": "application/json",
"headers": {
"Authorization": "Bearer " + apiKey
},
"payload": JSON.stringify(payload)
};

try {
var response = UrlFetchApp.fetch(url, options);
var json = JSON.parse(response.getContentText());
return json.choices[0].message.content.trim();
} catch (error) {
return 'Error: ' + error.message;
}
}

// Wrapper function for custom formula
function GPT4o(prompt, cellValue) {
var combinedPrompt = prompt + " " + cellValue;
return GPT4oQuery(combinedPrompt);
}

Replace 'YOUR_OPENAI_API_KEY' with your actual OpenAI API key.

GPT 4o-mini Update (July 2024)

OpenAI released a new model called GPT 4o-mini. It is much faster and cheaper than GPT 4o and even GPT 3.5. To use this model you just need to change this line “model”: “gpt-4o”, to“model”: “gpt-4o-mini”, and that’s it!

5. Save the Script

Click on the disk icon or press Ctrl+S to save your script.

6. Use the Custom Formula in Google Sheets

Go back to your Google Sheets. You can now use the custom function =GPT4o("your additional prompt here", A1) in any cell. For example:

=GPT4o("Tell me a joke about", A1)

Assuming cell A1 contains “cats”, the combined prompt will be “Tell me a joke about cats”. The GPT-4o API will process this prompt and return the result directly in the cell.

Benefits of This Integration

  • Cost-Effective: This solution doesn’t require any paid add-ons. You only pay for your usage of the OpenAI API based on your plan.
  • Ease of Use: You can easily integrate powerful AI capabilities into your spreadsheets without needing advanced technical knowledge.
  • Customization: You can modify the script to adjust parameters such as temperature and max tokens, tailoring the responses to your needs.

Conclusion

Integrating GPT-4o with Google Sheets is a powerful way to leverage AI directly within your workflows without needing expensive add-ons. By following the steps outlined in this guide, you can set up a custom Google Sheets formula that calls the GPT-4o API and returns responses directly in your spreadsheet.

This setup allows for a wide range of applications, from generating creative content to automating complex data analysis tasks. Start exploring the possibilities of AI in your spreadsheets today!

If you have any questions or run into issues, feel free to leave a comment below. Happy coding!

Excel Version

If you are looking for doing the same thing in Excel, now you can. Read my new article!

--

--