-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.js
129 lines (117 loc) · 3.58 KB
/
excel.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
// deno-lint-ignore-file no-var no-inner-declarations
function saveActiveRow(excel) {
var sheet = excel.ActiveSheet;
if (sheet) {
// get max column that have data
var maxColumn = Math.min(sheet.UsedRange.Columns.Count, 100);
// get active range
var cell = excel.ActiveCell;
if (cell) {
var activeRow = cell.Row;
var rowValues = []
var headings = []
for (var i = 1; i <= maxColumn; i++) {
var value = sheet.Cells(activeRow, i).Value;
rowValues.push(value);
var heading = sheet.Cells(1, i).Value;
headings.push(heading);
}
// get excel file name
var fileName = excel.ActiveWorkbook.FullName;
// get sheet name
var sheetName = sheet.Name;
// get active row number
var headingsStr = headings.join('_@@HS@@_');
var rowValuesStr = rowValues.join('_@@VS@@_');
var content = [fileName, sheetName, activeRow, headingsStr, rowValuesStr].join('_@@RS@@_');
WScript.Echo('CCE:' + encodeStr(content));
// WScript.Echo(content);
return
}
}
WScript.Echo('');
}
function updateActiveRow(col, value) {
var sheet = excel.ActiveSheet;
if (sheet) {
var cell = excel.ActiveCell;
if (cell) {
var activeRow = cell.Row;
sheet.Cells(activeRow, col).Value = value;
}
}
}
function gotoRow(row) {
var sheet = excel.ActiveSheet;
if (sheet) {
sheet.Cells(row, 1).Select();
}
}
function navigateRow(offset) {
var sheet = excel.ActiveSheet;
if (sheet) {
var cell = excel.ActiveCell;
if (cell) {
var activeRow = cell.Row;
var newRow = activeRow + offset;
// keep current column
sheet.Cells(newRow, cell.Column).Select();
}
}
}
var excel
function main() {
excel = new ActiveXObject('Excel.Application');
excel.Visible = true;
for (;;) {
var arr = WScript.StdIn.ReadLine().split(' ');
var cmd = arr[0];
var args = arr.slice(1);
if (cmd == "exit") {
WScript.Echo("ok");
break;
}
try {
if (cmd === 'getActiveRow') {
try {
saveActiveRow(excel);
} catch (e) {
WScript.Echo('Error: ' + e.message);
}
} else if (cmd === 'updateActiveRow') {
var col = parseInt(args[0]);
var value = args[1];
updateActiveRow(col, value);
WScript.Echo("done");
} else if (cmd === 'gotoRow') {
var row = parseInt(args[0]);
gotoRow(row);
WScript.Echo("done");
} else if (cmd === 'navigateRow') {
var offset = parseInt(args[0]);
navigateRow(offset);
WScript.Echo("done");
} else if (cmd === 'test') {
WScript.Echo("excel sync: test");
WScript.Echo(encodeStr(excel.ActiveCell.Value));
} else if (cmd === 'eval') {
WScript.Echo(eval(args.join(' ')));
}
} catch (e) {
WScript.Echo(e.message);
}
}
}
function encodeStr(s) {
var e = []
for (var i = 0; i < s.length; i++) {
e.push(s.charCodeAt(i));
}
return e.join(",");
}
try {
main();
} catch (e) {
WScript.Echo('Error: ' + e.message);
}
excel.Quit()