-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.gs
91 lines (65 loc) · 2.82 KB
/
table.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
// 実行する関数 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
function myFunction(arr,hf) {
var header = hf.length == 0 ? 2 : hf[0];
var footer = hf.length == 0 ? 2 : hf[1];
var output = testArrayToLatexTable(arr, header, footer)
return output;
}
// latexの表を作成する関数 ///////////////////////////////////////////////////////////////////////////////////////////////////////
function testArrayToLatexTable(array_maxdegit, header, footer) {
var array = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange().getValues();
var tableCaption = "表"
var tableLabel = "table1"
if(array.length > 0){
var latexTable = arrayToLatexTablecaption(array, array_maxdegit, tableCaption, tableLabel, header, footer)
Logger.log(latexTable);
return latexTable
}else{
return "表を取得できませんでした。";
}
}
function arrayToLatexTablecaption(array, decimalDigits, tableCaption, tableLabel, header, footer) {
var rows = array.length;
var cols = array[0].length;
var latexTable = "\\begin{table}[h]<br>";
latexTable += "\\caption{" + tableCaption + "}<br>";
latexTable += "\\label{" + tableLabel + "}<br>";
latexTable += "\\centering<br>";
latexTable += "\\scalebox{0.9}[0.9]{<br>";
latexTable += "\\begin{tabular}{|";
for (var i = 0; i < cols; i++) {
latexTable += "r|";
}
latexTable += "}<br>";
latexTable += "\\hline<br>";
latexTable += makeLaTXtable(0, header, cols, array, decimalDigits)
header == 0 ? null : latexTable += "\\hline \\hline<br>";
latexTable += makeLaTXtable(header, rows-footer, cols, array, decimalDigits)
footer == 0 ? null : latexTable += "\\hline \\hline<br>";
latexTable += makeLaTXtable(rows-footer, rows, cols, array, decimalDigits)
latexTable += "\\hline<br>";
latexTable += "\\end{tabular}<br>";
latexTable += "}<br>";
latexTable += "\\end{table}";
latexTable = String(latexTable).replace(/undefined/g,"")
return latexTable;
}
function makeLaTXtable(startrow, endrow, cols, array, decimalDigits){
let latexTable;
for (var i = startrow; i < endrow ; i++) {
for (var j = 0; j < cols; j++) {
//数値がどうかの判定。数値ならば小数点切り捨ての操作を行いそうでなければテキストとして扱う。
//空白セルは空白として扱う。
var value = String(array[i][j]).length <= 0 ? "" : (isFinite(String(array[i][j])) == true
? parseFloat(array[i][j]).toFixed(decimalDigits[j])
: String(array[i][j]))
latexTable += "" + value;
if (j < cols - 1) {
latexTable += " & ";
} else {
latexTable += " \\\\<br>";
}
}
}
return latexTable;
}