diff --git a/lib/msoffice.js b/lib/msoffice.js index 13ad583..8aeadc8 100644 --- a/lib/msoffice.js +++ b/lib/msoffice.js @@ -1,4 +1,5 @@ // msoffice.js +// Namhyeon Go // https://github.com/gnh1201/welsonjs var STD = require("lib/std"); var SYS = require("lib/system"); @@ -114,7 +115,7 @@ function Excel() { this.version = this.application.Version; this.application.Visible = true; - console.info("Microsoft Office Excel:", this.version); + console.info("Microsoft Office Excel", this.version); this.currentWorkbook = null; this.currentWorksheet = null; @@ -127,11 +128,11 @@ function Excel() { filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path } if (FILE.fileExists(filename)) { - console.warn("Found the file:", filename); + console.info("FOUND", filename); this.application.Workbooks.Open(filename); this.currentWorkbook = this.application.ActiveWorkbook; } else { - console.warn("File not exists!"); + console.warn("NOT FOUND", filename); this.currentWorkbook = this.application.Workbooks.Add(); } } else { @@ -173,6 +174,8 @@ function Excel() { this.currentWorksheet = this.currentWorkbook.Worksheets(idx); } + console.log(typeof this.currentWorksheet); + return this; }; @@ -215,12 +218,48 @@ Excel.Cell = function(cell) { // EXAMPLE: new Office.PowerPoint() function PowerPoint() { this.application = CreateObject("PowerPoint.Application"); + this.version = this.application.Version; + this.application.Visible = true; + + console.info("Microsoft Office PowerPoint", this.version); + + this.open = function(filename) { + if (typeof filename !== "undefined") { + // check type of the path + if (filename.indexOf(":\\") < 0 && filename.indexOf(":/") < 0) { + filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path + } + if (FILE.fileExists(filename)) { + console.info("FOUND", filename); + } else { + console.warn("NOT FOUND", filename); + } + } + }; } PowerPoint.SupportedFileTypes = FileTypes.PowerPoint; // EXAMPLE: new Office.Word() function Word() { this.application = CreateObject("Word.Application"); + this.version = this.application.Version; + this.application.Visible = true; + + console.info("Microsoft Office Word", this.version); + + this.open = function(filename) { + if (typeof filename !== "undefined") { + // check type of the path + if (filename.indexOf(":\\") < 0 && filename.indexOf(":/") < 0) { + filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path + } + if (FILE.fileExists(filename)) { + console.info("FOUND", filename); + } else { + console.warn("NOT FOUND", filename); + } + } + }; } Word.SupportedFileTypes = FileTypes.Word; @@ -228,7 +267,7 @@ exports.Excel = Excel; exports.PowerPoint = PowerPoint; exports.Word = Word; -exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.1.8"; +exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.1.9"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/officeloader.js b/officeloader.js index 191664a..224aa8f 100644 --- a/officeloader.js +++ b/officeloader.js @@ -1,13 +1,12 @@ // officeloader.js // Namhyeon Go // 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 if (args.length > 0) { var filename = args[0]; open(filename); @@ -17,22 +16,56 @@ function main(args) { } function open(filename) { - // 엑셀 인스턴스 생성 - var excel = new Office.Excel(); - - // 엑셀 열기 - excel.open(filename); - - // .... 여기서 작업하세요 .... + var filetypes = [ + {"application": "excel", "filetypes": Office.Excel.SupportedFileTypes}, + {"application": "powerpoint", "filetypes": Office.PowerPoint.SupportedFileTypes}, + {"application": "word", "filetypes": Office.Word.SupportedFileTypes} + ]; - // 엑셀 닫기 - //excel.close(); + var resolved_filetype = 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; + }, ''); + + switch (resolved_filetype) { + case "excel": { + var excel = new Office.Excel(); // Create an Excel instance + excel.open(filename); // Open the Excel instance + break; + } + + case "powerpoint": { + var powerpoint = new Office.PowerPoint(); // Create a PowerPoint instance + powerpoint.open(filename); // Open the PowerPoint instance + break; + } + + case "word": { + var word = new Office.Word(); // Create an Word instance + word.open(filename); // Open the Word instance + break; + } + + default: { + console.error("Not supported filetype"); + } + } } function test() { // 엑셀 인스턴스 생성 var excel = new Office.Excel(); - + // 질문 목록 var questions = [ "엄마가 좋아 아빠가 좋아?",