-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrosswordNumberer.gs
134 lines (118 loc) · 3.25 KB
/
CrosswordNumberer.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
// code to add the custom menu
function onOpen() {
const ui = DocumentApp.getUi();
ui.createMenu('Crosswords')
.addItem('renumber Crossword', 'numberCrossword')
.addToUi();
}
function numberCrossword() {
var doc = DocumentApp.getActiveDocument();
var element = null;
var cursor = doc.getCursor();
if (cursor != null)
{
element = cursor.getElement();
}
else
{
var selection = doc.getSelection();
if (selection != null)
{
element = selection.getRangeElements()[0].getElement();
}
else
{
DocumentApp.getUi().alert("Can't get element for some reason!");
return;
}
}
while (element.getType() != "TABLE" && element.getType() != "BODY_SECTION"){
element = element.getParent();
}
// Logger.log(element);
if (element.getType() != "TABLE") {
DocumentApp.getUi().alert("Crossword grid not currently selected");
return;
}
var nextNumber = 1;
var xtable = element.asTable();
var rowCount = xtable.getNumRows();
var previousRow = null;
var row = null;
var nextRow = xtable.getRow(0);
for (var rnum = 0; rnum < rowCount; rnum++)
{
previousRow = row;
row = nextRow;
if (rnum < rowCount - 1)
{
nextRow = xtable.getRow(rnum + 1);
}
else
{
nextRow = null;
}
var cellCount = row.getNumCells();
for (var cnum = 0; cnum < cellCount; cnum++)
{
var cell = row.getCell(cnum);
var bg = cell.getBackgroundColor();
if (bg != null) // it's a black cell - don't need to do anything (null is colour = none = white)
{
continue;
}
var bg_above = '#000000';
if (previousRow != null) // not the first row
{
bg_above = previousRow.getCell(cnum).getBackgroundColor();
}
var bg_below = '#000000';
if (nextRow != null) // not the last row
{
bg_below = nextRow.getCell(cnum).getBackgroundColor();
}
var bg_left = '#000000';
if (cnum > 0) // not the first cell
{
bg_left = row.getCell(cnum - 1).getBackgroundColor();
}
var bg_right = '#000000';
if (cnum < cellCount - 1) // not the last cell
{
bg_right = row.getCell(cnum + 1).getBackgroundColor();
}
var needsNumber = false;
if (bg_above != null && bg_below == null) // (null is colour = none = white, anything else considered black)
{
needsNumber = true;
}
if (bg_left != null && bg_right == null) // (null is colour = none = white, anything else considered black)
{
needsNumber = true;
}
var cellChildCount = cell.getNumChildren();
for (var childNum = 0; childNum < cellChildCount; childNum++)
{
var para = cell.getChild(childNum).asParagraph();
if (para != null)
{
if (needsNumber)
{
para.setText(nextNumber);
text = para.getChild(0).asText();
text.setBackgroundColor(null);
text.setForegroundColor('#000000');
text.setFontSize(9);
text.setFontFamily('Arial');
nextNumber++;
}
else
{
para.clear();
}
break; // don't look for any more paragraphs!
}
}
}
}
}