-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
111 lines (92 loc) · 3.07 KB
/
Code.gs
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
// Format every link in your document using its own style
// Properties storage
const docProps = PropertiesService.getDocumentProperties();
const setDocProperties = (formObject) => {
try {
docProps.setProperties(formObject);
Logger.log(`Saved properties: ${JSON.stringify(formObject)}`);
} catch (err) {
Logger.log(`Failed with error ${err.message}`);
}
}
const getDocProperties = () => {
try {
const props = docProps.getProperties();
Logger.log(`Requested properties: ${JSON.stringify(props)}`);
return props;
} catch (err) {
Logger.log(`Failed with error ${err.message}`);
}
}
const Style = docProps.getProperties();
// Add-on logic
const setStyle = (linkText, startRange, endRange) => {
if (linkText && linkText.getType() === DocumentApp.ElementType.TEXT) {
linkText.setForegroundColor(startRange, endRange, Style.color);
linkText.setBold(startRange, endRange, (Style.bold === "true"));
linkText.setItalic(startRange, endRange, (Style.italic === "true"));
linkText.setUnderline(startRange, endRange, (Style.underline === "true"));
}
}
const setLinksStyle = (element) => {
let links = [];
element = element || DocumentApp.getActiveDocument().getBody();
if (element.getType() === DocumentApp.ElementType.TEXT) {
const textObj = element.editAsText();
const text = element.getText();
let inUrl = false;
let curUrl = {};
for (let charIndex = 0; charIndex < text.length; charIndex++) {
const url = textObj.getLinkUrl(charIndex);
if (url === null || charIndex === text.length - 1) {
if (inUrl) {
if (url != null) curUrl.endOffsetInclusive = charIndex;
curUrl.text = text.substring(curUrl.startOffset, curUrl.endOffsetInclusive + 1).trim();
setStyle(textObj, curUrl.startOffset, curUrl.endOffsetInclusive);
links.push(curUrl);
inUrl = false;
curUrl = {};
}
} else {
if (!inUrl) {
curUrl = {};
curUrl.url = url;
curUrl.startOffset = charIndex;
inUrl = true;
} else {
curUrl.endOffsetInclusive = charIndex;
}
}
}
if (links.length > 0) Logger.log(links);
} else {
if (element.getType() != DocumentApp.ElementType.HORIZONTAL_RULE & element.getType() != DocumentApp.ElementType.DATE) {
const numChildren = element.getNumChildren();
for (let i = 0; i < numChildren; i++) {
links = links.concat(setLinksStyle(element.getChild(i)));
}
}
}
}
// UI part
const showModal = () => {
const html = HtmlService.createTemplateFromFile('Config')
.evaluate();
const output = HtmlService.createHtmlOutput(html)
.setWidth(220)
.setHeight(170);
DocumentApp.getUi()
.showModalDialog(output, 'Set up your links');
}
const includeFile = (filename) => {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
const onOpen = () => {
DocumentApp.getUi()
.createAddonMenu()
.addItem('Style apply', 'setLinksStyle')
.addSeparator()
.addItem(`Style setup`, 'showModal')
.addToUi();
}