This repository was archived by the owner on Feb 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettify.gs
More file actions
78 lines (63 loc) · 2.32 KB
/
prettify.gs
File metadata and controls
78 lines (63 loc) · 2.32 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
73
74
75
76
77
78
var selection = DocumentApp.getActiveDocument().getSelection();
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Add Box', 'addBox')
.addToUi();
}
function onInstall(e) {
onOpen(e);
}
function addBox () {
var body = DocumentApp.getActiveDocument().getBody();
if (selection) {
var table;
var cell;
var firstIndex;
var selectedText = [];
var elements = selection.getRangeElements();
for (var i = 0; i < elements.length; i++) {
if (elements[i].isPartial()) {
var element = elements[i].getElement().asText();
var startIndex = elements[i].getStartOffset();
var endIndex = elements[i].getEndOffsetInclusive();
var text = element.getText().substring(startIndex, endIndex + 1);
if (i === 0) {
firstIndex = DocumentApp.getActiveDocument().getBody().getChildIndex(elements[i].getElement().getParent());
}
selectedText.push(text);
element.deleteText(startIndex, endIndex);
} else {
var element = elements[i].getElement();
if (i === 0) {
firstIndex = DocumentApp.getActiveDocument().getBody().getChildIndex(element);
}
selectedText.push(element.editAsText().getText());
element.removeFromParent();
}
}
table = body.insertTable(firstIndex);
cell = table.appendTableRow().appendTableCell([[selectedText.join('\r')]]);
setTableStyle(table,cell);
} else {
var cursorElement = DocumentApp.getActiveDocument().getCursor().getElement();
var index;
if (cursorElement == 'Paragraph') {
index = body.getChildIndex(DocumentApp.getActiveDocument().getCursor().getElement());
} else {
index = body.getChildIndex(DocumentApp.getActiveDocument().getCursor().getElement().getParent());
}
var table = body.insertTable(index);
var cell = table.appendTableRow().appendTableCell([['']]);
setTableStyle(table,cell);
}
}
function setTableStyle(table,cell){
var tableStyle = {};
var cellStyle = {};
tableStyle[DocumentApp.Attribute.BORDER_COLOR] = '#d9d9d9';
tableStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Consolas';
tableStyle[DocumentApp.Attribute.FONT_SIZE] = 9;
cellStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#f5f5f5';
table.setAttributes(tableStyle);
cell.setAttributes(cellStyle);
}