-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLaTeX2sheet.js
executable file
·218 lines (203 loc) · 7.71 KB
/
LaTeX2sheet.js
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
function latexToSheet() {
var result = SpreadsheetApp.getUi().alert("Only work for the LaTeX code generated via the spread-latex. Please provide me all the LaTeX code at least from \\begin{tabular} to \\end{tabular}. Click OK to continue.", SpreadsheetApp.getUi().ButtonSet.OK_CANCEL)
if (result != 'OK') {
return 0;
}
ui = SpreadsheetApp.getUi();
// var spreadsheet = SpreadsheetApp.openById('1gBEkOtHDZoUsF4RwV_mgrbAJ7rUlCzEPs3bRH51rK-o');
// var range = spreadsheet.getSheetByName("table maker").getDataRange()
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange();
var table = range.getValues();
var latexCode = []
// console.log(table)
var start_r = -1
var start_c = -1
var end_c = -1
for (var r = 0; r < table.length; r++) {
for (var c = 0; c < table[r].length; c++) {
if (String(table[r][c]).includes('\\begin{tabular}')) {
start_r = r
start_c = c
}
if (String(table[r][c]).includes('\\end{tabular}')) {
end_c = c
}
}
}
if (start_r === -1 || start_c === -1) {
SpreadsheetApp.getUi().alert("Please provide me all the LaTeX code from \\begin{tabular} to \\end{tabular}", SpreadsheetApp.getUi().ButtonSet.OK)
return 0;
}
if (end_c === -1) {
SpreadsheetApp.getUi().alert("\\end{tabular} is not found", SpreadsheetApp.getUi().ButtonSet.OK)
return 0;
}
while (true) {
start_r += 1;
cell_value = table[start_r][start_c]
if (cell_value.includes('\\bottomrule') || cell_value.includes('\\end{tabular}')) {
break;
}
cell_value = getContent(cell_value)
latexCode.push(cell_value);
}
// convert latex code to the table.
var sheet;
var result = SpreadsheetApp.getUi().alert("Clear the current sheet for the new table? Click \"No\" if needs a new sheet.", SpreadsheetApp.getUi().ButtonSet.YES_NO_CANCEL)
if (result == "YES") {
sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.clear();
} else if (result == "NO") {
sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet();
} else {
return 0;
}
// console.log(latexCode)
var rowIndex = 1;
var columnIndex = 1;
var str;
for (let count = 0; count < latexCode.length; count++) {
var row = latexCode[count]
// if (count == 16){
// console.log(count)
// }
row = row.split("&");
for (var j = 0; j < row.length; j++) {
//remove all the white space at the beginning and end of the string.
var cell = trimWhiteSpace(row[j]);
//remove all the automatically generated ",,"
var cs = 1;
var rs = 1;
str = trimStart(cell, "\\multicolumn");
if (str !== cell) {
cell = str;
idx_start = cell.indexOf("{")
idx_end = cell.indexOf("}")
cs = Number(cell.substring(idx_start + 1, idx_end));
cell = cell.substring(idx_end + 5, cell.lastIndexOf("}"));
}
str = trimStart(cell, "\\multirow");
if (str !== cell) {
cell = str;
idx_start = cell.indexOf("{")
idx_end = cell.indexOf("}")
rs = Number(cell.substring(idx_start + 1, idx_end));
cell = cell.substring(idx_end + 5, cell.lastIndexOf("}"));
}
if (rs > 1 || cs > 1) {
sheet.getRange(rowIndex, columnIndex, rs, cs).merge()
}
str = trimStart(cell, "\\cellcolor");
if (str !== cell) {
cell = str;
var color = "#" + cell.substring(cell.indexOf("{") + 1, cell.indexOf("}"));
sheet.getRange(rowIndex, columnIndex).setBackground(color);
cell = trimStart(cell, "}");
}
str = trimStart(cell, "\\ul");
if (str !== cell) {
cell = str;
sheet.getRange(rowIndex, columnIndex).setFontLine('underline');
cell = cell.substring(cell.indexOf("{") + 1, cell.lastIndexOf("}"));
}
str = trimStart(cell, "\\textbf");
if (str !== cell) {
cell = str;
sheet.getRange(rowIndex, columnIndex).setFontWeight("bold");
cell = cell.substring(cell.indexOf("{") + 1, cell.lastIndexOf("}"));
}
if (cell !== "") {
var percentage = ""
var currency = ""
// if cell object is a number
if (cell.indexOf("%") !== -1) {
var a = cell.substring(cell.length - 2, cell.length)
var b = cell.substring(0, cell.length - 2)
if (a === "\\%" && !isNaN(b)) {
percentage = "%"
cell = b
} else {
cell = cell.replace(/\\%/g, "%");
}
}
if (cell.indexOf("#") !== -1) {
cell = cell.replace(/\\#/g, "#")
}
if (cell.indexOf("_") !== -1 & cell.indexOf("$") === -1) {
cell = cell.replace(/\\_/g, "_")
}
if (cell.indexOf("\\$") !== -1) {
var a = cell.substring(0, 2)
var b = cell.substring(2, cell.length)
if (a == "\\$" && !isNaN(b)) {
cell = b
currency = "$"
} else {
cell = cell.replace(/\\$/g, "$");
}
}
if (!isNaN(cell)) {
var form = "0";
if (cell.indexOf(".") !== -1) {
var decimals = cell.split(".")[1].split("e")[0]
form += "." + repeat("0", decimals.length);
}
if (cell.indexOf('e') !== -1) {
form += "E+00"
}
form = currency + form + percentage
sheet.getRange(rowIndex, columnIndex).setNumberFormat(form);
sheet.getRange(rowIndex, columnIndex).setHorizontalAlignment("right");
} else {
sheet.getRange(rowIndex, columnIndex).setShowHyperlink(false);
// sheet.getRange(rowIndex, columnIndex).setValue(cell);
}
sheet.getRange(rowIndex, columnIndex).setValue(cell);
}
columnIndex += cs;
}
rowIndex++;
columnIndex = 1;
}
sheet.autoResizeColumns(1, sheet.getDataRange().getNumColumns());
Browser.msgBox('The table is loaded successfully!', Browser.Buttons.OK)
}
/**
* repeat str for num times.
* num larger than 1.
**/
function repeat(str, num) {
var newStr = "";
for (var i = 0; i < num; i++) {
newStr += str;
}
return newStr;
}
/**
*
* @param str original string
* @param substr
* @returns true if substr is trimmed from string, false if str does not include substr.
*/
function trimStart(str, substr) {
var ind = str.indexOf(substr);
if (ind !== -1) {
str = str.substring(ind + substr.length);
}
return str;
}
function getContent(str) {
var ind = str.lastIndexOf('\\\\');
if (ind != -1) {
str = str.substring(0, ind - 1)
}
str = trimWhiteSpace(str)
return str
}
function trimWhiteSpace(str) {
do {
var original = str;
str = str.replace(/(^\s)|(\s$)/g, "");
} while (original !== str);
return str;
}