-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
officeloader.js
81 lines (70 loc) · 2.53 KB
/
officeloader.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
// officeloader.js
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
var SYS = require("lib/system");
var Office = require("lib/msoffice");
var ChatGPT = require("lib/chatgpt");
function main(args) {
// EXAMPLE: cscript app.js officeloader <data\example.xlsx> <programfile>
// TEST: cscript app.js testloader open_excel_file data\test-msoffice-20231219.json
if (args.length > 0) {
var filename = args[0];
var programfile = args[1];
open(filename, programfile);
} else {
console.error("Insufficient arguments");
}
}
function open(filename, programfile) {
var filetypes = [
{"application": "excel", "filetypes": Office.Excel.SupportedFileTypes},
{"application": "powerpoint", "filetypes": Office.PowerPoint.SupportedFileTypes},
{"application": "word", "filetypes": Office.Word.SupportedFileTypes}
];
var resolved_application = filetypes.reduce(function(a, x) {
if (a == '') {
var application = x.application;
var extensions = x.filetypes.reduce(function(b, x) {
return b.concat(x.extension);
}, []);
return extensions.reduce(function(b, x) {
return (b == '' && filename.lastIndexOf(x) > -1 ? application : b);
}, '');
}
return a;
}, '');
var after_opened = function(officeInstance) {
if (typeof programfile !== "undefined") {
var target = require(programfile);
try {
target.onApplicationOpened(resolved_application, officeInstance);
} catch (e) {
console.error("after_opened:", e.message);
}
};
};
switch (resolved_application) {
case "excel": {
var excel = new Office.Excel(); // Create an Excel instance
excel.open(filename); // Open the Excel instance
after_opened(excel);
break;
}
case "powerpoint": {
var powerpoint = new Office.PowerPoint(); // Create a PowerPoint instance
powerpoint.open(filename); // Open the PowerPoint instance
after_opened(powerpoint);
break;
}
case "word": {
var word = new Office.Word(); // Create an Word instance
word.open(filename); // Open the Word instance
after_opened(word);
break;
}
default: {
console.error("Not supported filetype");
}
}
}
exports.main = main;