{"id":52680,"date":"2025-03-12T14:00:00","date_gmt":"2025-03-12T13:00:00","guid":{"rendered":"https:\/\/blog.sheetgo.com\/?p=52680"},"modified":"2025-06-03T22:06:53","modified_gmt":"2025-06-03T20:06:53","slug":"integrar-chatgpt-con-google-sheets","status":"publish","type":"post","link":"https:\/\/www.sheetgo.com\/es\/blog\/google-sheets-features\/integrate-chatgpt-with-google-sheets\/","title":{"rendered":"Integra ChatGPT con Google Sheets para obtener informaci\u00f3n automatizada sobre los datos"},"content":{"rendered":"\n[et_pb_section fb_built=&#8221;1&#8243; theme_builder_area=&#8221;post_content&#8221; _builder_version=&#8221;4.27.2&#8243; _module_preset=&#8221;default&#8221;][et_pb_row _builder_version=&#8221;4.27.2&#8243; _module_preset=&#8221;default&#8221; theme_builder_area=&#8221;post_content&#8221;][et_pb_column _builder_version=&#8221;4.27.2&#8243; _module_preset=&#8221;default&#8221; type=&#8221;4_4&#8243; theme_builder_area=&#8221;post_content&#8221;][et_pb_text _builder_version=&#8221;4.27.2&#8243; _module_preset=&#8221;default&#8221; theme_builder_area=&#8221;post_content&#8221; hover_enabled=&#8221;0&#8243; sticky_enabled=&#8221;0&#8243;]<blockquote>\n<p><strong data-start=\"47\" data-end=\"56\">Note:<\/strong> This post was originally published in our community forum.<\/p>\n<\/blockquote>\n<p>I recently worked on an exciting project where I integrated Google Sheets with ChatGPT. This integration allows you to generate AI-driven insights directly from your spreadsheet data. If you\u2019re looking to automate report generation or data analysis, this guide is for you!<\/p>\n<h4><strong>Why Should You Integrate Google Sheets with ChatGPT?<\/strong><\/h4>\n<p><strong>By connecting Google Sheets to ChatGPT, you can:<\/strong><\/p>\n<ul>\n<li>Automate the generation of summaries and insights based on your data.<\/li>\n<li>Simplify complex data analysis tasks.<\/li>\n<li>Save time by directly fetching AI-generated responses into your spreadsheet.<\/li>\n<\/ul>\n<h4>What You\u2019ll Need:<\/h4>\n<ul>\n<li><strong>Google Sheets<\/strong>: A spreadsheet with your data.<\/li>\n<li><strong>OpenAI API Key<\/strong>: You\u2019ll need an API key from OpenAI to connect Google Sheets with ChatGPT.<\/li>\n<\/ul>\n<h3>Step-by-Step Guide<\/h3>\n<h4>Step 1: Setting Up Your Google Sheet<\/h4>\n<p>Start by organizing your data and preparing your prompts.<\/p>\n<ol>\n<li>Create a \u2018Sales Data\u2019 Tab:\n<ul>\n<li>This tab will hold your data. Here\u2019s an example of what it might look like:<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><a href=\"https:\/\/i.imgur.com\/vVAlehn.png\" rel=\"ugc noopener nofollow\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/vVAlehn.png\" title=\"\" alt=\"\" \/><\/a><\/p>\n<ol>\n<li>Create a \u2018Summary\u2019 Tab:\n<ul>\n<li>This is where you\u2019ll generate your prompts and display the AI responses.\n<p>Example<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>In cell A1, write: plaintext<\/strong><\/p>\n<p><a href=\"https:\/\/i.imgur.com\/B4n6mcQ.png\" rel=\"ugc noopener nofollow\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/B4n6mcQ.png\" title=\"\" alt=\"\" \/><\/a><\/p>\n<h4>Step 3: Connecting Google Sheets to ChatGPT Using Google Apps Script<\/h4>\n<p>Now, let\u2019s automate the process by sending your prompt to ChatGPT and fetching the response.<\/p>\n<ol>\n<li><strong>Google Apps Script Example<\/strong>:\n<ul>\n<li>Open Google Apps Script from your Google Sheet (<code>Extensions &gt; Apps Script<\/code>).<\/li>\n<li>Paste the following script:\n<p><code>function getChatGPTResponse() {<\/code><\/p>\n<p><code>var prompt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary').getRange('A1').getValue();<\/code><\/p>\n<p><code>var apiKey = 'YOUR_OPENAI_API_KEY';<\/code><\/p>\n<p><code>var url = 'https:\/\/api.openai.com\/v1\/chat\/completions';<\/code><\/p>\n<p><code>var payload = {<\/code><\/p>\n<p><code>'model': 'gpt-3.5-turbo', \/\/ Ensure you're using a valid model<\/code><\/p>\n<p><code>'messages': [{'role': 'user', 'content': prompt}],<\/code><\/p>\n<p><code>'max_tokens': 150<\/code><\/p>\n<p><code>};<\/code><\/p>\n<p><code>var options = {<\/code><\/p>\n<p><code>'method' : 'post',<\/code><\/p>\n<p><code>'contentType': 'application\/json',<\/code><\/p>\n<p><code>'headers': {<\/code><\/p>\n<p><code>'Authorization': 'Bearer ' + apiKey<\/code><\/p>\n<p><code>},<\/code><\/p>\n<p><code>'payload' : JSON.stringify(payload),<\/code><\/p>\n<p><code>'muteHttpExceptions': true \/\/ Enables detailed error responses<\/code><\/p>\n<p><code>};<\/code><\/p>\n<p><code><\/code><\/p>\n<p><code>\/\/ Fetch response from the API<\/code><\/p>\n<p><code>var response = UrlFetchApp.fetch(url, options);<\/code><\/p>\n<p><code>var json = response.getContentText();<\/code><\/p>\n<p><code><\/code><\/p>\n<p><code>Logger.log(json); \/\/ Log the full response for debugging<\/code><\/p>\n<p><code><\/code><\/p>\n<p><code>var parsedJson = JSON.parse(json);<\/code><\/p>\n<p><code><\/code><\/p>\n<p><code>if (parsedJson.choices &amp;&amp; parsedJson.choices.length &gt; 0) {<\/code><\/p>\n<p><code>var chatGPTOutput = parsedJson.choices[0].message.content.trim();<\/code><\/p>\n<p><code>SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary').getRange('B1').setValue(chatGPTOutput);<\/code><\/p>\n<p><code>} else {<\/code><\/p>\n<p><code>var errorMessage = \"Error: Unexpected API response structure. Full response: \" + json;<\/code><\/p>\n<p><code>SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary').getRange('B1').setValue(errorMessage);<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p><code>}<\/code><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong><em>Replace<span>\u00a0<\/span><code>'YOUR_OPENAI_API_KEY'<\/code><span>\u00a0<\/span>with your actual OpenAI API key.<\/em><\/strong><br \/><a href=\"https:\/\/i.imgur.com\/SQfBA72.png\" rel=\"ugc noopener nofollow\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/SQfBA72.png\" title=\"\" alt=\"\" \/><\/a><\/p>\n<ol>\n<li><strong>Running the Script<\/strong>:\n<ul>\n<li>Save the script and run the<span>\u00a0<\/span><code>getChatGPTResponse<\/code><span>\u00a0<\/span>function.<\/li>\n<li>This script will take the prompt from cell<span>\u00a0<\/span><code>A1<\/code><span>\u00a0<\/span>of the \u2018Summary\u2019 tab, send it to ChatGPT, and place the response in cell<span>\u00a0<\/span><code>B1<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h4>Step 4: Example Output<\/h4>\n<p>Here\u2019s how your \u2018Summary\u2019 tab might look after running the script:<\/p>\n<p><a href=\"https:\/\/i.imgur.com\/kBKWCfp.png\" rel=\"ugc noopener nofollow\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/i.imgur.com\/kBKWCfp.png\" title=\"\" alt=\"\" \/><\/a><\/p>\n<p>In this example, the prompt in<span>\u00a0<\/span><code>A1<\/code><span>\u00a0<\/span>was processed by ChatGPT, and the response in<span>\u00a0<\/span><code>B1<\/code><span>\u00a0<\/span>provides a summary based on the data from the \u2018Sales Data\u2019 tab.<\/p>\n<h4>Step 5: Troubleshooting Common Issues<\/h4>\n<ol>\n<li><strong>404 Error<\/strong>:\n<ul>\n<li>If you encounter a<span>\u00a0<\/span><code>404 error<\/code>, it might be due to using a deprecated model. Ensure you\u2019re using a valid and supported model like<span>\u00a0<\/span><code>gpt-3.5-turbo<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>TypeError<\/strong>:\n<ul>\n<li>If you see<span>\u00a0<\/span><code>TypeError: Cannot read properties of undefined (reading '0')<\/code>, it means the response structure isn\u2019t as expected. The script now logs the full response so you can debug it more easily.<\/li>\n<\/ul>\n<\/li>\n<\/ol>[\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]\n","protected":false},"excerpt":{"rendered":"<p>Note: This post was originally published in our community forum. I recently worked on an exciting project where I integrated Google Sheets with ChatGPT. This integration allows you to generate AI-driven insights directly from your spreadsheet data. If you\u2019re looking to automate report generation or data analysis, this guide is for you! Why Should You [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":44470,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[37],"tags":[],"class_list":["post-52680","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-google-sheets-features"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/posts\/52680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/comments?post=52680"}],"version-history":[{"count":0,"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/posts\/52680\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/media\/44470"}],"wp:attachment":[{"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/media?parent=52680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/categories?post=52680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sheetgo.com\/es\/wp-json\/wp\/v2\/tags?post=52680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}