-
Notifications
You must be signed in to change notification settings - Fork 0
/
headingwordcounter.gs
executable file
·282 lines (237 loc) · 9.1 KB
/
headingwordcounter.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
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
// Script to create section word/character counts in document headings
// Author: Ben Coleman (ben[dot]coleman[at]alum[dot]utoronto[dot]ca)
// Github repository: https://github.com/tallcoleman/heading-word-counter
// GLOBAL ALIASES
const raw = String.raw;
let documentProperties = PropertiesService.getDocumentProperties();
// PREFERENCES
// text highlight colors in hex format
// under the word/character count (default: pastel green)
const underColor = '#b6d7a8';
// close to the word/character count (default: pastel yellow)
const closeColor = '#ffe599';
// over the word/character count (default: pastel red)
const overColor = '#ea9999';
// threshold for close colour ('exact' is used for longer sections)
// 'exact' is multiplied by average word length for character counts
const thresholdUnits = {
proportion: .1, // % of words left
exact: 20, // number of words left
}
const averageWordLength = 5;
// text matching
// needs to be in a format that works in a regular expression
const textEncapsulators = [raw`\(`, raw`\)`]; // default '(' and ')'
const textSeparators = [raw`\/`]; // default '/'
const textWordLimitTokens = ['w', 'words'];
const textCharLimitTokens = ['c', 'char', 'chars'];
const textMaxLimitTokens = ['max', 'maximum'];
// trigger timeout
const triggerTimeOut = 30; // number of minutes
// SETUP
// run this to create onOpen trigger
function createOnOpenTrigger() {
ScriptApp.newTrigger('onOpenActions')
.forDocument(DocumentApp.getActiveDocument())
.onOpen()
.create()
// run actions if document already open
onOpenActions();
}
// INITIALIZATION
// create regex string
let reComponents = [
`(?<unitCountPrefix>` +
textEncapsulators[0],
`)` +
raw`(?<unitCount>\d+)??` +
`(?<unitCountSuffix>`,
`(?<trailingSeparator>${textSeparators.join('|')})?`,
`(?:` +
raw`(?<wordLimit>\d+)`,
`(?:${textWordLimitTokens.join('|')})` +
`|` +
raw`(?<charLimit>\d+)`,
`(?:${textCharLimitTokens.join('|')})` +
`)`,
`(?:` +
`(?:${textSeparators.join('|')})`,
raw`(?<maxLimit>\d+)`,
`(?:${textMaxLimitTokens.join('|')})` +
`)?`,
textEncapsulators[1] +
`)`
];
const reIndicator = new RegExp(reComponents.join(raw`\s*?`));
// MAIN FUNCTIONS
// Iterate through paragraphs
function parProcess(currentNode, runningCount = {words: 0, chars: 0}, previousHeading) {
// skips tables
if (currentNode.getHeading) {
// headings
if (currentNode.getHeading() != 'NORMAL') {
if (previousHeading) headingUpdate(previousHeading, runningCount);
runningCount = {words: 0, chars: 0};
previousHeading = currentNode;
// normal paragraphs
} else if (currentNode.getHeading() == 'NORMAL') {
runningCount.words += countWords(currentNode);
runningCount.chars += currentNode.getText().length;
}
// reset unit count at horizontal rules
if (currentNode.findElement(DocumentApp.ElementType.HORIZONTAL_RULE)) {
runningCount = {words: 0, chars: 0};
}
}
// stop if at end of doc
if (currentNode.isAtDocumentEnd() || !currentNode.getNextSibling()) {
headingUpdate(previousHeading, runningCount);
} else {
return parProcess(currentNode.getNextSibling(), runningCount, previousHeading);
}
return;
}
// Count words - from Jed Grant (source: https://stackoverflow.com/questions/33338667/function-for-word-count-in-google-docs-apps-script)
function countWords(p) {
var s = p.getText();
// check for empty nodes
if (s.length === 0)
return 0;
// A simple \n replacement didn't work, neither did \s not sure why
s = s.replace(/\r\n|\r|\n/g, " ");
// In cases where you have "...last word.First word..."
// it doesn't count the two words around the period.
// so I replace all punctuation with a space
var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\_`~()"?“”…]/g," ");
// Finally, trim it down to single spaces (not sure this even matters)
var finalString = punctuationless.replace(/\s{2,}/g," ");
// Actually count it
var count = finalString.trim().split(/\s+/).length;
return count;
}
// Update and style headings
function headingUpdate(heading, counts) {
// extract indicator values
let matches = reIndicator.exec(heading.getText());
if (!matches) return;
// indicator is only valid if it specifies a unit maximum
let unitType, unitLimit, unitCount;
if (matches.groups.wordLimit) {
unitType = 'words';
unitLimit = matches.groups.wordLimit;
} else if (matches.groups.charLimit) {
unitType = 'chars';
unitLimit = matches.groups.charLimit;
} else {
return;
}
unitCount = counts[unitType];
// write in unit count with appropriate formatting
// add new unit count to heading
let trailingSeparator = !matches.groups.trailingSeparator ? textSeparators[0].replace('\\','') : '';
// convert to RE2-compliant syntax for .replaceText() method
let reIndicatorRE2 = reIndicator.source.replace(/\(\?</g,"(?P<");
heading.replaceText(reIndicatorRE2, `${matches.groups.unitCountPrefix}${unitCount}${trailingSeparator}${matches.groups.unitCountSuffix}`);
// determine string position of updated unit count
let unitCountBegins = matches.index + matches.groups.unitCountPrefix.length;
let unitCountEnds = unitCountBegins + unitCount.toString().length;
// build format objects
let hlUnder = {}, hlClose = {}, hlOver = {};
hlUnder[DocumentApp.Attribute.BACKGROUND_COLOR] = underColor;
hlClose[DocumentApp.Attribute.BACKGROUND_COLOR] = closeColor;
hlOver[DocumentApp.Attribute.BACKGROUND_COLOR] = overColor;
// calculate whether unit count is under, close to, or over the limit
let threshold = Math.max(
Math.round((1 - thresholdUnits.proportion) * unitLimit),
unitLimit - thresholdUnits.exact * (unitType === 'chars' ? averageWordLength : 1)
);
if (unitCount < threshold) {
highlightStyle = hlUnder;
} else if (unitCount >= threshold && unitCount <= unitLimit) {
highlightStyle = hlClose;
} else if (unitCount > unitLimit) {
highlightStyle = hlOver;
}
// apply highlight formatting
heading.editAsText().setAttributes(unitCountBegins, unitCountEnds - 1, highlightStyle);
return;
}
// MENU & TRIGGER GENERATION
// Main processing function
function runCount() {
const lock = LockService.getScriptLock();
lock.waitLock(4000);
// Check if document length has changed before running time trigger
let myDoc = DocumentApp.getActiveDocument().getBody();
let docText = myDoc.editAsText().getText();
let docLen = docText.length;
if(docLen != documentProperties.getProperty('docLen')) {
parProcess(myDoc.getChild(0));
documentProperties.setProperty('docLen', docLen)
documentProperties.setProperty('lastUpdated', Date.now());
}
// remove trigger on timeout
let myDocID = DocumentApp.getActiveDocument().getId();
let lastUpdated = new Date(Number(documentProperties.getProperty('lastUpdated')));
let lastManualRun = new Date(Number(documentProperties.getProperty('lastManualRun')));
let timeOutDate = lastUpdated > lastManualRun ? lastUpdated : lastManualRun;
let elapsedTime = Math.floor((Date.now() - timeOutDate.getTime()) / 1000 / 60);
if (elapsedTime >= triggerTimeOut) {
deleteTrigger(documentProperties.getProperty('timeTriggerID'));
Logger.log(`Trigger removed: script timed out after ${triggerTimeOut} minutes.`);
}
lock.releaseLock();
}
// creates menu and starts trigger on open
function onOpenActions() {
// create menu
let ui = DocumentApp.getUi();
ui.createMenu('Word Count')
.addItem('Update Word/Character Counts', 'manualRunCount')
.addToUi();
manualRunCount();
}
// Manually (re)start script and start a manual timeout
function manualRunCount() {
if (!timeTriggerExists()) {
documentProperties.setProperty('timeTriggerID', createTimeTrigger());
}
// reset manual cooldown
documentProperties.setProperty('lastManualRun', Date.now());
runCount();
}
// Check to see if time-based trigger exists
function timeTriggerExists() {
let timeTriggerID = documentProperties.getProperty('timeTriggerID');
if (!timeTriggerID) return false;
// Detect if user has manually deleted the trigger
let allTriggers = ScriptApp.getProjectTriggers();
for (trigger of allTriggers) {
if (trigger.getUniqueId() === timeTriggerID) {
return true;
}
}
return false;
}
// Create time-based trigger
function createTimeTrigger() {
// Trigger every minute
let timeTriggerID = ScriptApp.newTrigger('runCount')
.timeBased()
.everyMinutes(1)
.create()
.getUniqueId();
return timeTriggerID;
}
// Remove time-based trigger
function deleteTrigger(triggerID) {
// Loop over all triggers
let allTriggers = ScriptApp.getProjectTriggers();
for (trigger of allTriggers) {
if (trigger.getUniqueId() === triggerID) {
ScriptApp.deleteTrigger(trigger);
documentProperties.deleteProperty('timeTriggerID');
break;
}
}
}