diff --git a/source/lib/style/classes/protection.js b/source/lib/style/classes/protection.js new file mode 100644 index 0000000..efcd69f --- /dev/null +++ b/source/lib/style/classes/protection.js @@ -0,0 +1,57 @@ +const xmlbuilder = require('xmlbuilder'); + +class Protection { // §18.8.1 protection (Protection) + /** + * @class Protection + * @param {Object} opts Properties of Protection object + * @param {Boolean} opts.hidden Indicates if text should be shrunk to fit into cell + * @param {Boolean} opts.locked States whether text with newline characters should wrap + * @returns {Protection} + */ + constructor(opts) { + + if (opts.hidden !== undefined) { + if (typeof opts.hidden === 'boolean') { + this.hidden = opts.hidden; + } else { + throw new TypeError('hidden protection option must be of type boolean'); + } + } + + if (opts.locked !== undefined) { + if (typeof opts.locked === 'boolean') { + this.locked = opts.locked; + } else { + throw new TypeError('locked protection option must be of type boolean'); + } + } + } + + /** + * @func Protection.toObject + * @desc Converts the Protection instance to a javascript object + * @returns {Object} + */ + toObject() { + let obj = {}; + + this.hidden !== undefined ? obj.hidden = this.hidden : null; + this.locked !== undefined ? obj.locked = this.locked : null; + + return obj; + } + + /** + * @alias Protection.addToXMLele + * @desc When generating Workbook output, attaches style to the styles xml file + * @func Protection.addToXMLele + * @param {xmlbuilder.Element} ele Element object of the xmlbuilder module + */ + addToXMLele(ele) { + let thisEle = ele.ele('protection'); + this.hidden === true ? thisEle.att('hidden', 1) : null; + this.locked === true ? thisEle.att('locked', 1) : null; + } +} + +module.exports = Protection; \ No newline at end of file diff --git a/source/lib/style/style.js b/source/lib/style/style.js index 9e04422..798a491 100644 --- a/source/lib/style/style.js +++ b/source/lib/style/style.js @@ -6,6 +6,7 @@ const Border = require('./classes/border.js'); const Fill = require('./classes/fill.js'); const Font = require('./classes/font.js'); const NumberFormat = require('./classes/numberFormat.js'); +const Protection = require('./classes/protection.js'); let _getFontId = (wb, font = {}) => { @@ -142,7 +143,11 @@ let _getNumFmt = (wb, val) => { patternType: 'solid', color: 'Yellow' }, - numberFormat: integer or string // §18.8.30 numFmt (Number Format) + numberFormat: integer or string, // §18.8.30 numFmt (Number Format) + protection: { // §18.8.1 + hidden: boolean, + locked: boolean + }, } */ class Style { @@ -158,6 +163,7 @@ class Style { * @param {Object} opts.border Options for creating a Border instance * @param {Object} opts.fill Options for creating a Fill instance * @param {String} opts.numberFormat + * @param {Object} opts.protection Options for creating a Protection instance * @property {Alignment} alignment Alignment instance associated with Style * @property {Border} border Border instance associated with Style * @property {Number} borderId ID of Border instance in the Workbook @@ -166,6 +172,7 @@ class Style { * @property {Font} font Font instance associated with Style * @property {Number} fontId ID of Font instance in the Workbook * @property {String} numberFormat String represenation of the way a number should be formatted + * @property {Protection} protection Protection instance associated with Style * @property {Number} xf XF id of the Style in the Workbook * @returns {Style} */ @@ -202,6 +209,10 @@ class Style { this.pivotButton = null; // attribute boolean } + if (opts.protection !== undefined) { + this.protection = new Protection(opts.protection); + } + if (opts.quotePrefix !== undefined) { this.quotePrefix = null; // attribute boolean } @@ -240,6 +251,11 @@ class Style { thisXF.alignment = this.alignment; } + if (this.protection instanceof Protection) { + thisXF.applyProtection = 1; + thisXF.protection = this.protection; + } + return thisXF; } @@ -273,10 +289,14 @@ class Style { if (this.alignment instanceof Alignment) { obj.alignment = this.alignment.toObject(); } - + if (this.pivotButton !== undefined) { obj.pivotButton = this.pivotButton; } + + if (this.protection instanceof Protection) { + obj.protection = this.protection.toObject(); + } if (this.quotePrefix !== undefined) { obj.quotePrefix = this.quotePrefix; @@ -295,7 +315,7 @@ class Style { let thisEle = ele.ele('xf'); let thisXF = this.xf; Object.keys(thisXF).forEach((a) => { - if (a === 'alignment') { + if (a === 'alignment' || a === 'protection') { thisXF[a].addToXMLele(thisEle); } else { thisEle.att(a, thisXF[a]); @@ -327,10 +347,14 @@ class Style { if (this.alignment instanceof Alignment) { this.alignment.addToXMLele(thisEle); } - + if (this.border instanceof Border) { this.border.addToXMLele(thisEle); } + + if (this.protection instanceof Protection) { + this.protection.addToXMLele(thisEle); + } } }