-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA03. Table insertion.js
More file actions
318 lines (296 loc) · 7.73 KB
/
A03. Table insertion.js
File metadata and controls
318 lines (296 loc) · 7.73 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
function insertTable2x2() {
insertTable(2, 2);
}
function insertTable3x3() {
insertTable(3, 3);
}
function insertTable4x4() {
insertTable(4, 4);
}
const tableStyles = {
textStyle_TOPIC_COLUMN_CELL: {
fontSize: {
magnitude: 12,
unit: 'PT'
},
bold: true,
weightedFontFamily: {
fontFamily: styles[ACTIVE_STYLE]['fontFamily'],
weight: 400
}
},
textStyle_ITEM_CELL: {
fontSize: {
magnitude: 12,
unit: 'PT'
},
bold: false,
weightedFontFamily: {
fontFamily: styles[ACTIVE_STYLE]['fontFamily'],
weight: 400
}
}
};
const tableStyle_TRANSPERENT_BORDER = {
width: {
magnitude: 0,
unit: 'PT'
},
dashStyle: 'SOLID',
color: {
color: {
rgbColor: {}
}
}
};
const tableStyle_ORANGE_BORDER = {
width: {
magnitude: 1.0,
unit: 'PT'
},
dashStyle: 'SOLID',
color: {
color: {
rgbColor: hexToRGB(styles[ACTIVE_STYLE]['main_heading_font_color'])
}
},
};
const paragraphStyle_TABLE = {
namedStyleType: 'NORMAL_TEXT',
spaceAbove: { magnitude: styles[ACTIVE_STYLE]['paragraphSpacesInCell'], unit: 'PT' },
spaceBelow: { magnitude: styles[ACTIVE_STYLE]['paragraphSpacesInCell'], unit: 'PT' },
alignment: 'START',
};
const paragraphStyle_TABLE_HEADING = {
namedStyleType: 'HEADING_6',
spaceAbove: { magnitude: 10, unit: 'PT' },
spaceBelow: { magnitude: 10, unit: 'PT' },
alignment: 'START'
};
const textStyle_TABLE_HEADING_PART_1 = {
foregroundColor: {
color: {
rgbColor: hexToRGB(styles[ACTIVE_STYLE]['customStyle']['h6']['FOREGROUND_COLOR'])
}
},
fontSize: {
magnitude: styles[ACTIVE_STYLE]['customStyle']['h6']['FONT_SIZE'],
unit: 'PT'
},
bold: true,
italic: false,
weightedFontFamily: {
fontFamily: styles[ACTIVE_STYLE]['fontFamily'],
weight: 400
}
};
const textStyle_TABLE_HEADING_PART_2 = {
foregroundColor: {
color: {
rgbColor: hexToRGB(styles[ACTIVE_STYLE]['customStyle']['h6']['FOREGROUND_COLOR'])
}
},
fontSize: {
magnitude: styles[ACTIVE_STYLE]['customStyle']['h6']['FONT_SIZE'],
unit: 'PT'
},
bold: false,
italic: true,
weightedFontFamily: {
fontFamily: styles[ACTIVE_STYLE]['fontFamily'],
weight: 400
}
};
// Get number of rows and number of columns
// Return object that contains texts for cells, styles for cells, startIndex, endIndex for each cell
// insertTable use the function
function addTableParameters(numRows, numCols) {
const table = [];
let previousCellPos = 1;
for (let row = 0; row < numRows; row++) {
table.push([]);
for (let column = 0; column < numCols; column++) {
table[row].push({});
// Describe cell's style
if (row == 0 || column == 0) {
if (row == 0 && column == 0) {
table[row][column]['text'] = ' ';
}
table[row][column]['style'] = 'TOPIC_COLUMN_CELL';
if (row != 0) {
table[row][column]['text'] = 'Topic ' + row;
}
if (column != 0) {
table[row][column]['text'] = 'Column ' + column;
}
} else {
table[row][column]['style'] = 'ITEM_CELL';
table[row][column]['text'] = 'Item';
}
if (column == 0) {
table[row][column]['pos'] = previousCellPos + 3;
} else {
table[row][column]['pos'] = previousCellPos + 2;
}
previousCellPos = table[row][column]['pos'];
}
}
return table;
}
// Get number of rows and number of columns
// Insert table using Doc API
// insertTable2x2, insertTable3x3, insertTable4x4 use the function
function insertTable(numRows, numCols) {
const ui = DocumentApp.getUi();
try {
const doc = DocumentApp.getActiveDocument();
const documentId = doc.getId();
const cursorPosition = detectCursorPosition(doc, documentId);
if (cursorPosition.status == 'error') {
ui.alert(cursorPosition.message);
return 0;
}
const tableStartIndex = cursorPosition.endIndex;
const requests = [];
requests.push(
{
insertTable: {
rows: numRows,
columns: numCols,
location: { index: tableStartIndex }
}
},
{
updateTableCellStyle: {
tableRange: {
tableCellLocation: {
tableStartLocation: {
index: tableStartIndex + 1
},
},
rowSpan: numRows,
columnSpan: numCols
},
tableCellStyle: {
borderBottom: tableStyle_ORANGE_BORDER,
borderLeft: tableStyle_TRANSPERENT_BORDER,
borderRight: tableStyle_TRANSPERENT_BORDER,
},
fields: 'borderBottom,borderLeft,borderRight'
}
},
{
updateTableCellStyle: {
tableRange: {
tableCellLocation: {
tableStartLocation: {
index: tableStartIndex + 1
},
},
rowSpan: 1,
columnSpan: numCols
},
tableCellStyle: {
borderTop: tableStyle_TRANSPERENT_BORDER
},
fields: 'borderTop'
}
}
);
const table = addTableParameters(numRows, numCols);
let textStyle;
for (let row = numRows - 1; row >= 0; row--) {
for (let col = numCols - 1; col >= 0; col--) {
textStyle = tableStyles['textStyle_' + table[row][col]['style']];
requests.push({
insertText: {
text: table[row][col]['text'],
location: {
index: tableStartIndex + table[row][col]['pos']
},
}
},
{
updateParagraphStyle: {
paragraphStyle: paragraphStyle_TABLE,
range: {
startIndex: tableStartIndex + table[row][col]['pos'],
endIndex: tableStartIndex + table[row][col]['pos'] + table[row][col]['text'].length
},
fields: formFieldsString(paragraphStyle_TABLE)
}
},
{
updateTextStyle: {
range: {
startIndex: tableStartIndex + table[row][col]['pos'],
endIndex: tableStartIndex + table[row][col]['pos'] + table[row][col]['text'].length
},
text_style: textStyle,
fields: formFieldsString(textStyle)
}
}
);
}
}
requests.push({
insertText: {
text: 'Table X. Table title',
location: {
index: tableStartIndex
}
}
},
{
updateParagraphStyle: {
paragraphStyle: paragraphStyle_TABLE_HEADING,
range: {
startIndex: tableStartIndex,
endIndex: tableStartIndex + 20
},
fields: formFieldsString(paragraphStyle_TABLE_HEADING)
}
},
{
updateTextStyle: {
range: {
startIndex: tableStartIndex,
endIndex: tableStartIndex + 8
},
text_style: textStyle_TABLE_HEADING_PART_1,
fields: formFieldsString(textStyle_TABLE_HEADING_PART_1)
}
},
{
updateTextStyle: {
range: {
startIndex: tableStartIndex + 8,
endIndex: tableStartIndex + 20
},
text_style: textStyle_TABLE_HEADING_PART_2,
fields: formFieldsString(textStyle_TABLE_HEADING_PART_2)
}
},
{
deleteNamedRange: {
name: cursorPosition.rangeName
}
},
{
deleteContentRange: {
range: {
startIndex: tableStartIndex - 1,
endIndex: tableStartIndex,
}
}
}
);
Docs.Documents.batchUpdate({
requests: requests
}, documentId);
}
catch (error) {
ui.alert('Error in insertTable: ' + error);
return 0;
}
}