Skip to content

[bugfix] conditionally hide a ExcelColumn from sheetData #156

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

Open
wants to merge 1 commit into
base: master
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
272 changes: 140 additions & 132 deletions dist/ExcelPlugin/components/ExcelFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
Expand Down Expand Up @@ -35,158 +35,166 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var ExcelFile = function (_React$Component) {
_inherits(ExcelFile, _React$Component);
_inherits(ExcelFile, _React$Component);

function ExcelFile(props) {
_classCallCheck(this, ExcelFile);
function ExcelFile(props) {
_classCallCheck(this, ExcelFile);

var _this = _possibleConstructorReturn(this, (ExcelFile.__proto__ || Object.getPrototypeOf(ExcelFile)).call(this, props));
var _this = _possibleConstructorReturn(this, (ExcelFile.__proto__ || Object.getPrototypeOf(ExcelFile)).call(this, props));

_initialiseProps.call(_this);
_initialiseProps.call(_this);

if (_this.props.hideElement) {
_this.download();
} else {
_this.handleDownload = _this.download.bind(_this);
}

_this.createSheetData = _this.createSheetData.bind(_this);
return _this;
if (_this.props.hideElement) {
_this.download();
} else {
_this.handleDownload = _this.download.bind(_this);
}

_createClass(ExcelFile, [{
key: "createSheetData",
value: function createSheetData(sheet) {
var columns = sheet.props.children;
var sheetData = [_react2.default.Children.map(columns, function (column) {
return column.props.label;
})];
var data = typeof sheet.props.data === 'function' ? sheet.props.data() : sheet.props.data;

data.forEach(function (row) {
var sheetRow = [];

_react2.default.Children.forEach(columns, function (column) {
var getValue = typeof column.props.value === 'function' ? column.props.value : function (row) {
return row[column.props.value];
};
var itemValue = getValue(row);
sheetRow.push(isNaN(itemValue) ? itemValue || '' : itemValue);
});

sheetData.push(sheetRow);
});

return sheetData;
_this.createSheetData = _this.createSheetData.bind(_this);
return _this;
}

_createClass(ExcelFile, [{
key: "createSheetData",
value: function createSheetData(sheet) {
var columns = sheet.props.children;
var sheetData = [_react2.default.Children.map(columns, function (column) {
if (![false, undefined, null].includes(column)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ignores falsy column from the sheetData array

return column.props.label;
}
}, {
key: "download",
value: function download() {
var _this2 = this;

var wb = {
SheetNames: _react2.default.Children.map(this.props.children, function (sheet) {
return sheet.props.name;
}),
Sheets: {}
};
})];
var data = typeof sheet.props.data === "function" ? sheet.props.data() : sheet.props.data;

data.forEach(function (row) {
var sheetRow = [];

_react2.default.Children.forEach(this.props.children, function (sheet) {
if (typeof sheet.props.dataSet === 'undefined' || sheet.props.dataSet.length === 0) {
wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromAoA)(_this2.createSheetData(sheet));
} else {
wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromDataSet)(sheet.props.dataSet);
}
});
_react2.default.Children.forEach(columns, function (column) {
if (![false, undefined, null].includes(column)) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ignores falsy column from the sheetRow array

var getValue = typeof column.props.value === "function" ? column.props.value : function (row) {
return row[column.props.value];
};
var itemValue = getValue(row);
sheetRow.push(isNaN(itemValue) ? itemValue || "" : itemValue);
}
});

var fileExtension = this.getFileExtension();
var fileName = this.getFileName();
var wbout = _tempaXlsx2.default.write(wb, { bookType: fileExtension, bookSST: true, type: 'binary' });
sheetData.push(sheetRow);
});

(0, _fileSaver.saveAs)(new Blob([(0, _DataUtil.strToArrBuffer)(wbout)], { type: "application/octet-stream" }), fileName);
}
}, {
key: "getFileName",
value: function getFileName() {
if (this.props.filename === null || typeof this.props.filename !== 'string') {
throw Error('Invalid file name provided');
}
return this.getFileNameWithExtension(this.props.filename, this.getFileExtension());
}
}, {
key: "getFileExtension",
value: function getFileExtension() {
var extension = this.props.fileExtension;

if (extension.length === 0) {
var slugs = this.props.filename.split('.');
if (slugs.length === 0) {
throw Error('Invalid file name provided');
}
extension = slugs[slugs.length - 1];
}

if (this.fileExtensions.indexOf(extension) !== -1) {
return extension;
}

return this.defaultFileExtension;
}
}, {
key: "getFileNameWithExtension",
value: function getFileNameWithExtension(filename, extension) {
return filename + "." + extension;
return sheetData;
}
}, {
key: "download",
value: function download() {
var _this2 = this;

var wb = {
SheetNames: _react2.default.Children.map(this.props.children, function (sheet) {
return sheet.props.name;
}),
Sheets: {}
};

_react2.default.Children.forEach(this.props.children, function (sheet) {
if (typeof sheet.props.dataSet === "undefined" || sheet.props.dataSet.length === 0) {
wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromAoA)(_this2.createSheetData(sheet));
} else {
wb.Sheets[sheet.props.name] = (0, _DataUtil.excelSheetFromDataSet)(sheet.props.dataSet);
}
}, {
key: "render",
value: function render() {
var _props = this.props,
hideElement = _props.hideElement,
element = _props.element;


if (hideElement) {
return null;
} else {
return _react2.default.createElement(
"span",
{ onClick: this.handleDownload },
element
);
}
});

var fileExtension = this.getFileExtension();
var fileName = this.getFileName();
var wbout = _tempaXlsx2.default.write(wb, {
bookType: fileExtension,
bookSST: true,
type: "binary"
});

(0, _fileSaver.saveAs)(new Blob([(0, _DataUtil.strToArrBuffer)(wbout)], { type: "application/octet-stream" }), fileName);
}
}, {
key: "getFileName",
value: function getFileName() {
if (this.props.filename === null || typeof this.props.filename !== "string") {
throw Error("Invalid file name provided");
}
return this.getFileNameWithExtension(this.props.filename, this.getFileExtension());
}
}, {
key: "getFileExtension",
value: function getFileExtension() {
var extension = this.props.fileExtension;

if (extension.length === 0) {
var slugs = this.props.filename.split(".");
if (slugs.length === 0) {
throw Error("Invalid file name provided");
}
}]);
extension = slugs[slugs.length - 1];
}

if (this.fileExtensions.indexOf(extension) !== -1) {
return extension;
}

return ExcelFile;
return this.defaultFileExtension;
}
}, {
key: "getFileNameWithExtension",
value: function getFileNameWithExtension(filename, extension) {
return filename + "." + extension;
}
}, {
key: "render",
value: function render() {
var _props = this.props,
hideElement = _props.hideElement,
element = _props.element;


if (hideElement) {
return null;
} else {
return _react2.default.createElement(
"span",
{ onClick: this.handleDownload },
element
);
}
}
}]);

return ExcelFile;
}(_react2.default.Component);

ExcelFile.props = {
hideElement: _propTypes2.default.bool,
filename: _propTypes2.default.string,
fileExtension: _propTypes2.default.string,
element: _propTypes2.default.any,
children: function children(props, propName, componentName) {
_react2.default.Children.forEach(props[propName], function (child) {
if (child.type !== _ExcelSheet2.default) {
throw new Error('<ExcelFile> can only have <ExcelSheet> as children. ');
}
});
}
hideElement: _propTypes2.default.bool,
filename: _propTypes2.default.string,
fileExtension: _propTypes2.default.string,
element: _propTypes2.default.any,
children: function children(props, propName, componentName) {
_react2.default.Children.forEach(props[propName], function (child) {
if (child.type !== _ExcelSheet2.default) {
throw new Error("<ExcelFile> can only have <ExcelSheet> as children. ");
}
});
}
};
ExcelFile.defaultProps = {
hideElement: false,
filename: "Download",
fileExtension: "xlsx",
element: _react2.default.createElement(
"button",
null,
"Download"
)
hideElement: false,
filename: "Download",
fileExtension: "xlsx",
element: _react2.default.createElement(
"button",
null,
"Download"
)
};

var _initialiseProps = function _initialiseProps() {
this.fileExtensions = ['xlsx', 'xls', 'csv', 'txt', 'html'];
this.defaultFileExtension = 'xlsx';
this.fileExtensions = ["xlsx", "xls", "csv", "txt", "html"];
this.defaultFileExtension = "xlsx";
};

exports.default = ExcelFile;
Loading