-
Notifications
You must be signed in to change notification settings - Fork 30
/
code.gs
168 lines (137 loc) · 4.73 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
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
/**
* @OnlyCurrentDoc
*/
function onOpen() {
DocumentApp.getUi().createMenu('Heading Tools')
.addItem('Add Headings Numbers', 'numberHeadingsAdd')
.addItem('Remove Heading Numbers', 'numberHeadingsRemove')
.addItem('Promote Headings (H1➡Title ... H6➡H5)', 'increaseHeadingLevels')
.addItem('Demote Headings (Title➡Title, H1➡H2 ... H6➡Normal)', 'decreaseHeadingLevels')
.addSeparator()
.addItem('Add Heading Numbers... (asks which)', 'numberHeadingsAddSkipping')
.addItem('Remove Heading Numbers... (asks which)', 'numberHeadingsRemoveSkipping')
.addItem('Promote Headings (H1➡Title ... H6➡H5)... (asks which)', 'increaseHeadingLevelsSkipping')
.addItem('Demote Headings (Title➡Title, H1➡H2 ... H6➡Normal)... (asks which)', 'decreaseHeadingLevelsSkipping')
.addSeparator()
.addToUi();
}
function numberHeadingsAdd() {
numberHeadings(true);
}
function numberHeadingsAddSkipping() {
numberHeadings(true, true);
}
function numberHeadingsRemove() {
numberHeadings(false);
}
function numberHeadingsRemoveSkipping() {
numberHeadings(false, true);
}
function increaseHeadingLevels() {
changeHeadingLevels("up")
}
function decreaseHeadingLevels() {
changeHeadingLevels("down")
}
function increaseHeadingLevelsSkipping() {
changeHeadingLevels("up", true)
}
function decreaseHeadingLevelsSkipping() {
changeHeadingLevels("down", true)
}
function skipHeadingsPrompt() {
let ui = DocumentApp.getUi()
let userResponse = ui.prompt('What heading levels to process? (numbers)', ui.ButtonSet.OK_CANCEL)
let userText = userResponse.getResponseText()
let userClick = userResponse.getSelectedButton()
if (userClick == ui.Button.CANCEL || userClick == ui.Button.CLOSE)
return false
if (userText.match(/^[1-6,; e and y-]+$/) == null)
return false
userText = userText.replace(/\D/g, '')
return eval('/HEADING[' + userText + ']/')
}
function numberHeadings(add = false, skipHeadings = false) {
let document = DocumentApp.getActiveDocument();
let paragraphs = document.getParagraphs();
let numbers = [0, 0, 0, 0, 0, 0, 0];
let headingsToProcessRegex = /HEADING\d/
if (skipHeadings) {
let userResponse = skipHeadingsPrompt()
if (userResponse === false)
return
headingsToProcessRegex = userResponse
}
for (let i in paragraphs) {
let element = paragraphs[i];
let text = element.getText() + '';
let type = element.getHeading() + '';
// exclude everything but headings
if (!type.match(headingsToProcessRegex)) {
continue;
}
// exclude empty headings (e.g. page breaks generate these)
if (text.match(/^\s*$/)) {
continue;
}
element.replaceText("^[0-9]+(\\.[0-9]+)*\\. ", "")
if (add == true) {
let level = new RegExp(/HEADING(\d)/).exec(type)[1];
let numbering = '';
numbers[level]++;
for (let currentLevel = 1; currentLevel <= 6; currentLevel++) {
if (currentLevel <= level) {
numbering += numbers[currentLevel] + '.';
} else {
numbers[currentLevel] = 0;
}
}
element.insertText(0, numbering + ' ')
}
}
}
function changeHeadingLevels(direction = '', skipHeadings = false) {
let body = DocumentApp.getActiveDocument().getBody()
let paragraphs = body.getParagraphs();
let headingsToProcessRegex = /HEADING\d/
if (skipHeadings) {
let userResponse = skipHeadingsPrompt()
if (userResponse === false)
return
headingsToProcessRegex = userResponse
}
for (let i in paragraphs) {
let current_paragraph = paragraphs[i];
let text = current_paragraph.getText() + '';
let type = current_paragraph.getHeading() + '';
// exclude everything but headings
if (!type.match(headingsToProcessRegex)) {
continue;
}
// exclude empty headings (e.g. page breaks generate these)
if (text.match(/^\s*$/)) {
continue;
}
console.log(type)
// as integer
let currentLevel = new RegExp(/HEADING(\d)/).exec(type)[1] * 1;
let problemCurrentLevel = 6
let newLevel = currentLevel + 1
let problemLevelFix = "NORMAL"
if (direction == "up") {
problemCurrentLevel = 1
newLevel = currentLevel - 1
problemLevelFix = "TITLE"
}
let newHeadingLevel = eval("DocumentApp.ParagraphHeading.HEADING" + newLevel)
if (currentLevel == problemCurrentLevel) {
newHeadingLevel = eval("DocumentApp.ParagraphHeading." + problemLevelFix)
}
let style = {};
style[DocumentApp.Attribute.HEADING] = newHeadingLevel
let curr_para_id = body.getChildIndex(current_paragraph)
let new_paragraph = current_paragraph.copy().setText(" ")
body.insertParagraph(curr_para_id + 1, new_paragraph).setAttributes(style).merge()
// current_paragraph.setAttributes(style)
}
}