diff --git a/lib/msoffice.js b/lib/msoffice.js index 605ee60..6aee4bc 100644 --- a/lib/msoffice.js +++ b/lib/msoffice.js @@ -1,5 +1,7 @@ -// msoffice.js +var STD = require("lib/std"); +var FILE = require("lib/file"); +// msoffice.js var FileTypes = {}; // https://support.microsoft.com/en-au/office/file-formats-that-are-supported-in-excel-0943ff2c-6014-4e8d-aaea-b83d51d46247 @@ -104,29 +106,86 @@ FileTypes.Word = [ {"extension": [".xps"], "type": "XPS Document"} ]; -// Example: new Office.Word() +// Example: new Office.Excel() function Excel() { this.Application = CreateObject("Excel.Application"); - this.SupportedFileTypes = FileTypes.Excel; -} + this.Version = this.Application.Version; + this.Application.Visible = true; + + console.info("Microsoft Excel Version:", this.Version); + + this.CurrentWorkbook = null; + this.CurrentWorksheet = null; + + this.open = function(filename) { + if (typeof filename !== "undefined") { + Application.Workbooks.Open(filename); + this.CurrentWorkbook = Application.ActiveWorkbook; + } else { + this.CurrentWorkbook = this.Application.Workbooks.Add(); + } + this.selectWorksheet(1); + }; + + this.close = function() { + try { + this.CurrentWorksheet = null; + this.CurrentWorkbook.Close(); + this.CurrentWorkbook = null; + this.Application.Quit(); + this.Application = null; + } catch (e) { + this.CurrentWorksheet = null; + this.CurrentWorkbook = null; + this.Application = null; + } + }; + + this.saveAs = function(filename) { + try { + this.CurrentWorkbook.saveAs(filename); + return FILE.fileExists(filename); + } catch (e) { + console.error("Could not save a file:", e.message); + return false; + } + }; + + this.selectWorksheet = function(idx) { + if (idx == 0) { + this.CurrentWorksheet = this.Application.ActiveSheet; + } else { + this.CurrentWorksheet = this.CurrentWorkbook.Worksheets(idx); + } + }; + + this.getValueByCell = function(row, col) { + return this.CurrentWorksheet.Cells(row, col).Value; + }; + + this.setValueByCell = function(row, col, value) { + return this.CurrentWorksheet.Cells(row, col).Value = value; + }; +}; +Excel.SupportedFileTypes = FileTypes.Excel; // Example: new Office.PowerPoint() function PowerPoint() { this.Application = CreateObject("PowerPoint.Application"); - this.SupportedFileTypes = FileTypes.PowerPoint; } +PowerPoint.SupportedFileTypes = FileTypes.PowerPoint; // Example: new Office.Word() function Word() { this.Application = CreateObject("Word.Application"); - this.SupportedFileTypes = FileTypes.Word; } +Word.SupportedFileTypes = FileTypes.Word; exports.Excel = Excel; exports.PowerPoint = PowerPoint; exports.Word = Word; -exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.1"; +exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.1.1"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/officetest.js b/officetest.js new file mode 100644 index 0000000..ee99625 --- /dev/null +++ b/officetest.js @@ -0,0 +1,13 @@ +var Office = require("lib/msoffice"); + +function main(args) { + var excel = new Office.Excel(); + + excel.open(); + + excel.setValueByCell(1, 1, "hello world"); + + excel.close(); +} + +exports.main = main; \ No newline at end of file