Update lib/msoffice, officeloader #93

This commit is contained in:
Namhyeon Go 2023-12-19 05:12:05 +09:00
parent 94a0b66c12
commit 5f487d9409
2 changed files with 88 additions and 16 deletions

View File

@ -1,4 +1,5 @@
// msoffice.js // msoffice.js
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs // https://github.com/gnh1201/welsonjs
var STD = require("lib/std"); var STD = require("lib/std");
var SYS = require("lib/system"); var SYS = require("lib/system");
@ -114,7 +115,7 @@ function Excel() {
this.version = this.application.Version; this.version = this.application.Version;
this.application.Visible = true; this.application.Visible = true;
console.info("Microsoft Office Excel:", this.version); console.info("Microsoft Office Excel", this.version);
this.currentWorkbook = null; this.currentWorkbook = null;
this.currentWorksheet = null; this.currentWorksheet = null;
@ -127,11 +128,11 @@ function Excel() {
filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path
} }
if (FILE.fileExists(filename)) { if (FILE.fileExists(filename)) {
console.warn("Found the file:", filename); console.info("FOUND", filename);
this.application.Workbooks.Open(filename); this.application.Workbooks.Open(filename);
this.currentWorkbook = this.application.ActiveWorkbook; this.currentWorkbook = this.application.ActiveWorkbook;
} else { } else {
console.warn("File not exists!"); console.warn("NOT FOUND", filename);
this.currentWorkbook = this.application.Workbooks.Add(); this.currentWorkbook = this.application.Workbooks.Add();
} }
} else { } else {
@ -173,6 +174,8 @@ function Excel() {
this.currentWorksheet = this.currentWorkbook.Worksheets(idx); this.currentWorksheet = this.currentWorkbook.Worksheets(idx);
} }
console.log(typeof this.currentWorksheet);
return this; return this;
}; };
@ -215,12 +218,48 @@ Excel.Cell = function(cell) {
// EXAMPLE: new Office.PowerPoint() // EXAMPLE: new Office.PowerPoint()
function PowerPoint() { function PowerPoint() {
this.application = CreateObject("PowerPoint.Application"); 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; PowerPoint.SupportedFileTypes = FileTypes.PowerPoint;
// EXAMPLE: new Office.Word() // EXAMPLE: new Office.Word()
function Word() { function Word() {
this.application = CreateObject("Word.Application"); 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; Word.SupportedFileTypes = FileTypes.Word;
@ -228,7 +267,7 @@ exports.Excel = Excel;
exports.PowerPoint = PowerPoint; exports.PowerPoint = PowerPoint;
exports.Word = Word; 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.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;

View File

@ -1,13 +1,12 @@
// officeloader.js // officeloader.js
// Namhyeon Go <abuse@catswords.net> // Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs // https://github.com/gnh1201/welsonjs
var SYS = require("lib/system"); var SYS = require("lib/system");
var Office = require("lib/msoffice"); 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
if (args.length > 0) { if (args.length > 0) {
var filename = args[0]; var filename = args[0];
open(filename); open(filename);
@ -17,22 +16,56 @@ function main(args) {
} }
function open(filename) { function open(filename) {
// 엑셀 인스턴스 생성 var filetypes = [
var excel = new Office.Excel(); {"application": "excel", "filetypes": Office.Excel.SupportedFileTypes},
{"application": "powerpoint", "filetypes": Office.PowerPoint.SupportedFileTypes},
// 엑셀 열기 {"application": "word", "filetypes": Office.Word.SupportedFileTypes}
excel.open(filename); ];
// .... 여기서 작업하세요 ....
// 엑셀 닫기 var resolved_filetype = filetypes.reduce(function(a, x) {
//excel.close(); 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() { function test() {
// 엑셀 인스턴스 생성 // 엑셀 인스턴스 생성
var excel = new Office.Excel(); var excel = new Office.Excel();
// 질문 목록 // 질문 목록
var questions = [ var questions = [
"엄마가 좋아 아빠가 좋아?", "엄마가 좋아 아빠가 좋아?",