If you run an e-shop or a product catalogue, you know all too well how time-consuming it is to write attractive, consistent descriptions for every single item. Automating that process with artificial intelligence can save you a great deal of time and make you far more efficient. This tutorial shows you how to use OpenAI’s ChatGPT to generate descriptions automatically, right inside Google Sheets. The way I do it myself is to import into Google Sheets a list of products that already have the following values filled in: product code, name, short description and long description, which we usually get from a supplier feed. We then use all of that information to generate unique product descriptions exactly the way we want them.
What you will need
- A Google account to access Google Sheets.
- An API key from OpenAI, which gives you access to their AI models.
Step 1: Create the Google Sheet
- Open Google Sheets:
- Go to Google Sheets and sign in with your Google account.
- Click “Blank” to create a new spreadsheet.
- Set up the columns:
- Name the first row with the following column headers:
product_codenameshort_descriptionlong_descriptiongenerated_description
Step 2: Get an API key from OpenAI
- Sign up and log in:
- Go to OpenAI and register, or log in to your existing account.
- Create the API key:
- Once you are logged in, head to the API keys section of your dashboard. You will find it at https://platform.openai.com/api-keys
- Click “Create new secret key”.
- Copy your API key and keep it somewhere safe.
Step 3: Install the script in Google Sheets
- Open the script editor:
- In your Google Sheet click
Extensionsand thenApps Script.
- Paste in the script:
- In the script editor, delete any existing code and paste in the following script:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('OpenAI')
.addItem('Generate descriptions', 'generateDescriptions')
.addToUi();
}
function generateDescriptions() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var apiKey = 'OPEN_AI_API_KEY';
var apiUrl = 'https://api.openai.com/v1/chat/completions';
for (var i = 1; i < data.length; i++) {
var title = data[i][3]; // Title
var shortDescription = data[i][4]; // Short description
var longDescription = data[i][5]; // Long description
if (title && shortDescription && longDescription) {
var prompt = `Title: ${title}nShort description: ${shortDescription}nLong description: ${longDescription}nnGenerated description:`;
var payload = {
"model": "gpt-4o",
"messages": [{"role": "system", "content": "Write the product description in English, ............"},
{"role": "user", "content": prompt}],
"max_tokens": 1000,
"temperature": 0.7
};
var options = {
"method" : "post",
"contentType": "application/json",
"headers": {
"Authorization": "Bearer " + apiKey
},
"payload" : JSON.stringify(payload)
};
try {
var response = UrlFetchApp.fetch(apiUrl, options);
var json = JSON.parse(response.getContentText());
var generatedDescription = json.choices[0].message.content.trim();
sheet.getRange(i + 1, 7).setValue(generatedDescription); // Column for the generated description
} catch (e) {
Logger.log(e.toString());
}
}
}
}
- Replace the API key:
- Replace
'YOUR_OPENAI_API_KEY'in the script with the real API key you got from OpenAI.
- Save and run the script:
- Save the script by clicking
File>Save. - Close the script editor.
Step 4: Generate the descriptions
- Open the OpenAI menu:
- A new
OpenAImenu should now have appeared in Google Sheets. - Click
OpenAIand thenGenerate descriptions.
- Automatic description generation:
- The script starts walking through the individual rows in your Google Sheet, takes the data from the relevant cells and generates a new description, which it writes into the
generated_descriptioncolumn.
Congratulations! You have successfully set up a Google Sheet that generates product descriptions automatically using OpenAI GPT-4o. This process can save you hours of work and make sure your descriptions are consistent and well written.
Customising the script
As it stands, the script talks to the API of the latest model, ChatGPT-4o. You can of course swap the model out; the list of available models is on this page.
You can also play with the prompt: you might specify the style in which the model should write the descriptions, what role you want to assign to it, whether it should use HTML tags, bullet points, headings, subheadings and so on.
By default the script is set to write descriptions of at most 1000 tokens (you can check how much text 1000 tokens amounts to in this calculator). You can also set the level of creativity (temperature). I explain what temperature means in plain language in this article.
If you add another column or change the order of the columns, you will have to adjust the indexes in the code that determine where the data sits. That comes in handy if your export contains further columns you want to use, such as product parameters.
- Indexes in the code: Indexes start at 0. For example
data[i][0]refers to the first column,data[i][1]to the second, and so on. - Adjusting the indexes: If you add a new column at the beginning, you will have to shift every index one place to the right. If you insert the new column between
product_codeandname, for instance, the new code will look like this:
var code = data[i][0]; // Product code
var newColumn = data[i][1]; // New column
var title = data[i][2]; // Name
var shortDescription = data[i][3]; // Short description
var longDescription = data[i][4]; // Long description
Do not forget to shift the column for the generated content as well. The script can obviously be modified to suit your needs. It can serve, for example, to generate parameters out of parameters, and with a small change you can use it for translations. It all depends on what you need and how far your imagination stretches…
If you are curious about how you could use AI tools in other practical situations, do not hesitate to get in touch, or book one-to-one training, or take a look at whether there are any open dates for subsidised group AI training (courses are delivered in Czech). And if there are none, just drop me a line and we will find a date.
