Update officeloader.js

This commit is contained in:
Namhyeon Go 2023-12-19 05:56:16 +09:00
parent 5f487d9409
commit 92dedd182e

View File

@ -6,23 +6,24 @@ var Office = require("lib/msoffice");
var ChatGPT = require("lib/chatgpt"); var ChatGPT = require("lib/chatgpt");
function main(args) { function main(args) {
// EXAMPLE: cscript app.js officeloader data\example.xlsx // EXAMPLE: cscript app.js officeloader <data\example.xlsx> <programfile>
if (args.length > 0) { if (args.length > 0) {
var filename = args[0]; var filename = args[0];
open(filename); var programfile = args[1];
open(filename, programfile);
} else { } else {
test(); test();
} }
} }
function open(filename) { function open(filename, programfile) {
var filetypes = [ var filetypes = [
{"application": "excel", "filetypes": Office.Excel.SupportedFileTypes}, {"application": "excel", "filetypes": Office.Excel.SupportedFileTypes},
{"application": "powerpoint", "filetypes": Office.PowerPoint.SupportedFileTypes}, {"application": "powerpoint", "filetypes": Office.PowerPoint.SupportedFileTypes},
{"application": "word", "filetypes": Office.Word.SupportedFileTypes} {"application": "word", "filetypes": Office.Word.SupportedFileTypes}
]; ];
var resolved_filetype = filetypes.reduce(function(a, x) { var resolved_application = filetypes.reduce(function(a, x) {
if (a == '') { if (a == '') {
var application = x.application; var application = x.application;
var extensions = x.filetypes.reduce(function(b, x) { var extensions = x.filetypes.reduce(function(b, x) {
@ -36,23 +37,37 @@ function open(filename) {
return a; return a;
}, ''); }, '');
switch (resolved_filetype) { 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": { case "excel": {
var excel = new Office.Excel(); // Create an Excel instance var excel = new Office.Excel(); // Create an Excel instance
excel.open(filename); // Open the Excel instance excel.open(filename); // Open the Excel instance
after_opened(excel);
break; break;
} }
case "powerpoint": { case "powerpoint": {
var powerpoint = new Office.PowerPoint(); // Create a PowerPoint instance var powerpoint = new Office.PowerPoint(); // Create a PowerPoint instance
powerpoint.open(filename); // Open the PowerPoint instance powerpoint.open(filename); // Open the PowerPoint instance
after_opened(powerpoint);
break; break;
} }
case "word": { case "word": {
var word = new Office.Word(); // Create an Word instance var word = new Office.Word(); // Create an Word instance
word.open(filename); // Open the Word instance word.open(filename); // Open the Word instance
after_opened(word);
break; break;
} }