-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
165 lines (143 loc) · 6.09 KB
/
Program.cs
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
using System;
using Microsoft.Office.Interop.Excel;
namespace xlrun
{
class Program
{
static string Usage() {
var exe = System.AppDomain.CurrentDomain.FriendlyName;
var usage = $@"
Usage examples:
{exe} -xlFileOpen MyWorkbook.xlsx -xlRefreshLeftToRight -xlRngGet Summary!TestStatus
{exe} -xlFileOpen MyMacrobook.xlsm -xlEvalMacro MyMacro -xlRngGet Summary!B4 -xlFileSave -timeout 10
{exe} -xlFileNew -xlRngSet A1 1.0 -xlRngGet A1 -xlRngSet A2 =today() -xlRngGet A2 -xlFileSaveAs MyGeneratedBook.xlsx
";
return usage;
}
static void Main(string[] args)
{
SetTimeoutWatchdog(args);
MainTask(args);
}
static void MainTask(string[] args)
{
// display usage if no arguments
if (args.Length==0) {
Console.WriteLine(Usage());
return;
}
var xlApp = new Microsoft.Office.Interop.Excel.Application();
try
{
Microsoft.Office.Interop.Excel._Workbook wbk = null;
Microsoft.Office.Interop.Excel.Range rng = null;
xlApp.DisplayAlerts = false;
xlApp.Visible = true;
xlApp.Interactive = true;
// no-well established parsearg method
for (int i=0; i<args.Length; i++) {
var cmd = args[i];
while (cmd[0]=='-' || cmd[0]=='/')
cmd = cmd.Substring(1);
switch (cmd) {
case "xlFileOpen":
case "xlFilePath":
var openPath = MakeFullPath(args[++i]);
Console.WriteLine($"> Open Workbook {openPath}");
if (wbk != null)
wbk.Close(SaveChanges: false);
wbk = xlApp.Workbooks.Open(openPath);
break;
case "xlFileNew":
Console.WriteLine($"> New Workbook");
if (wbk != null)
wbk.Close(SaveChanges: false);
wbk = xlApp.Workbooks.Add();
break;
case "xlFileSave":
Console.WriteLine($"> Save Workbook");
wbk.Save();
break;
case "xlFileSaveAs":
var savePath = MakeFullPath(args[++i]);
Console.WriteLine($"> Save Workbook as {savePath}");
wbk.SaveAs(savePath);
break;
case "xlEvalMacro":
var macro = args[++i];
Console.WriteLine($"> Evaluate macro {macro}");
xlApp.Evaluate(macro);
break;
case "xlRefreshLeftToRight":
Console.WriteLine($"> Refresh sheets left to right");
foreach (Microsoft.Office.Interop.Excel._Worksheet wsh in wbk.Worksheets)
if (wsh.Visible == XlSheetVisibility.xlSheetVisible)
wsh.Calculate();
break;
case "xlRngGet":
var rngGetAddr = args[++i];
Console.WriteLine($"> Get Range {rngGetAddr}");
rng = xlApp.Range[rngGetAddr];
Console.WriteLine(rng.Value);
break;
case "xlRngSet":
var rngSetAddr = args[++i];
var rngSetValue = args[++i];
Console.WriteLine($"> Set Range {rngSetAddr} {rngSetValue}");
rng = xlApp.Range[rngSetAddr];
rng.Value = rngSetValue;
break;
case "timeout":
i++;
break;
default:
throw new Exception($"> Unexpected command {cmd}");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Environment.Exit(1);
}
finally
{
foreach (Microsoft.Office.Interop.Excel._Workbook wbk in xlApp.Workbooks)
wbk.Close(SaveChanges: false);
xlApp.Quit();
Console.WriteLine("Done.");
}
}
static string MakeFullPath(string path) {
if (path.Contains("\\"))
return path;
else
return System.IO.Directory.GetCurrentDirectory() + "\\" + path;
}
static void SetTimeoutWatchdog(string[] args)
{
int timeoutMs = -1;
for (int i=0; i<args.Length; i++) {
var cmd = args[i];
if (cmd[0]!='-' && cmd[0]!='/')
continue;
while (cmd[0]=='-' || cmd[0]=='/')
cmd = cmd.Substring(1);
if (cmd=="timeout") {
timeoutMs = 1000 * Int32.Parse(args[++i]);
break;
}
}
if (timeoutMs>0) {
Console.WriteLine($"> Set Timeout {timeoutMs/1000.0}s");
var timer = new System.Timers.Timer(timeoutMs);
timer.AutoReset = false;
timer.Elapsed += new System.Timers.ElapsedEventHandler((object source, System.Timers.ElapsedEventArgs eea) => {
Console.WriteLine($"> Timeout expired !!");
Environment.Exit(-1);
});
timer.Start();
}
}
}
}