-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_docs_count_words.js
More file actions
72 lines (56 loc) · 1.83 KB
/
google_docs_count_words.js
File metadata and controls
72 lines (56 loc) · 1.83 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
// These functions count words in a Google Doc and update a Google Spreadsheet
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Script')
.addItem('Update Count Trix', 'myFunction')
.addItem('Update Section Count', 'countPerSection')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
// Show sidebar in Google Doc
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('page')
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
/*
*** FIND WORDS ***
*/
function findWordsAndHighlight(term) {
var doc = DocumentApp.openById(""); // Google Doc URL
var text = doc.editAsText();
var search = term;
var search_count = 0;
var index = -1;
var color ="#2577ba";
var textLength = search.length-1;
while(true)
{
index = text.getText().indexOf(search,index+1);
if(index == -1)
break;
else {
text.setForegroundColor(index, index+textLength,color );
search_count = search_count + 1;
}
}
Logger.log(search_count);
var trix = SpreadsheetApp.openById(""); // Google Spreadsheet to be updated with word count
var tab = trix.getSheetByName("tags")
tab.appendRow([search, search_count]);
return search_count;
};
/*
*** MAIN FUNCTION ***
*/
function myFunction() {
var DS_text = DocumentApp.openById('').getBody().getText(); // Google Doc URL
var DS_words = DS_text.match(/\S+/g);
//
var trix = SpreadsheetApp.openById(''); // Google Spreadsheet to be updated with word count
var sheet = trix.getSheetByName('books');
var day = Utilities.formatDate(new Date(), "GMT-8", "MM-dd-yyyy");
sheet.appendRow([day, 60000, DS_words.length, TW_words.length, , , , findWordsAndHighlight("[name1]"),findWordsAndHighlight("[name2]"),findWordsAndHighlight("[name3]"),findWordsAndHighlight("[name4]")]);
}