-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10c Existing translation table.js
More file actions
67 lines (60 loc) · 2.17 KB
/
Copy path10c Existing translation table.js
File metadata and controls
67 lines (60 loc) · 2.17 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
function translationTableContinuation(selectedTable, preserveFormatting, paraout) {
const newLine = `
`;
const translationTable = contentFromTable(selectedTable);
if (translationTable[0].length > 1) {
alert('To begin the translation, ensure the table contains a single column. The selected table has ' + translationTable[0].length + ' columns.');
return 0;
}
let startRow = 0;
if (selectedTable.getCell(0, 0).getText() === '《translationOf:') {
startRow = 1;
} else {
const headerRow = selectedTable.insertTableRow(0);
headerRow.appendTableCell('《translationOf:').setBackgroundColor('#f4f4f4');
}
for (let i = startRow; i < translationTable.length; i++) {
const htmlArray = [], textArray = [];
for (let j = 0; j < translationTable[i][0].length; j++) {
const paragraph = translationTable[i][0][j];
const paragraphText = translationTable[i][0][j].getText();
const html = preserveFormatting === true ? convertToHtml(paragraph, paragraphText) : paragraphText;
htmlArray.push(html);
textArray.push(paragraphText);
}
const cellHtml = htmlArray.join(newLine);
const cellText = textArray.join(newLine);
paraout.push({ text: cellText, html: cellHtml });
}
return { status: 'ok' };
}
function contentFromTableCell(element) {
const paragraphs = [];
const cellParagraphs = element.getNumChildren();
for (let m = 0; m < cellParagraphs; m++) {
const child = element.getChild(m);
if (child.getType() === DocumentApp.ElementType.PARAGRAPH || child.getType() === DocumentApp.ElementType.LIST_ITEM) {
paragraphs.push(child);
}
}
return paragraphs;
}
function contentFromTableRow(element) {
const rowContent = [];
const cells = element.getNumCells();
for (let k = 0; k < cells; k++) {
const cell = element.getCell(k);
// paragraphs.push(...paragraphsFromTableCell(cell));
rowContent.push(contentFromTableCell(cell));
}
return rowContent;
}
function contentFromTable(element) {
const tableContent = [];
const rows = element.getNumRows();
for (let k = 0; k < rows; k++) {
const row = element.getRow(k);
tableContent.push(contentFromTableRow(row));
}
return tableContent;
}