UNPKG

@protobi/exceljs

Version:

Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge

253 lines (249 loc) 8.65 kB
'use strict'; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var Worksheet = require('./worksheet'); var DefinedNames = require('./defined-names'); var XLSX = require('../xlsx/xlsx'); var CSV = require('../csv/csv'); // Workbook requirements // Load and Save from file and stream // Access/Add/Delete individual worksheets // Manage String table, Hyperlink table, etc. // Manage scaffolding for contained objects to write to/read from var Workbook = /*#__PURE__*/function () { function Workbook() { _classCallCheck(this, Workbook); this.category = ''; this.company = ''; this.created = new Date(); this.description = ''; this.keywords = ''; this.manager = ''; this.modified = this.created; this.properties = {}; this.calcProperties = {}; this._worksheets = []; this.subject = ''; this.title = ''; this.views = []; this.media = []; this.pivotTables = []; this._definedNames = new DefinedNames(); } _createClass(Workbook, [{ key: "xlsx", get: function get() { if (!this._xlsx) this._xlsx = new XLSX(this); return this._xlsx; } }, { key: "csv", get: function get() { if (!this._csv) this._csv = new CSV(this); return this._csv; } }, { key: "nextId", get: function get() { // find the next unique spot to add worksheet for (var i = 1; i < this._worksheets.length; i++) { if (!this._worksheets[i]) { return i; } } return this._worksheets.length || 1; } }, { key: "addWorksheet", value: function addWorksheet(name, options) { var id = this.nextId; // if options is a color, call it tabColor (and signal deprecated message) if (options) { if (typeof options === 'string') { // eslint-disable-next-line no-console console.trace('tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { argb: "rbg value" } }'); options = { properties: { tabColor: { argb: options } } }; } else if (options.argb || options.theme || options.indexed) { // eslint-disable-next-line no-console console.trace('tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { ... } }'); options = { properties: { tabColor: options } }; } } var lastOrderNo = this._worksheets.reduce(function (acc, ws) { return (ws && ws.orderNo) > acc ? ws.orderNo : acc; }, 0); var worksheetOptions = Object.assign({}, options, { id: id, name: name, orderNo: lastOrderNo + 1, workbook: this }); var worksheet = new Worksheet(worksheetOptions); this._worksheets[id] = worksheet; return worksheet; } }, { key: "removeWorksheetEx", value: function removeWorksheetEx(worksheet) { delete this._worksheets[worksheet.id]; } }, { key: "removeWorksheet", value: function removeWorksheet(id) { var worksheet = this.getWorksheet(id); if (worksheet) { worksheet.destroy(); } } }, { key: "getWorksheet", value: function getWorksheet(id) { if (id === undefined) { return this._worksheets.find(Boolean); } if (typeof id === 'number') { return this._worksheets[id]; } if (typeof id === 'string') { return this._worksheets.find(function (worksheet) { return worksheet && worksheet.name === id; }); } return undefined; } }, { key: "worksheets", get: function get() { // return a clone of _worksheets return this._worksheets.slice(1).sort(function (a, b) { return a.orderNo - b.orderNo; }).filter(Boolean); } }, { key: "eachSheet", value: function eachSheet(iteratee) { this.worksheets.forEach(function (sheet) { iteratee(sheet, sheet.id); }); } }, { key: "definedNames", get: function get() { return this._definedNames; } }, { key: "clearThemes", value: function clearThemes() { // Note: themes are not an exposed feature, meddle at your peril! this._themes = undefined; } }, { key: "addImage", value: function addImage(image) { // TODO: validation? var id = this.media.length; this.media.push(Object.assign({}, image, { type: 'image' })); return id; } }, { key: "getImage", value: function getImage(id) { return this.media[id]; } }, { key: "model", get: function get() { return { creator: this.creator || 'Unknown', lastModifiedBy: this.lastModifiedBy || 'Unknown', lastPrinted: this.lastPrinted, created: this.created, modified: this.modified, properties: this.properties, worksheets: this.worksheets.map(function (worksheet) { return worksheet.model; }), sheets: this.worksheets.map(function (ws) { return ws.model; }).filter(Boolean), definedNames: this._definedNames.model, views: this.views, company: this.company, manager: this.manager, title: this.title, subject: this.subject, keywords: this.keywords, category: this.category, description: this.description, language: this.language, revision: this.revision, contentStatus: this.contentStatus, themes: this._themes, media: this.media, pivotTables: this.pivotTables, calcProperties: this.calcProperties }; }, set: function set(value) { var _this = this; this.creator = value.creator; this.lastModifiedBy = value.lastModifiedBy; this.lastPrinted = value.lastPrinted; this.created = value.created; this.modified = value.modified; this.company = value.company; this.manager = value.manager; this.title = value.title; this.subject = value.subject; this.keywords = value.keywords; this.category = value.category; this.description = value.description; this.language = value.language; this.revision = value.revision; this.contentStatus = value.contentStatus; this.properties = value.properties; this.calcProperties = value.calcProperties; this._worksheets = []; value.worksheets.forEach(function (worksheetModel) { var id = worksheetModel.id, name = worksheetModel.name, state = worksheetModel.state; var orderNo = value.sheets && value.sheets.findIndex(function (ws) { return ws.id === id; }); var worksheet = _this._worksheets[id] = new Worksheet({ id: id, name: name, orderNo: orderNo, state: state, workbook: _this }); worksheet.model = worksheetModel; }); this._definedNames.model = value.definedNames; this.views = value.views; this._themes = value.themes; this.media = value.media || []; this.pivotTables = value.pivotTables || []; } }]); return Workbook; }(); module.exports = Workbook; //# sourceMappingURL=workbook.js.map