Skip to content

Commit

Permalink
Merge pull request #14 from EyeSeeTea/development
Browse files Browse the repository at this point in the history
Release 4.2.0
  • Loading branch information
adrianq authored Jun 6, 2023
2 parents 6356d41 + 86d566b commit c971df1
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 4 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,7 @@ A cell
* [.hyperlink()](#Cell+hyperlink) ⇒ <code>string</code> \| <code>undefined</code>
* [.hyperlink(hyperlink)](#Cell+hyperlink) ⇒ [<code>Cell</code>](#Cell)
* [.hyperlink(opts)](#Cell+hyperlink) ⇒ [<code>Cell</code>](#Cell)
* [.comment(opts)](#Cell+comment) ⇒ [<code>Cell</code>](#Cell)
* [.dataValidation()](#Cell+dataValidation) ⇒ <code>object</code> \| <code>undefined</code>
* [.dataValidation(dataValidation)](#Cell+dataValidation) ⇒ [<code>Cell</code>](#Cell)
* [.tap(callback)](#Cell+tap) ⇒ [<code>Cell</code>](#Cell)
Expand Down Expand Up @@ -1226,6 +1227,21 @@ Set the hyperlink options on the cell.
| [opts.email] | <code>string</code> | Email address, ignored if opts.hyperlink is set. |
| [opts.emailSubject] | <code>string</code> | Email subject, ignored if opts.hyperlink is set. |

<a name="Cell+comment"></a>

#### cell.comment(opts) ⇒ [<code>Cell</code>](#Cell)
Set or clear the comment on the cell.

**Kind**: instance method of [<code>Cell</code>](#Cell)
**Returns**: [<code>Cell</code>](#Cell) - The cell.

| Param | Type | Description |
| --- | --- | --- |
| opts | <code>Object</code> \| <code>undefined</code> | Options or undefined (remove comment). |
| [opts.text] | <code>string</code> | The comment text to set. |
| [opts.width] | <code>string</code> | Comment box width. |
| [opts.height] | <code>string</code> | Comment box height. |

<a name="Cell+dataValidation"></a>

#### cell.dataValidation() ⇒ <code>object</code> \| <code>undefined</code>
Expand Down Expand Up @@ -2666,6 +2682,8 @@ A worksheet.
* [.pageBreaks()](#Sheet+pageBreaks) ⇒ <code>Object</code>
* [.verticalPageBreaks()](#Sheet+verticalPageBreaks) ⇒ [<code>PageBreaks</code>](#PageBreaks)
* [.horizontalPageBreaks()](#Sheet+horizontalPageBreaks) ⇒ [<code>PageBreaks</code>](#PageBreaks)
* [.comment(address, opts)](#Sheet+comment) ⇒ [<code>Sheet</code>](#Sheet)
* [.conditionalFormatting(address)](#Sheet+conditionalFormatting) ⇒ [<code>Sheet</code>](#Sheet)
* [.hyperlink(address)](#Sheet+hyperlink) ⇒ <code>string</code> \| <code>undefined</code>
* [.hyperlink(address, hyperlink, [internal])](#Sheet+hyperlink) ⇒ [<code>Sheet</code>](#Sheet)
* [.hyperlink(address, opts)](#Sheet+hyperlink) ⇒ [<code>Sheet</code>](#Sheet)
Expand Down Expand Up @@ -3104,6 +3122,34 @@ Gets the horizontal page breaks.

**Kind**: instance method of [<code>Sheet</code>](#Sheet)
**Returns**: [<code>PageBreaks</code>](#PageBreaks) - horizontal PageBreaks.
<a name="Sheet+comment"></a>

#### sheet.comment(address, opts) ⇒ [<code>Sheet</code>](#Sheet)
Set the comment on the cell with the given address.

**Kind**: instance method of [<code>Sheet</code>](#Sheet)
**Returns**: [<code>Sheet</code>](#Sheet) - The sheet.

| Param | Type | Description |
| --- | --- | --- |
| address | <code>string</code> | The address of the commented cell. |
| opts | <code>Object</code> \| <code>undefined</code> | Options or undefined (remove comment). |
| [opts.text] | <code>string</code> | The comment text to set. |
| [opts.width] | <code>string</code> | Comment box width. |
| [opts.height] | <code>string</code> | Comment box height. |

<a name="Sheet+conditionalFormatting"></a>

#### sheet.conditionalFormatting(address) ⇒ [<code>Sheet</code>](#Sheet)
Set the conditional Formatting on the cell with the given address.

**Kind**: instance method of [<code>Sheet</code>](#Sheet)
**Returns**: [<code>Sheet</code>](#Sheet) - The sheet.

| Param | Type | Description |
| --- | --- | --- |
| address | <code>string</code> | The address of the commented cell. |

<a name="Sheet+hyperlink"></a>

#### sheet.hyperlink(address) ⇒ <code>string</code> \| <code>undefined</code>
Expand Down
16 changes: 16 additions & 0 deletions lib/Cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ class Cell {
.handle(arguments);
}

/**
* Set or clear the comment on the cell.
* @param {{}|undefined} opts - Options or undefined (remove comment).
* @param {string} [opts.text] - The comment text to set.
* @param {string} [opts.width] - Comment box width.
* @param {string} [opts.height] - Comment box height.
* @returns {Cell} The cell.
*/
comment() {
return new ArgHandler('Cell.comment')
.case('*', options => {
this.sheet().comment(this.address(), options);
return this;
})
.handle(arguments);
}

/**
* Gets the data validation object attached to the cell.
Expand Down
4 changes: 2 additions & 2 deletions lib/ContentTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class ContentTypes {
name: "Default",
children: [],
attributes: {
ContentType: contentType,
Extension: extension
Extension: extension,
ContentType: contentType
}
};

Expand Down
125 changes: 124 additions & 1 deletion lib/Sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,65 @@ class Sheet {
return this;
}

/**
* Set the comment on the cell with the given address.
* @param {string} address - The address of the commented cell.
* @param {{}|undefined} opts - Options or undefined (remove comment).
* @param {string} [opts.text] - The comment text to set.
* @param {string} [opts.width] - Comment box width.
* @param {string} [opts.height] - Comment box height.
* @returns {Sheet} The sheet.
*/
comment() {
return new ArgHandler('Sheet.comment')
.case(['string', '*'], (address, comment) => {
if (!comment) {
delete this._comments[address]
return;
}
const ref = addressConverter.fromAddress(address);

this._comments[address] = {
width: "200pt",
height: "50pt",
...comment,
row: ref.rowNumber,
column: ref.columnNumber
};

return this;
})
.handle(arguments);
}

/**
* Set the conditional Formatting on the cell with the given address.
* @param {string} address - The address of the commented cell.
* @returns {Sheet} The sheet.
*/
conditionalFormatting() {
return new ArgHandler('Sheet.conditionalFormatting')
.case(['string', '*'], (address, conditionalFormatting) => {
if (!conditionalFormatting) {
delete this._conditionalFormatting[address];
return
}

const { style } = conditionalFormatting;
const dxStyle = this.workbook().styleSheet().addDxStyle();
Object.keys(style).forEach(name => dxStyle.style(name, style[name]));

this._conditionalFormatting[address] = {
type: conditionalFormatting.type,
formula: conditionalFormatting.formula,
priority: conditionalFormatting.priority,
styleId: dxStyle.id(),
};
return this;
})
.handle(arguments);
}

/**
* Get the hyperlink attached to the cell with the given address.
* @param {string} address - The address of the hyperlinked cell.
Expand Down Expand Up @@ -1089,6 +1148,7 @@ class Sheet {
error: this._dataValidations[address].attributes.error,
errorTitle: this._dataValidations[address].attributes.errorTitle,
operator: this._dataValidations[address].attributes.operator,
errorStyle: this._dataValidations[address].attributes.errorStyle,
formula1: this._dataValidations[address].children[0].children[0],
formula2: this._dataValidations[address].children[1] ? this._dataValidations[address].children[1].children[0] : undefined
};
Expand Down Expand Up @@ -1117,6 +1177,7 @@ class Sheet {
error: '',
errorTitle: '',
operator: '',
errorStyle: '',
sqref: address
},
children: [
Expand Down Expand Up @@ -1145,6 +1206,7 @@ class Sheet {
error: obj.error,
errorTitle: obj.errorTitle,
operator: obj.operator,
errorStyle: obj.errorStyle,
sqref: address
},
children: [
Expand Down Expand Up @@ -1200,6 +1262,40 @@ class Sheet {
xmlq.insertInOrder(node, this._hyperlinksNode, nodeOrder);
}

// Add the conditional formatting if needed.
if (Object.keys(this._conditionalFormatting).length > 0) {
const keys = Object.keys(this._conditionalFormatting);

keys.forEach((address, idx) => {
const conditionalFormatting = this._conditionalFormatting[address];

const conditionalFormattingNode = {
name: 'conditionalFormatting',
attributes: { "sqref": address },
children: [
{
name: "cfRule",
attributes: {
type: conditionalFormatting.type,
priority: conditionalFormatting.priority.toString(),
text: "",
dxfId: idx.toString(),
},
children: [
{
name: "formula",
attributes: {},
children: conditionalFormatting.formula,
},
],
},
],
}

xmlq.insertInOrder(node, conditionalFormattingNode, nodeOrder);
})
}

// Add the printOptions if needed.
if (this._printOptionsNode) {
if (Object.keys(this._printOptionsNode.attributes).length) {
Expand Down Expand Up @@ -1253,6 +1349,32 @@ class Sheet {
}
});

// Add the comment node and relationship to sheet
if (Object.keys(this._comments).length > 0) {
const sheetId = this._idNode.attributes.sheetId;

let vmlDrawingRelationship = this._relationships.findByType("vmlDrawing");
if (!vmlDrawingRelationship) {
vmlDrawingRelationship = this._relationships.add(
"vmlDrawing",
`../drawings/vmlDrawing${sheetId}.vml`
)
}

if (!xmlq.hasChild("legacyDrawing")) {
const legacyDrawingNode = {
name: 'legacyDrawing',
attributes: { "r:id": vmlDrawingRelationship.attributes.Id },
children: [],
};
xmlq.insertInOrder(node, legacyDrawingNode, nodeOrder);
}

if (!this._relationships.findByType("comments")) {
this._relationships.add("comments", `../comments${sheetId}.xml`)
}
}

// Add the drawing node and relationship to sheet
if (Object.keys(this._drawings).length > 0) {
const sheetId = this._idNode.attributes.sheetId;
Expand Down Expand Up @@ -1731,6 +1853,8 @@ class Sheet {
this._hyperlinks = {};
this._autoFilter = null;
this._drawings = {};
this._comments = {};
this._conditionalFormatting = {};
this._sharedFormulas = {};

// Create the relationships.
Expand Down Expand Up @@ -1873,7 +1997,6 @@ class Sheet {
this._hyperlinks[hyperlinkNode.attributes.ref] = hyperlinkNode;
});


// Create the printOptions.
this._printOptionsNode = xmlq.findChild(this._node, "printOptions");
if (this._printOptionsNode) {
Expand Down
18 changes: 18 additions & 0 deletions lib/StyleSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ class StyleSheet {
this._cacheNumberFormats();
}

addDxStyle() {
const fontNode = { name: "font", attributes: {}, children: [] };
const fillNode = { name: "fill", attributes: {}, children: [] };
const borderNode = { name: "border", attributes: {}, children: [] };
const dxfNode = { name: "dxf", attributes: {}, children: [fontNode, fillNode, borderNode] };

this._dxfsNode.children.push(dxfNode);
const id = this._dxfsNode.children.length - 1;
return new Style(this, id, null, fontNode, fillNode, borderNode);
}

/**
* Create a style.
* @param {number} [sourceId] - The source style ID to copy, if provided.
Expand Down Expand Up @@ -206,6 +217,12 @@ class StyleSheet {
this._fillsNode = xmlq.findChild(this._node, "fills");
this._bordersNode = xmlq.findChild(this._node, "borders");
this._cellXfsNode = xmlq.findChild(this._node, "cellXfs");
this._dxfsNode = xmlq.findChild(this._node, "dxfs");

if (!this._dxfsNode) {
this._dxfsNode = { name: "dxfs", attributes: {}, children: [] };
xmlq.insertAfter(this._node, this._dxfsNode, this._cellXfs);
}

if (!this._numFmtsNode) {
this._numFmtsNode = {
Expand All @@ -224,6 +241,7 @@ class StyleSheet {
delete this._fillsNode.attributes.count;
delete this._bordersNode.attributes.count;
delete this._cellXfsNode.attributes.count;
delete this._dxfsNode.attributes.count;
}
}

Expand Down
Loading

0 comments on commit c971df1

Please sign in to comment.