Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protection to styles #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions source/lib/style/classes/protection.js
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 28 additions & 4 deletions source/lib/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) => {

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -240,6 +251,11 @@ class Style {
thisXF.alignment = this.alignment;
}

if (this.protection instanceof Protection) {
thisXF.applyProtection = 1;
thisXF.protection = this.protection;
}

return thisXF;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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]);
Expand Down Expand Up @@ -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);
}
}
}

Expand Down