-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.js
More file actions
318 lines (278 loc) · 7.73 KB
/
Copy pathCode.js
File metadata and controls
318 lines (278 loc) · 7.73 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* Main entry point for Google Docs Search & Replace Add-on
* This file orchestrates all modules in the src/ directory
*/
// Note: Google Apps Script doesn't support ES6 imports
// All module functions are available globally when included in the project
/**
* Creates a menu entry in the Google Docs Add-on menu.
* This function runs automatically when the add-on is installed.
* @see src/menu.js
*/
function onInstall(e) {
onOpen(e);
}
/**
* Creates a menu entry in the Google Docs Add-on menu.
* This function runs automatically when the document is opened.
* @see src/menu.js
*/
function onOpen(e) {
DocumentApp.getUi()
.createAddonMenu()
.addItem("Open Sidebar", "showSidebar")
.addToUi();
}
/**
* Opens the sidebar in the Google Docs editor.
* @see src/ui.js
*/
function showSidebar() {
const html = HtmlService.createHtmlOutputFromFile("sidebar")
.setTitle("Search & Replace")
.setWidth(350);
DocumentApp.getUi().showSidebar(html);
}
/**
* Performs search and replace operation in the document.
* @param {string} searchText - The text to search for
* @param {string} replaceText - The text to replace with
* @return {Object} Response object with success status, message, and count
* @see src/search.js
*/
function searchAndReplace(searchText, replaceText) {
try {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
// Count occurrences before replacement
let count = 0;
let searchResult = body.findText(searchText);
while (searchResult) {
count++;
searchResult = body.findText(searchText, searchResult);
}
if (count === 0) {
return {
success: true,
message: "No occurrences found",
count: 0,
};
}
// Perform the replacement
body.replaceText(escapeRegex(searchText), replaceText);
return {
success: true,
message: `Successfully replaced ${count} occurrence(s)`,
count: count,
};
} catch (error) {
console.error("Error in searchAndReplace:", error);
return {
success: false,
message: `Error: ${error.toString()}`,
count: 0,
};
}
}
/**
* Escapes special regex characters in a string.
* @param {string} str - The string to escape
* @return {string} The escaped string
* @see src/utils.js
*/
function escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
/**
* Gets the current document content (for future features).
* @return {Object} Response with document content
* @see src/search.js
*/
function getDocumentContent() {
try {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const text = body.getText();
return {
success: true,
content: text,
wordCount: text.split(/\s+/).filter((word) => word.length > 0).length,
};
} catch (error) {
console.error("Error getting document content:", error);
return {
success: false,
message: `Error: ${error.toString()}`,
};
}
}
/**
* Finds all occurrences of a search term (for future features).
* @param {string} searchText - The text to search for
* @return {Object} Response with occurrence details
* @see src/search.js
*/
function findOccurrences(searchText) {
try {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const occurrences = [];
let searchResult = body.findText(searchText);
while (searchResult) {
const element = searchResult.getElement();
const startOffset = searchResult.getStartOffset();
const endOffset = searchResult.getEndOffsetInclusive();
occurrences.push({
text: element
.asText()
.getText()
.substring(startOffset, endOffset + 1),
context: getContext(element, startOffset, endOffset),
});
searchResult = body.findText(searchText, searchResult);
}
return {
success: true,
count: occurrences.length,
occurrences: occurrences,
};
} catch (error) {
console.error("Error finding occurrences:", error);
return {
success: false,
message: `Error: ${error.toString()}`,
};
}
}
/**
* Gets the context around a found text occurrence.
* @param {Element} element - The element containing the text
* @param {number} startOffset - Start position of the found text
* @param {number} endOffset - End position of the found text
* @return {string} The context string
* @see src/utils.js
*/
function getContext(element, startOffset, endOffset) {
const text = element.asText().getText();
const contextBefore = Math.max(0, startOffset - 20);
const contextAfter = Math.min(text.length, endOffset + 20);
return `...${text.substring(contextBefore, contextAfter)}...`;
}
/**
* Highlights all occurrences of the search text in the document.
* @param {string} searchText - The text to highlight
* @return {Object} Response object with success status, message, and count
* @see src/highlight.js
*/
function highlightText(searchText) {
try {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
// Find and highlight all occurrences
let count = 0;
let searchResult = body.findText(searchText);
while (searchResult) {
const element = searchResult.getElement();
const startOffset = searchResult.getStartOffset();
const endOffset = searchResult.getEndOffsetInclusive();
// Apply yellow highlight
if (element.editAsText) {
element
.editAsText()
.setBackgroundColor(startOffset, endOffset, "#ffff00");
}
count++;
searchResult = body.findText(searchText, searchResult);
}
if (count === 0) {
return {
success: true,
message: "No occurrences found to highlight",
count: 0,
};
}
return {
success: true,
message: `Highlighted ${count} occurrence(s)`,
count: count,
};
} catch (error) {
console.error("Error in highlightText:", error);
return {
success: false,
message: `Error: ${error.toString()}`,
count: 0,
};
}
}
/**
* Removes all highlights (yellow background) from the document.
* @return {Object} Response object with success status and message
* @see src/highlight.js
*/
function removeAllHighlights() {
try {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const text = body.editAsText();
// Remove background color from entire document
text.setBackgroundColor(null);
return {
success: true,
message: "All highlights removed",
};
} catch (error) {
console.error("Error in removeAllHighlights:", error);
return {
success: false,
message: `Error: ${error.toString()}`,
};
}
}
/**
* Removes highlights only for specific text occurrences.
* @param {string} searchText - The text to remove highlights from
* @return {Object} Response object with success status, message, and count
* @see src/highlight.js
*/
function removeHighlightForText(searchText) {
try {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
let count = 0;
let searchResult = body.findText(searchText);
while (searchResult) {
const element = searchResult.getElement();
const startOffset = searchResult.getStartOffset();
const endOffset = searchResult.getEndOffsetInclusive();
// Remove highlight (set background to null/transparent)
if (element.editAsText) {
element.editAsText().setBackgroundColor(startOffset, endOffset, null);
}
count++;
searchResult = body.findText(searchText, searchResult);
}
return {
success: true,
message: `Removed highlights from ${count} occurrence(s)`,
count: count,
};
} catch (error) {
console.error("Error in removeHighlightForText:", error);
return {
success: false,
message: `Error: ${error.toString()}`,
count: 0,
};
}
}
/**
* Shows the instruction dialog with usage guidelines.
* @see src/ui.js
*/
function showInstructionDialog() {
const html = HtmlService.createHtmlOutputFromFile("instruction-dialog")
.setWidth(500)
.setHeight(600);
DocumentApp.getUi().showModalDialog(html, "Instructions");
}