welsonjs/officeloader.js

82 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-11-27 10:14:11 +00:00
// officeloader.js
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
var SYS = require("lib/system");
2023-11-27 07:56:18 +00:00
var Office = require("lib/msoffice");
2023-11-27 09:03:22 +00:00
var ChatGPT = require("lib/chatgpt");
2023-11-27 07:56:18 +00:00
function main(args) {
2023-12-18 20:56:16 +00:00
// EXAMPLE: cscript app.js officeloader <data\example.xlsx> <programfile>
2023-12-18 21:47:48 +00:00
// TEST: cscript app.js testloader open_excel_file data\test-msoffice-20231219.json
2023-11-27 10:14:11 +00:00
if (args.length > 0) {
var filename = args[0];
2023-12-18 20:56:16 +00:00
var programfile = args[1];
open(filename, programfile);
2023-11-27 10:14:11 +00:00
} else {
2023-12-18 21:47:48 +00:00
console.error("Insufficient arguments");
2023-11-27 10:14:11 +00:00
}
}
2023-12-18 20:56:16 +00:00
function open(filename, programfile) {
2023-12-18 20:12:05 +00:00
var filetypes = [
{"application": "excel", "filetypes": Office.Excel.SupportedFileTypes},
{"application": "powerpoint", "filetypes": Office.PowerPoint.SupportedFileTypes},
{"application": "word", "filetypes": Office.Word.SupportedFileTypes}
];
2023-12-18 20:56:16 +00:00
var resolved_application = filetypes.reduce(function(a, x) {
2023-12-18 20:12:05 +00:00
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;
}, '');
2023-12-18 20:56:16 +00:00
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) {
2023-12-18 20:12:05 +00:00
case "excel": {
var excel = new Office.Excel(); // Create an Excel instance
excel.open(filename); // Open the Excel instance
2023-12-18 20:56:16 +00:00
after_opened(excel);
2023-12-18 20:12:05 +00:00
break;
}
case "powerpoint": {
var powerpoint = new Office.PowerPoint(); // Create a PowerPoint instance
powerpoint.open(filename); // Open the PowerPoint instance
2023-12-18 20:56:16 +00:00
after_opened(powerpoint);
2023-12-18 20:12:05 +00:00
break;
}
2023-11-27 10:14:11 +00:00
2023-12-18 20:12:05 +00:00
case "word": {
var word = new Office.Word(); // Create an Word instance
word.open(filename); // Open the Word instance
2023-12-18 20:56:16 +00:00
after_opened(word);
2023-12-18 20:12:05 +00:00
break;
}
default: {
console.error("Not supported filetype");
}
}
2023-11-27 10:14:11 +00:00
}
exports.main = main;