-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.ts
101 lines (92 loc) · 2.67 KB
/
Code.ts
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
97
98
99
100
101
const doc = DocumentApp.getActiveDocument();
const ui = DocumentApp.getUi();
const onOpen = () => {
ui.createMenu("Corrections")
.addItem("Dyslexia ", "dyslexiaText")
.addItem("Clearer English", "clearerText")
.addSeparator()
.addItem("Set API key", "setApiKey")
.addToUi();
};
const dyslexiaText = () => {
const selection = getSelectedText();
const template = DIS_ENG;
gpt(selection, template, {
prompt: template.text.replace("{{INPUT}}", selection.text),
...template.req,
});
};
const clearerText = () => {
const selection = getSelectedText();
const template = CLEARER_ENG;
gpt(selection, template, {
prompt: template.text.replace("{{INPUT}}", selection.text),
...template.req,
});
};
const testSelection = () => {
const selection = doc.getSelection();
const elem = selection.getRangeElements();
elem[elem.length - 1].getElement().asText().appendText("\nHello world");
};
const getSelectedText = () => {
const selection = doc.getSelection();
if (!selection) {
throw new Error("No text was selected");
}
let total = "";
let selectedElements = selection.getRangeElements();
if (selection) {
for (let i = 0; i < selectedElements.length; i++) {
const selElem = selectedElements[i];
const el = selElem.getElement();
const isPartial = selElem.isPartial();
let selStart: number;
let selEnd: number;
if (isPartial) {
selStart = selElem.getStartOffset();
selEnd = selElem.getEndOffsetInclusive();
} else {
selStart = selElem.getStartOffset();
selEnd = selElem.getEndOffsetInclusive();
}
const elType = el.getType();
let txt = "";
if (elType == DocumentApp.ElementType.TEXT) {
txt = selElem
.getElement()
.asText()
.getText()
.slice(selStart, selEnd + 1);
}
if (elType == DocumentApp.ElementType.PARAGRAPH) {
txt = selElem.getElement().asParagraph().getText();
}
Logger.log(selElem.getEndOffsetInclusive());
total += txt;
}
}
return {
text: total.trim(),
lastElem: selectedElements[selectedElements.length - 1].getElement(),
};
};
const setApiKey = () => {
PropertiesService.getDocumentProperties().setProperty(
"apiKey",
createPrompt("Your API key")
);
};
const getApiKey = () => {
const key = PropertiesService.getDocumentProperties().getProperty("apiKey");
if (!key) throw new Error("No API key");
return key;
};
const createPrompt = (title: string): string | null => {
const prompt = ui.prompt(title);
if (prompt.getSelectedButton() == ui.Button.OK) {
return prompt.getResponseText();
} else {
return null;
}
};