-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.gs
More file actions
94 lines (83 loc) · 2.79 KB
/
Copy pathcode.gs
File metadata and controls
94 lines (83 loc) · 2.79 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
/**
* @OnlyCurrentDoc
*/
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi();
}
function onInstall(e) {
onOpen(e);
}
function showSidebar() {
const ui = HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('Text Storage');
DocumentApp.getUi().showSidebar(ui);
}
function getSelectedText() {
const selection = DocumentApp.getActiveDocument().getSelection();
const text = [];
if (selection) {
const elements = selection.getSelectedElements();
for (let i = 0; i < elements.length; ++i) {
if (elements[i].isPartial()) {
const element = elements[i].getElement().asText();
const startIndex = elements[i].getStartOffset();
const endIndex = elements[i].getEndOffsetInclusive();
text.push(element.getText().substring(startIndex, endIndex + 1));
}
}
}
if (!text.length) throw new Error('Please select some text.');
return text;
}
function storeText(text) {
const userProperties = PropertiesService.getUserProperties();
const storedText = userProperties.getProperty('storedText');
if (storedText) {
userProperties.setProperty('storedText', storedText + '\n' + text);
} else {
userProperties.setProperty('storedText', text);
}
}
function getStoredText() {
const userProperties = PropertiesService.getUserProperties();
const storedText = userProperties.getProperty('storedText');
return storedText ? storedText.split('\n') : [];
}
function getSelectedText() {
const selection = DocumentApp.getActiveDocument().getSelection();
const text = [];
if (selection) {
const elements = selection.getSelectedElements();
for (let i = 0; i < elements.length; ++i) {
if (elements[i].isPartial()) {
const element = elements[i].getElement().asText();
const startIndex = elements[i].getStartOffset();
const endIndex = elements[i].getEndOffsetInclusive();
text.push(element.getText().substring(startIndex, endIndex + 1));
}
}
}
if (!text.length) throw new Error('Please select some text.');
return text;
}
function insertText(newText) {
const sidebarText = PropertiesService.getUserProperties().getProperty('sidebarText');
if (sidebarText) {
PropertiesService.getUserProperties().setProperty('sidebarText', sidebarText + '\n' + newText);
} else {
PropertiesService.getUserProperties().setProperty('sidebarText', newText);
}
}
function deleteStoredText(index) {
const userProperties = PropertiesService.getUserProperties();
const storedText = userProperties.getProperty('storedText');
if (storedText) {
const textArray = storedText.split('\n');
if (index >= 0 && index < textArray.length) {
textArray.splice(index, 1);
userProperties.setProperty('storedText', textArray.join('\n'));
}
}
}