Google Sheet and OpenAI to create bulk post

Status
Not open for further replies.

ICT Lover

Junior Member
Joined
Jan 8, 2024
Messages
190
Reaction score
112
Providing a script for generating multiple articles through the integration of Google Sheets and OpenAI.

Prerequisites:

  • Access to Google Sheets
  • Input your keywords
  • OpenAI account with a valid payment card linked to it.

Step 1.

Go to sheets.
create a new excel sheets
open the extensions tab
select Apps Script

1709289218115.png



Step 2.
Delete all scripts which are present there and add the following code.

Code:
// Replace 'your-openai-api-key' with your actual OpenAI API key
var openai_api_key = 'sk-fvqRkxxxxxxxxxxxxxxxxxxxxxxxxxxx';

function callOpenAI() {
  var url = 'https://api.openai.com/v1/engines/text-davinci-002/completions';
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 
  var prompts = [
    'Write an introduction like a first person. give it nuance, do not use AI words. write 200 words minimum for an article titled: ',
    'Write the full article like a expert content writer. use headings and explain give point if needed. do not use AI words. minimum 1500 words. output should be in markup language for an article titled: ',
    'Write a conclusion for an article titled: ',
    'Generate some FAQs for an article titled: '
  ];

  // Iterate over the cells
  for (var i = 1; i <= 30; i++) {
    var title = sheet.getRange(i, 1).getValue(); // Get the title from column A (index 1)

    for (var j = 0; j < prompts.length; j++) {
      var options = {
        'method' : 'post',
        'headers': {
          'Authorization': 'Bearer ' + openai_api_key,
          'Content-Type': 'application/json'
        },
        'payload' : JSON.stringify({
          'prompt': prompts[j] + title, // Combine the prompt with the article title
          'max_tokens': 1500 // Adjust this value to get the required length of the content
        }),
        'muteHttpExceptions': true
      };
   
      var response = UrlFetchApp.fetch(url, options);
      var data = JSON.parse(response.getContentText());

      if (data && data.choices && data.choices.length > 0 && data.choices[0].text) {
        // Set the content value in column B, C, D, or E (indexes 2, 3, 4, or 5)
        sheet.getRange(i, j + 2).setValue(data.choices[0].text.trim());
      } else {
        Logger.log("No choices found or error in API call for the prompt at row " + i + ", column " + (j + 2));
        Logger.log("Response: " + JSON.stringify(data));
      }
   
      // Add a sleep to prevent rate limit issues
      Utilities.sleep(1000);
    }
  }
}

Replace the code with your open ai code.
Feel free to change the prompt.

Step 3:

Navigate to Google Sheets.
Paste your keywords into column A cells.
You can generate up to 30 posts (as tested) within 5 minutes. Therefore, it's advisable to start with the first 30 keywords listed in the Google sheet.

Enjoy
 
My man is giving secret for free
1490886827-risibo.png
 
Is this working. I tried Something with davinci 3 and it grave me error.
 
Providing a script for generating multiple articles through the integration of Google Sheets and OpenAI.

Prerequisites:

  • Access to Google Sheets
  • Input your keywords
  • OpenAI account with a valid payment card linked to it.

Step 1.

Go to sheets.
create a new excel sheets
open the extensions tab
select Apps Script

View attachment 325818



Step 2.
Delete all scripts which are present there and add the following code.

Code:
// Replace 'your-openai-api-key' with your actual OpenAI API key
var openai_api_key = 'sk-fvqRkxxxxxxxxxxxxxxxxxxxxxxxxxxx';

function callOpenAI() {
  var url = 'https://api.openai.com/v1/engines/text-davinci-002/completions';
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 
  var prompts = [
    'Write an introduction like a first person. give it nuance, do not use AI words. write 200 words minimum for an article titled: ',
    'Write the full article like a expert content writer. use headings and explain give point if needed. do not use AI words. minimum 1500 words. output should be in markup language for an article titled: ',
    'Write a conclusion for an article titled: ',
    'Generate some FAQs for an article titled: '
  ];

  // Iterate over the cells
  for (var i = 1; i <= 30; i++) {
    var title = sheet.getRange(i, 1).getValue(); // Get the title from column A (index 1)

    for (var j = 0; j < prompts.length; j++) {
      var options = {
        'method' : 'post',
        'headers': {
          'Authorization': 'Bearer ' + openai_api_key,
          'Content-Type': 'application/json'
        },
        'payload' : JSON.stringify({
          'prompt': prompts[j] + title, // Combine the prompt with the article title
          'max_tokens': 1500 // Adjust this value to get the required length of the content
        }),
        'muteHttpExceptions': true
      };
  
      var response = UrlFetchApp.fetch(url, options);
      var data = JSON.parse(response.getContentText());

      if (data && data.choices && data.choices.length > 0 && data.choices[0].text) {
        // Set the content value in column B, C, D, or E (indexes 2, 3, 4, or 5)
        sheet.getRange(i, j + 2).setValue(data.choices[0].text.trim());
      } else {
        Logger.log("No choices found or error in API call for the prompt at row " + i + ", column " + (j + 2));
        Logger.log("Response: " + JSON.stringify(data));
      }
  
      // Add a sleep to prevent rate limit issues
      Utilities.sleep(1000);
    }
  }
}

Replace the code with your open ai code.
Feel free to change the prompt.

Step 3:

Navigate to Google Sheets.
Paste your keywords into column A cells.
You can generate up to 30 posts (as tested) within 5 minutes. Therefore, it's advisable to start with the first 30 keywords listed in the Google sheet.

Enjoy

interesting!


Thanks for the share!
 
Providing a script for generating multiple articles through the integration of Google Sheets and OpenAI.

Prerequisites:

  • Access to Google Sheets
  • Input your keywords
  • OpenAI account with a valid payment card linked to it.

Step 1.

Go to sheets.
create a new excel sheets
open the extensions tab
select Apps Script

View attachment 325818



Step 2.
Delete all scripts which are present there and add the following code.

Code:
// Replace 'your-openai-api-key' with your actual OpenAI API key
var openai_api_key = 'sk-fvqRkxxxxxxxxxxxxxxxxxxxxxxxxxxx';

function callOpenAI() {
  var url = 'https://api.openai.com/v1/engines/text-davinci-002/completions';
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 
  var prompts = [
    'Write an introduction like a first person. give it nuance, do not use AI words. write 200 words minimum for an article titled: ',
    'Write the full article like a expert content writer. use headings and explain give point if needed. do not use AI words. minimum 1500 words. output should be in markup language for an article titled: ',
    'Write a conclusion for an article titled: ',
    'Generate some FAQs for an article titled: '
  ];

  // Iterate over the cells
  for (var i = 1; i <= 30; i++) {
    var title = sheet.getRange(i, 1).getValue(); // Get the title from column A (index 1)

    for (var j = 0; j < prompts.length; j++) {
      var options = {
        'method' : 'post',
        'headers': {
          'Authorization': 'Bearer ' + openai_api_key,
          'Content-Type': 'application/json'
        },
        'payload' : JSON.stringify({
          'prompt': prompts[j] + title, // Combine the prompt with the article title
          'max_tokens': 1500 // Adjust this value to get the required length of the content
        }),
        'muteHttpExceptions': true
      };
  
      var response = UrlFetchApp.fetch(url, options);
      var data = JSON.parse(response.getContentText());

      if (data && data.choices && data.choices.length > 0 && data.choices[0].text) {
        // Set the content value in column B, C, D, or E (indexes 2, 3, 4, or 5)
        sheet.getRange(i, j + 2).setValue(data.choices[0].text.trim());
      } else {
        Logger.log("No choices found or error in API call for the prompt at row " + i + ", column " + (j + 2));
        Logger.log("Response: " + JSON.stringify(data));
      }
  
      // Add a sleep to prevent rate limit issues
      Utilities.sleep(1000);
    }
  }
}

Replace the code with your open ai code.
Feel free to change the prompt.

Step 3:

Navigate to Google Sheets.
Paste your keywords into column A cells.
You can generate up to 30 posts (as tested) within 5 minutes. Therefore, it's advisable to start with the first 30 keywords listed in the Google sheet.

Enjoy
Thank you for Sharing :)
 
Status
Not open for further replies.
Back
Top