Product title modification

For google merchant center and google sheets

Step #1: Copy this existing spreadsheet.

Step #2: Move this spreadsheet to a folder of your choice.

Step #3: Log into merchant center and export your products into a file. Paste them with their column titles into the products sheet without deleting the "GPT prompt" and "New title" column

Step #4: Open apps script in spreadsheet and paste the following script...

function generateNewTitles() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var apiKey = "YOUR_OPENAI_API_KEY"; // Replace with your OpenAI API key
  var model = "gpt-4"; // Use "gpt-3.5-turbo" if needed

  var AD_COLUMN_INDEX = 30; // Replace with actual column number of AD (GPT prompt)
  var AE_COLUMN_INDEX = 31; // Replace with actual column number of AE (New title)

  for (var i = 1; i < data.length; i++) { // Start from row 2 (skip headers)
    var prompt = data[i][AD_COLUMN_INDEX - 1]; // Convert to zero-based index
    var newTitleCell = sheet.getRange(i + 1, AE_COLUMN_INDEX); // Column AE (New title)

    if (prompt && newTitleCell.getValue() === "") { // Skip rows with existing values
      var response = callOpenAI(apiKey, model, prompt);
      if (response) {
        newTitleCell.setValue(response);
      }
      Utilities.sleep(2000); // Rate limit handling
    }
  }
}

function callOpenAI(apiKey, model, prompt) {
  var url = "https://api.openai.com/v1/chat/completions"; // Corrected API endpoint

  var payload = {
    model: model,
    messages: [{"role": "system", "content": "You are an assistant that generates product titles following best SEO practices."},
               {"role": "user", "content": prompt}],
    max_tokens: 250 // Adjust as needed
  };

  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 (e) {
    Logger.log("Error: " + e.toString());
    return null;
  }
}

Step #5: Run script

Last updated