-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtable.gs
86 lines (76 loc) · 2.53 KB
/
table.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
/**
* update one table reference: (1) auto number the bookmark, (2) update the text/url of ALL reference to the bookmark
*
* @param: bookmarkObj - bookmarkObj
* tableNumber - number
* bookmarkObjs - an array of bookmarkObj
* paragraphMap - Map
* bookmarkMap - Map
* .
* @returns
*/
function updateTableReference(bookmarkObj, tableNumber, bookmarkObjs, paragraphMap, bookmarkMap) {
// update text of bookmark
updateTableBookmarkText(bookmarkObj.paragraph.editAsText(), tableNumber);
// add new bookmark and get its new url
bookmarkObj.bookmark = setBookmark(bookmarkObj.paragraph);
var url = '#bookmark=' + bookmarkObj.bookmark.getId();
// update bookmark link (both text and link)
var adjustment = 0;
for (var [parKey, linkObj] of bookmarkObj.linkMap.entries()) {
adjustment = updateLinkText(linkObj.paraTextObj, 'Table ' + tableNumber, linkObj.start, linkObj.end, url);
// update positions of sequential reference links in the same paragraph
adjustPositions(bookmarkObjs, paragraphMap, bookmarkMap, parKey, bookmarkObj.bookmarkId, adjustment);
}
}
/**
* auto number the table title
*
* @param: textOjb - this should be the paragraph that contains the Bookmark
* Note: A paragraph as such should be table titles.
* num - the number of the table title
* .
* @returns
*/
function updateTableBookmarkText(textOjb, num) {
var start, end;
var flag = true;
var text = textOjb.getText();
Logger.log(text);
// if starting with table xxx
start = text.search(/[tT]able\s*[0-9]+\./);
if (start != -1) {
// Logger.log('Case 1: ' + text);
end = text.split('.')[0].length;
updateText(textOjb, 'Table ' + num + '.', start, end);
flag = false;
return;
}
// if starting with table xxx (no period)
start = text.search(/[tT]able\s+[0-9]+\s+/);
if (start != -1) {
// Logger.log('Case 2: ' + text);
textArr = text.split(' ');
end = textArr[0].length + textArr[1].length;
updateText(textOjb, 'Table ' + num + '.', start, end);
flag = false;
return;
}
// if starting with tablexxx (no period)
start = text.search(/[tT]able[0-9]+\s+/);
if (start != -1) {
// Logger.log('Case 3: ' + text);
textArr = text.split(' ');
end = textArr[0].length;
updateText(textOjb, 'Table ' + num + '. ', start, end);
flag = false;
return;
}
// if not any of the above case - insertion
if (flag) {
// Logger.log('Case 4: ' + text);
start = 0;
textOjb.insertText(start, 'Table ' + num + '. ');
return;
}
}