How to Integrate the GPT-4o AI Model into Google Sheets

How to Integrate the GPT-4o AI Model into Google Sheets

Artificial intelligence and its capabilities are constantly evolving. The new GPT-4o is a powerful natural language processing model developed by OpenAI, capable of generating text, answering questions, performing assessments, and more. In this article, we will discuss how to integrate the GPT-4o AI model into Google Sheets and how it can help automate and improve data management processes.


By integrating GPT-4o into Google Sheets, you can:

  • Automate data analysis and processing;
  • Generate texts and reports directly from data;
  • Quickly get answers to questions about the data;
  • Improve productivity and reduce the amount of manual work.


Before starting the integration, you will need:

  • A Google account to use Google Sheets;
  • An OpenAI account to get the API key for using the GPT-4o model;
  • Basic programming skills.


Integration steps:

1. Create a new Google Sheets document at: https://docs.google.com/spreadsheets/u/0/.



2. Click on "Extensions" -> "App Script".

 


3. In the window that opens, enter the following code:

const OPENAI_API_KEY = 'YOUR_API_KEY';
function GPT4O(query, cell) {
  const url = 'https://api.openai.com/v1/chat/completions';
  const payload = {
    model: 'gpt-4o',
    temperature: 0.8,
    messages: [
      {"role": "user", "content": cell +' - '+ query}
    ],
    max_tokens: 150
  };

  const options = {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${OPENAI_API_KEY}`,
      'Content-Type': 'application/json'
    },
    payload: JSON.stringify(payload)
  };

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


4. Replace 'YOUR_API_KEY' with your actual API key, which you can get at: https://platform.openai.com/.


5. Rename the project to a clearer name.

 


6. In the top right corner, click "Deploy" and select "Test deployments".



7. In the window that opens, click the gear icon and select "Editor Add-on".



8. Click "Create new test".



9. In the "Editor Add-on" settings, select "Installed and Enabled" in the "Config" section, and in the "Test document" section, select the newly created document and click "Save test".

 

10. Click "Done".

 


11. Return to the newly created Google Sheets document. To use the GPT-4o model, use the GPT4O function created in step 3, with two parameters: the query for the AI and the selected Google Sheets cell (e.g., =GPT4O("Write the value in words", A1)).

 


Integrating GPT-4o into Google Sheets can significantly improve your productivity, automate tasks, or detect data anomalies. Whether you need to perform complex data analysis, generate texts, or get answers to questions, GPT-4o can become a valuable tool in your workflow.