|
|
@@ -1,6 +1,19 @@
|
|
|
"use strict";
|
|
|
|
|
|
-const _ = require("lodash");
|
|
|
+// const _ = require("lodash");
|
|
|
+const cloneDeep = require("lodash/cloneDeep");
|
|
|
+const filter = require("lodash/filter");
|
|
|
+const findIndex = require("lodash/findIndex");
|
|
|
+const forEach = require("lodash/forEach");
|
|
|
+const clone = require("lodash/clone");
|
|
|
+const values = require("lodash/values");
|
|
|
+const assign = require("lodash/assign");
|
|
|
+const isEqual = require("lodash/isEqual");
|
|
|
+const sortBy = require("lodash/sortBy");
|
|
|
+const isNaN = require("lodash/isNaN");
|
|
|
+const includes = require("lodash/includes");
|
|
|
+const isNumber = require("lodash/isNumber");
|
|
|
+
|
|
|
const Cell = require("./Cell");
|
|
|
const Row = require("./Row");
|
|
|
const Column = require("./Column");
|
|
|
@@ -149,7 +162,7 @@ class Sheet {
|
|
|
// If the existing node covered earlier columns than the new one, we need to have a col node to cover the min up to our new node.
|
|
|
if (existingColNode.attributes.min < columnNumber) {
|
|
|
// Clone the node and set the max to the column before our new col.
|
|
|
- const beforeColNode = _.cloneDeep(existingColNode);
|
|
|
+ const beforeColNode = cloneDeep(existingColNode);
|
|
|
beforeColNode.attributes.max = columnNumber - 1;
|
|
|
|
|
|
// Update the col nodes cache.
|
|
|
@@ -159,14 +172,14 @@ class Sheet {
|
|
|
}
|
|
|
|
|
|
// Make a clone for the new column. Set the min/max to the column number and cache it.
|
|
|
- colNode = _.cloneDeep(existingColNode);
|
|
|
+ colNode = cloneDeep(existingColNode);
|
|
|
colNode.attributes.min = columnNumber;
|
|
|
colNode.attributes.max = columnNumber;
|
|
|
this._colNodes[columnNumber] = colNode;
|
|
|
|
|
|
// If the max of the existing node is greater than the nre one, create a col node for that too.
|
|
|
if (existingColNode.attributes.max > columnNumber) {
|
|
|
- const afterColNode = _.cloneDeep(existingColNode);
|
|
|
+ const afterColNode = cloneDeep(existingColNode);
|
|
|
afterColNode.attributes.min = columnNumber + 1;
|
|
|
for (let i = afterColNode.attributes.min; i <= afterColNode.attributes.max; i++) {
|
|
|
this._colNodes[i] = afterColNode;
|
|
|
@@ -279,7 +292,7 @@ class Sheet {
|
|
|
})
|
|
|
.case('*', hidden => {
|
|
|
if (hidden) {
|
|
|
- const visibleSheets = _.filter(this.workbook().sheets(), sheet => !sheet.hidden());
|
|
|
+ const visibleSheets = filter(this.workbook().sheets(), sheet => !sheet.hidden());
|
|
|
if (visibleSheets.length === 1 && visibleSheets[0] === this) {
|
|
|
throw new Error("This sheet may not be hidden as a workbook must contain at least one visible sheet.");
|
|
|
}
|
|
|
@@ -470,7 +483,7 @@ class Sheet {
|
|
|
* @returns {Range|undefined} The used range or undefined if no cells in the sheet are used.
|
|
|
*/
|
|
|
usedRange() {
|
|
|
- const minRowNumber = _.findIndex(this._rows);
|
|
|
+ const minRowNumber = findIndex(this._rows);
|
|
|
const maxRowNumber = this._rows.length - 1;
|
|
|
|
|
|
let minColumnNumber = 0;
|
|
|
@@ -557,7 +570,7 @@ class Sheet {
|
|
|
* @ignore
|
|
|
*/
|
|
|
forEachExistingColumnNumber(callback) {
|
|
|
- _.forEach(this._colNodes, (node, columnNumber) => {
|
|
|
+ forEach(this._colNodes, (node, columnNumber) => {
|
|
|
if (!node) return;
|
|
|
callback(columnNumber);
|
|
|
});
|
|
|
@@ -570,7 +583,7 @@ class Sheet {
|
|
|
* @ignore
|
|
|
*/
|
|
|
forEachExistingRow(callback) {
|
|
|
- _.forEach(this._rows, (row, rowNumber) => {
|
|
|
+ forEach(this._rows, (row, rowNumber) => {
|
|
|
if (row) callback(row, rowNumber);
|
|
|
});
|
|
|
|
|
|
@@ -828,11 +841,11 @@ class Sheet {
|
|
|
*/
|
|
|
toXmls() {
|
|
|
// Shallow clone the node so we don't have to remove these children later if they don't belong.
|
|
|
- const node = _.clone(this._node);
|
|
|
+ const node = clone(this._node);
|
|
|
node.children = node.children.slice();
|
|
|
|
|
|
// Add the columns if needed.
|
|
|
- this._colsNode.children = _.filter(this._colNodes, (colNode, i) => {
|
|
|
+ this._colsNode.children = filter(this._colNodes, (colNode, i) => {
|
|
|
// Columns should only be present if they have attributes other than min/max.
|
|
|
return colNode && i === colNode.attributes.min && Object.keys(colNode.attributes).length > 2;
|
|
|
});
|
|
|
@@ -841,7 +854,7 @@ class Sheet {
|
|
|
}
|
|
|
|
|
|
// Add the hyperlinks if needed.
|
|
|
- this._hyperlinksNode.children = _.values(this._hyperlinks);
|
|
|
+ this._hyperlinksNode.children = values(this._hyperlinks);
|
|
|
if (this._hyperlinksNode.children.length) {
|
|
|
xmlq.insertInOrder(node, this._hyperlinksNode, nodeOrder);
|
|
|
}
|
|
|
@@ -856,10 +869,10 @@ class Sheet {
|
|
|
// Add the pageMargins if needed.
|
|
|
if (this._pageMarginsNode && this._pageMarginsPresetName) {
|
|
|
// Clone to preserve the current state of this sheet.
|
|
|
- const childNode = _.clone(this._pageMarginsNode);
|
|
|
+ const childNode = clone(this._pageMarginsNode);
|
|
|
if (Object.keys(this._pageMarginsNode.attributes).length) {
|
|
|
// Fill in any missing attribute values with presets.
|
|
|
- childNode.attributes = _.assign(
|
|
|
+ childNode.attributes = assign(
|
|
|
this._pageMarginsPresets[this._pageMarginsPresetName],
|
|
|
this._pageMarginsNode.attributes);
|
|
|
} else {
|
|
|
@@ -870,13 +883,13 @@ class Sheet {
|
|
|
}
|
|
|
|
|
|
// Add the merge cells if needed.
|
|
|
- this._mergeCellsNode.children = _.values(this._mergeCells);
|
|
|
+ this._mergeCellsNode.children = values(this._mergeCells);
|
|
|
if (this._mergeCellsNode.children.length) {
|
|
|
xmlq.insertInOrder(node, this._mergeCellsNode, nodeOrder);
|
|
|
}
|
|
|
|
|
|
// Add the DataValidation cells if needed.
|
|
|
- this._dataValidationsNode.children = _.values(this._dataValidations);
|
|
|
+ this._dataValidationsNode.children = values(this._dataValidations);
|
|
|
if (this._dataValidationsNode.children.length) {
|
|
|
xmlq.insertInOrder(node, this._dataValidationsNode, nodeOrder);
|
|
|
}
|
|
|
@@ -1091,17 +1104,17 @@ class Sheet {
|
|
|
// Validate preset attribute keys.
|
|
|
const pageMarginsAttributeNames = [
|
|
|
'left', 'right', 'top', 'bottom', 'header', 'footer'];
|
|
|
- const isValidPresetAttributeKeys = _.isEqual(
|
|
|
- _.sortBy(pageMarginsAttributeNames),
|
|
|
- _.sortBy(Object.keys(presetAttributes)));
|
|
|
+ const isValidPresetAttributeKeys = isEqual(
|
|
|
+ sortBy(pageMarginsAttributeNames),
|
|
|
+ sortBy(Object.keys(presetAttributes)));
|
|
|
if (isValidPresetAttributeKeys === false) {
|
|
|
throw new Error(`Sheet.pageMarginsPreset: Invalid preset attributes for one or key(s)! - "${Object.keys(presetAttributes)}"`);
|
|
|
}
|
|
|
|
|
|
// Validate preset attribute values.
|
|
|
- _.forEach((attributeValue, attributeName) => {
|
|
|
+ forEach((attributeValue, attributeName) => {
|
|
|
const attributeNumberValue = parseFloat(attributeValue);
|
|
|
- if (_.isNaN(attributeNumberValue) || _.isNumber(attributeNumberValue) === false) {
|
|
|
+ if (isNaN(attributeNumberValue) || isNumber(attributeNumberValue) === false) {
|
|
|
throw new Error(`Sheet.pageMarginsPreset: Invalid preset attribute value! - "${attributeValue}"`);
|
|
|
}
|
|
|
});
|
|
|
@@ -1149,7 +1162,7 @@ class Sheet {
|
|
|
return new ArgHandler('Sheet.pane')
|
|
|
.case(() => {
|
|
|
if (paneNode) {
|
|
|
- const result = _.cloneDeep(paneNode.attributes);
|
|
|
+ const result = cloneDeep(paneNode.attributes);
|
|
|
if (!result.state) result.state = 'split';
|
|
|
return result;
|
|
|
}
|
|
|
@@ -1159,7 +1172,7 @@ class Sheet {
|
|
|
return this;
|
|
|
})
|
|
|
.case(['object'], paneAttributes => {
|
|
|
- const attributes = _.assign({ activePane: 'bottomRight' }, paneAttributes);
|
|
|
+ const attributes = assign({ activePane: 'bottomRight' }, paneAttributes);
|
|
|
checkStateName(attributes.state);
|
|
|
checkActivePane(attributes.activePane);
|
|
|
if (paneNode) {
|
|
|
@@ -1237,7 +1250,7 @@ class Sheet {
|
|
|
*/
|
|
|
_getCheckAttributeNameHelper(functionName, supportedAttributeNames) {
|
|
|
return attributeName => {
|
|
|
- if (!_.includes(supportedAttributeNames, attributeName)) {
|
|
|
+ if (!includes(supportedAttributeNames, attributeName)) {
|
|
|
throw new Error(`Sheet.${functionName}: "${attributeName}" is not supported.`);
|
|
|
}
|
|
|
};
|
|
|
@@ -1372,7 +1385,7 @@ class Sheet {
|
|
|
|
|
|
// Cache the col nodes.
|
|
|
this._colNodes = [];
|
|
|
- _.forEach(this._colsNode.children, colNode => {
|
|
|
+ forEach(this._colsNode.children, colNode => {
|
|
|
const min = colNode.attributes.min;
|
|
|
const max = colNode.attributes.max;
|
|
|
for (let i = min; i <= max; i++) {
|
|
|
@@ -1475,7 +1488,7 @@ class Sheet {
|
|
|
|
|
|
// Search for a preset that matches existing attributes.
|
|
|
for (const presetName in this._pageMarginsPresets) {
|
|
|
- if (_.isEqual(this._pageMarginsNode.attributes, this._pageMarginsPresets[presetName])) {
|
|
|
+ if (isEqual(this._pageMarginsNode.attributes, this._pageMarginsPresets[presetName])) {
|
|
|
this._pageMarginsPresetName = presetName;
|
|
|
break;
|
|
|
}
|