Skip to content

Commit

Permalink
feat(lib): support raw cell descriptor (#68)
Browse files Browse the repository at this point in the history
* Managed data raw cell descriptor for powerful cell formatting

* Managed data raw cell descriptor for powerful cell formatting

* Fixed code standard style

* Fixed code standard style
  • Loading branch information
giano authored and mgcrea committed Aug 30, 2017
1 parent 76d76fd commit 7979d17
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import XLSX from 'xlsx';
const isBoolean = maybeBoolean => typeof maybeBoolean === 'boolean';
const isNumber = maybeNumber => typeof maybeNumber === 'number';
const isString = maybeString => typeof maybeString === 'string';
const isObject = maybeObject => maybeObject !== null && typeof maybeObject === 'object';
const isCellDescriptor = maybeCell => isObject(maybeCell) && 'v' in maybeCell;

const originDate = new Date(Date.UTC(1899, 11, 30));

Expand All @@ -28,7 +30,7 @@ const buildSheetFromMatrix = (data, options = {}) => {
if (data[R][C] === null) {
continue; // eslint-disable-line
}
const cell = {v: data[R][C]};
const cell = isCellDescriptor(data[R][C]) ? data[R][C] : {v: data[R][C]};
const cellRef = XLSX.utils.encode_cell({c: C, r: R});
if (isNumber(cell.v)) {
cell.t = 'n';
Expand All @@ -37,10 +39,11 @@ const buildSheetFromMatrix = (data, options = {}) => {
} else if (cell.v instanceof Date) {
cell.t = 'n';
cell.v = buildExcelDate(cell.v);
cell.z = XLSX.SSF._table[14]; // eslint-disable-line no-underscore-dangle
cell.z = cell.z || XLSX.SSF._table[14]; // eslint-disable-line no-underscore-dangle
} else {
cell.t = 's';
}
if (isNumber(cell.z)) cell.z = XLSX.SSF._table[cell.z]; // eslint-disable-line no-underscore-dangle
workSheet[cellRef] = cell;
}
}
Expand All @@ -56,4 +59,4 @@ const buildSheetFromMatrix = (data, options = {}) => {
return workSheet;
};

export {buildSheetFromMatrix, isBoolean, isNumber, isString};
export {buildSheetFromMatrix, isBoolean, isNumber, isString, isObject, isCellDescriptor};

0 comments on commit 7979d17

Please sign in to comment.