diff --git a/data/example.xlsx b/data/example.xlsx new file mode 100644 index 00000000..ff4e3f9e Binary files /dev/null and b/data/example.xlsx differ diff --git a/lib/msoffice.js b/lib/msoffice.js index 3ab9a3be..2ec51e43 100644 --- a/lib/msoffice.js +++ b/lib/msoffice.js @@ -1,4 +1,5 @@ var STD = require("lib/std"); +var SYS = require("lib/system"); var FILE = require("lib/file"); // msoffice.js @@ -119,12 +120,24 @@ function Excel() { this.open = function(filename) { if (typeof filename !== "undefined") { - Application.Workbooks.Open(filename); - this.CurrentWorkbook = Application.ActiveWorkbook; + // check type of the path + if (filename.indexOf(":\\") < 0 && filename.indexOf(":/") < 0) { + filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path + } + if (FILE.fileExists(filename)) { + console.warn("Found the file:", filename); + this.Application.Workbooks.Open(filename); + this.CurrentWorkbook = this.Application.ActiveWorkbook; + } else { + console.warn("File not exists!"); + this.CurrentWorkbook = this.Application.Workbooks.Add(); + } } else { this.CurrentWorkbook = this.Application.Workbooks.Add(); } this.selectWorksheet(1); + + return this; }; this.close = function() { @@ -157,6 +170,8 @@ function Excel() { } else { this.CurrentWorksheet = this.CurrentWorkbook.Worksheets(idx); } + + return this; }; this.getValueByPosition = function(row, col) { @@ -164,7 +179,9 @@ function Excel() { }; this.setValueByPosition = function(row, col, value) { - return this.CurrentWorksheet.Cells(row, col).Value = value; + this.CurrentWorksheet.Cells(row, col).Value = value; + + return this; }; }; Excel.SupportedFileTypes = FileTypes.Excel; @@ -185,7 +202,7 @@ exports.Excel = Excel; exports.PowerPoint = PowerPoint; exports.Word = Word; -exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.1.2"; +exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.1.3"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/lib/system.js b/lib/system.js index 8b2b11be..45455d0b 100644 --- a/lib/system.js +++ b/lib/system.js @@ -24,15 +24,15 @@ exports.createProcess = function(cmd) { exports.getEnvString = function(envName) { return (function(s) { - switch(s) { - case "PROGRAMFILES": - return WSH.ExpandEnvironmentStrings("%HOMEDRIVE%\\Program Files"); - case "PROGRAMFILES(X86)": - return WSH.ExpandEnvironmentStrings("%HOMEDRIVE%\\Program Files (x86)"); - default: - return WSH.ExpandEnvironmentStrings('%' + s + '%'); - } - })(envName.toUpperCase()); + switch(s) { + case "PROGRAMFILES": + return WSH.ExpandEnvironmentStrings("%HOMEDRIVE%\\Program Files"); + case "PROGRAMFILES(X86)": + return WSH.ExpandEnvironmentStrings("%HOMEDRIVE%\\Program Files (x86)"); + default: + return WSH.ExpandEnvironmentStrings('%' + s + '%'); + } + })(envName.toUpperCase()); }; exports.get32BitFolder = function() { @@ -176,6 +176,6 @@ exports.ping = function(address) { return WMI.execQuery("Select ResponseTime From Win32_PingStatus where address='" + address + "'").fetch().get("ResponseTime"); }; -exports.VERSIONINFO = "System Module (system.js) version 0.1.1"; +exports.VERSIONINFO = "System Module (system.js) version 0.1.2"; exports.global = global; exports.require = global.require; diff --git a/officetest.js b/officeloader.js similarity index 59% rename from officetest.js rename to officeloader.js index 025d690c..dda0e48c 100644 --- a/officetest.js +++ b/officeloader.js @@ -1,7 +1,36 @@ +// 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) { + // 기존 파일을 여는 경우 인자에 파일 경로 추가 + if (args.length > 0) { + var filename = args[0]; + open(filename); + } else { + test(); + } +} + +function open(filename) { + // 엑셀 인스턴스 생성 + var excel = new Office.Excel(); + + // 엑셀 열기 + excel.open(filename); + + // .... 여기서 작업하세요 .... + + // 엑셀 닫기 + //excel.close(); +} + +function test() { + // 엑셀 인스턴스 생성 var excel = new Office.Excel(); // 질문 목록 @@ -24,7 +53,8 @@ function main(args) { i++; }); + // 엑셀 닫기 //excel.close(); } -exports.main = main; \ No newline at end of file +exports.main = main;