welsonjs/lib/msoffice.js

228 lines
7.0 KiB
JavaScript
Raw Permalink Normal View History

2023-12-11 09:10:15 +00:00
// msoffice.js
2023-12-18 20:12:05 +00:00
// Namhyeon Go <abuse@catswords.net>
2023-12-11 09:10:15 +00:00
// https://github.com/gnh1201/welsonjs
2023-11-27 07:56:18 +00:00
var STD = require("lib/std");
2023-11-27 10:14:11 +00:00
var SYS = require("lib/system");
2023-11-27 07:56:18 +00:00
var FILE = require("lib/file");
var FileTypes = require("lib/filetypes");
2023-11-24 08:36:28 +00:00
// EXAMPLE: new Office.Excel()
2023-11-24 08:36:28 +00:00
function Excel() {
2023-12-11 08:53:40 +00:00
this.application = CreateObject("Excel.Application");
2023-12-11 08:56:31 +00:00
this.version = this.application.Version;
2023-12-11 08:53:40 +00:00
this.application.Visible = true;
2023-11-27 07:56:18 +00:00
2023-12-18 20:12:05 +00:00
console.info("Microsoft Office Excel", this.version);
2023-11-27 07:56:18 +00:00
2023-12-11 08:53:40 +00:00
this.currentWorkbook = null;
this.currentWorksheet = null;
this.range = null;
2023-11-27 07:56:18 +00:00
this.open = function(filename) {
if (typeof filename !== "undefined") {
2023-11-27 10:14:11 +00:00
// check type of the path
if (filename.indexOf(":\\") < 0 && filename.indexOf(":/") < 0) {
filename = SYS.getCurrentWorkingDirectory() + "\\" + filename; // get absolute path
}
if (FILE.fileExists(filename)) {
2023-12-18 20:12:05 +00:00
console.info("FOUND", filename);
2023-12-11 08:53:40 +00:00
this.application.Workbooks.Open(filename);
this.currentWorkbook = this.application.ActiveWorkbook;
2023-11-27 10:14:11 +00:00
} else {
2023-12-18 20:12:05 +00:00
console.warn("NOT FOUND", filename);
2023-12-11 08:53:40 +00:00
this.currentWorkbook = this.application.Workbooks.Add();
2023-11-27 10:14:11 +00:00
}
2023-11-27 07:56:18 +00:00
} else {
2023-12-11 08:53:40 +00:00
this.currentWorkbook = this.application.Workbooks.Add();
2023-11-27 07:56:18 +00:00
}
this.selectWorksheet(1);
2023-11-27 10:14:11 +00:00
return this;
2023-11-27 07:56:18 +00:00
};
this.close = function() {
try {
2023-12-11 08:53:40 +00:00
this.currentWorksheet = null;
this.currentWorkbook.Close();
this.currentWorkbook = null;
this.application.Quit();
this.application = null;
2023-11-27 07:56:18 +00:00
} catch (e) {
2023-12-11 08:53:40 +00:00
this.currentWorksheet = null;
this.currentWorkbook = null;
this.application = null;
2023-11-27 07:56:18 +00:00
}
};
this.saveAs = function(filename) {
try {
2023-12-11 08:53:40 +00:00
this.currentWorkbook.saveAs(filename);
2023-11-27 07:56:18 +00:00
return FILE.fileExists(filename);
} catch (e) {
console.error("Could not save a file:", e.message);
return false;
}
};
this.selectWorksheet = function(idx) {
if (idx == 0) {
2023-12-11 08:53:40 +00:00
this.currentWorksheet = this.application.ActiveSheet;
2023-11-27 07:56:18 +00:00
} else {
2023-12-11 08:53:40 +00:00
this.currentWorksheet = this.currentWorkbook.Worksheets(idx);
2023-11-27 07:56:18 +00:00
}
2023-11-27 10:14:11 +00:00
return this;
2023-11-27 07:56:18 +00:00
};
2023-12-11 08:53:40 +00:00
2023-12-11 09:09:53 +00:00
this.getRange = function(range) {
return new Excel.Range(this.currentWorksheet.Range(range));
2023-12-11 08:53:40 +00:00
};
2023-12-11 09:18:25 +00:00
this.getCellByPosition = function(row, col) {
2023-12-11 09:09:53 +00:00
return new Excel.Cell(this.currentWorksheet.Cells(row, col));
2023-11-27 07:56:18 +00:00
};
};
2023-12-11 09:09:53 +00:00
Excel.Range = function(range) {
this.range = range;
2023-12-11 09:18:25 +00:00
this.getCellByPosition = function(row, col) {
2023-12-11 09:09:53 +00:00
return new Excel.Cell(this.range.Cells(row, col));
};
};
Excel.Cell = function(cell) {
this.cell = cell;
2023-12-11 09:18:47 +00:00
// EXAMPLE: excel.getCellByPosition(1, 3).setValue("Hello world!");
this.setValue = function(value) {
this.cell.Value = value;
};
this.getValue = function() {
return this.cell.Value;
};
// EXAMPLE: excel.getCellByPosition(1, 3).setFormula("=SUM(A1:A2)");
this.setFormula = function(formula) {
if (formula.indexOf('=') != 0) {
console.warn("Be careful because it may not be the correct formula.");
}
this.cell.Formula = formula;
}
this.getFormula = function() {
return this.call.Formula;
}
};
Excel.FileExtensions = FileTypes.getExtensionsByOpenWith("msexcel");
2023-11-24 08:36:28 +00:00
// EXAMPLE: new Office.PowerPoint()
2023-11-24 08:36:28 +00:00
function PowerPoint() {
2023-12-11 08:53:40 +00:00
this.application = CreateObject("PowerPoint.Application");
2023-12-18 20:12:05 +00:00
this.version = this.application.Version;
this.application.Visible = true;
console.info("Microsoft Office PowerPoint", this.version);
2024-06-01 17:00:17 +00:00
this.currentPresentation = null;
2023-12-18 20:12:05 +00:00
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);
2024-06-01 17:00:17 +00:00
this.application.Presentations.Open(filename);
this.currentPresentation = this.application.ActivePresentation;
2023-12-18 20:12:05 +00:00
} else {
console.warn("NOT FOUND", filename);
2024-06-01 17:00:17 +00:00
this.currentPresentation = this.application.Presentations.Add(true);
2023-12-18 20:12:05 +00:00
}
2024-06-01 17:00:17 +00:00
} else {
2024-06-01 17:14:23 +00:00
this.currentPresentation = this.application.Presentations.Add(true);
}
//this.selectPresentation(1);
2024-06-01 17:00:17 +00:00
};
2024-06-01 17:14:23 +00:00
this.selectPresentation = function(idx) {
2024-06-01 17:00:17 +00:00
if (idx == 0) {
2024-06-01 17:14:23 +00:00
this.currentPresentation = this.application.ActivePresentation;
2024-06-01 17:00:17 +00:00
} else {
2024-06-01 17:14:23 +00:00
this.currentPresentation = this.application.Presentations(idx);
2023-12-18 20:12:05 +00:00
}
2024-06-01 17:00:17 +00:00
return this;
2023-12-18 20:12:05 +00:00
};
2024-06-01 17:00:17 +00:00
2024-06-01 17:14:23 +00:00
this.saveAs = function(filename) {
try {
this.currentPresentation.saveAs(filename);
return FILE.fileExists(filename);
} catch (e) {
console.error("Could not save a file:", e.message);
return false;
}
};
2024-06-01 17:00:17 +00:00
2024-06-01 17:14:23 +00:00
this.close = function() {
try {
this.currentPresentation.Close()
this.currentPresentation = null;
this.application = null;
} catch (e) {
this.currentPresentation = null;
this.application = null;
}
};
2024-06-01 17:00:17 +00:00
2024-06-01 17:14:23 +00:00
// get all slides
this.getSlides = function() {
var slides = [];
var slideEnum = new Enumerator(this.currentPresentation.Slides);
for (; !slideEnum.atEnd(); slideEnum.moveNext()) {
slides.push(new PowerPoint.Slide(slideEnum.item()));
}
return slides;
};
2023-11-24 08:36:28 +00:00
}
PowerPoint.Slide = function(slide) {
this.slide = slide;
2024-06-01 17:00:17 +00:00
2024-06-01 17:14:23 +00:00
this.getShapes = function() {
// todo
};
};
PowerPoint.Shape = function(shape) {
this.shape = shape;
}
PowerPoint.FileExtensions = FileTypes.getExtensionsByOpenWith("msppt");
2023-11-24 08:36:28 +00:00
// EXAMPLE: new Office.Word()
2023-11-24 08:36:28 +00:00
function Word() {
2023-12-11 08:53:40 +00:00
this.application = CreateObject("Word.Application");
2023-12-18 20:12:05 +00:00
this.version = this.application.Version;
this.application.Visible = true;
console.info("Microsoft Office Word", this.version);
2023-12-18 20:12:05 +00:00
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);
}
}
};
2023-11-24 08:36:28 +00:00
}
Word.FileExtensions = FileTypes.getExtensionsByOpenWith("msword");
2023-11-24 08:36:28 +00:00
exports.Excel = Excel;
exports.PowerPoint = PowerPoint;
exports.Word = Word;
exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.2.0";
2023-11-24 08:36:28 +00:00
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;