-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgoogle_docs_version.gs
More file actions
96 lines (76 loc) · 2.64 KB
/
google_docs_version.gs
File metadata and controls
96 lines (76 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function onOpen(){
var ui = DocumentApp.getUi();
ui.createMenu('My OpenAI Google Doc Functions')
.addItem('Appends Selected Text as new voice', 'ai_append_answer')
.addToUi();
}
function ai_append_answer() {
var doc = DocumentApp.getActiveDocument(); // Get the active document
var selection = doc.getSelection(); // Get the current selection
var body = doc.getBody();
// Check if there is a selection
if (selection) {
var elements = selection.getSelectedElements();
var selectedText = '';
for (var i = 0; i < elements.length; i++) {
var elementRange = elements[i];
var element = elementRange.getElement();
var elementType = element.getType();
// Handle partial selection within the element
if (elementRange.isPartial()) {
var startOffset = elementRange.getStartOffset();
var endOffset = elementRange.getEndOffsetInclusive();
if (elementType === DocumentApp.ElementType.TEXT) {
var textElement = element.asText();
// Append the selected part of the text
selectedText += textElement.getText().substring(startOffset, endOffset + 1);
}
// Optionally, handle other types of elements that can be partially selected
} else {
// For elements that are fully selected
if (elementType === DocumentApp.ElementType.PARAGRAPH) {
var paragraph = element.asParagraph();
selectedText += paragraph.getText();
}
}
// Add a newline character after each element except the last one
if (i < elements.length - 1) {
selectedText += '\n';
}
}
var correctedText = GPT("rewrite this in the voice of a makeup salesman " + selectedText);
if (correctedText) {
body.appendParagraph(correctedText);
}
}
}
function GPT(Input) {
const GPT_API = "sk-your api key";
const BASE_URL = "https://api.openai.com/v1/chat/completions";
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${GPT_API}`
};
const options = {
headers,
method: "GET",
muteHttpExceptions: true,
payload: JSON.stringify({
"model": "gpt-4-0125-preview",
"messages": [{
"role": "system",
"content": "You are a content writer for a marketing agency, OUTPUT ONLY ANSWER"
},
{
"role": "user",
"content": Input
}
],
"temperature": 0.1
})
}
const response = JSON.parse(UrlFetchApp.fetch(BASE_URL, options));
// console.log(response)
console.log(response.choices[0].message.content);
return response.choices[0].message.content
}