- Create a new Google Sheets file
- Create an account on OpenAI, get its API key and paste it in the black box
- In order to use the script, you must create a Google Cloud project
- Log in to your account if you haven't already.
- Create a new GCP (Google Cloud Platform) project.
- Once created, make sure to select it
- Then activate the Google Sheet API for this GCP project
- Then you have to specify the access rights required to use the script:
- Enter all required information " * "
- And select all the "scopes" of Google Sheet
- You will then need to add users, add your Google email
- Once set up, go to the GCP project dashboard
- And copy the project ID
- Finally go back to your Google Sheet
- In the menu go to Extensions > Apps Script
- Then in the parameters of the script
- Click on "Change Project" in the Google Cloud Platform section
- Then paste the ID of your new GCP project in the field that appears
- And finally click on "Set project".
That's it.
The code to paste in Apps Script inside Google Sheets:
var API_KEY = "";
var BUSINESS_DESCRIPTION = "";
const SS = SpreadsheetApp.getActiveSpreadsheet();
const GLOBAL = SS.getSheetByName("GLOBAL_SETTINGS");
const GPT_MODEL = "text-davinci-003";
function setGlobalSettings() {
API_KEY = GLOBAL.getRange("B26").getValue();
BUSINESS_DESCRIPTION = GLOBAL.getRange("B1").getValue();
}
setGlobalSettings();
function getGPTOptions(params) {
let options = {
'method': "POST",
'contentType': 'application/json',
'headers': {
Authorization: `Bearer ${API_KEY}`
},
'payload': JSON.stringify(params)
};
return options;
}
var contentPlanSheet = SS.getSheetByName("GENERATE MONTH CONTENT PLAN");
function generateIdeas(jours){
var description = contentPlanSheet.getRange("F1").getValue();
var input = "J'ai besoin de générer 1 contenu par jour pendant "+jours+" jours sur mon compte tiktok pour mon entreprise, c'est "+description+". Peux-tu me faire un planning d'idée de contenu sur "+jours+" jours ? Sous la forme d'une liste de bullet point.";
var params = {
prompt: input,
model: GPT_MODEL,
max_tokens: 3000,
temperature: 0.5,
top_p: 1
};
var response = UrlFetchApp.fetch('https://api.openai.com/v1/completions', getGPTOptions(params));
var completion = JSON.parse(response.getContentText())['choices'][0]['text'].replace("\n\n","");
var ideas = completion.split("\n");
return ideas;
}
function generationContentPlan(){
setGlobalSettings();
var annee = contentPlanSheet.getRange("B1").getValue();
var mois = contentPlanSheet.getRange("B2").getValue();
var date = new Date(annee+"/"+mois+"/01");
var ideas = generateIdeas(new Date(annee, mois, 0).getDate());//on envoi le nombre de jour dans le mois
var firstDay = date.getDay();
var cell = contentPlanSheet.getRange("C4");
switch (firstDay) {
case 0:
cell = contentPlanSheet.getRange(4,15);
break;
case 1:
cell = contentPlanSheet.getRange(4,3);
break;
case 2:
cell = contentPlanSheet.getRange(4,5);
break;
case 3:
cell = contentPlanSheet.getRange(4,7);
break;
case 4:
cell = contentPlanSheet.getRange(4,9);
break;
case 5:
cell = contentPlanSheet.getRange(4,11);
break;
case 6:
cell = contentPlanSheet.getRange(4,13);
break;
}
Logger.log(cell);
for (var idea of ideas) {
let col = cell.getColumn();
let row = cell.getRow();
cell.setValue(idea);
if(col == 15){
cell = contentPlanSheet.getRange(row+1,3);
}else{
cell = contentPlanSheet.getRange(row,col+2);
}
}
}
function clearCalendar(){
for (var i = 4; i < 10; i = i+1) {
for (var j = 3; j <= 15; j = j+2) {
Logger.log("i = "+i);
Logger.log("j = "+j);
contentPlanSheet.getRange(i,j).setValue("");
}
}
}