Skip to content

Commit

Permalink
Merge pull request #331 from jmaister/fix-data
Browse files Browse the repository at this point in the history
Add fix data functions
  • Loading branch information
jmaister authored Aug 23, 2020
2 parents 53af19d + 8f4b05f commit f29f80f
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/excellentexport.js

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions excellentexport.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ const ExcellentExport = function() {
array: [...], // Array with data
arrayHasHeader: true, // Array first row is the header
removeColumns: [...], // Array of column indexes (from 0)
filterRowFn: function(row) {return true}
filterRowFn: function(row) {return true}, // Function to decide which rows are returned
fixValue: function(value, row, column) {return fixedValue} // Function to fix values, receiving value, row num, column num
fixArray: function(array) {return array} // Function to manipulate the whole data array
},
...
}
Expand Down Expand Up @@ -211,22 +213,38 @@ const ExcellentExport = function() {
throw new Error('No data for sheet: [' + name + ']');
}

// Filter data
console.log("conf", sheetConf);
// Filter rows
if (sheetConf.filterRowFn) {
if (sheetConf.filterRowFn instanceof Function) {
dataArray = dataArray.filter(sheetConf.filterRowFn);
} else {
throw new Error('Parameter "filterRowFn" must be a function.');
}
}
// Filter columns
if (sheetConf.removeColumns) {
const toRemove = sheetConf.removeColumns.sort().reverse();
toRemove.forEach(function(columnIndex) {
removeColumn(dataArray, columnIndex);
});
}

// Convert data, by value
if (sheetConf.fixValue && typeof sheetConf.fixValue === 'function') {
const fn = sheetConf.fixValue;
dataArray.map((r, rownum) => {
r.map((value, colnum) => {
dataArray[rownum][colnum] = fn(value, rownum, colnum);
});
});
}

// Convert data, whole array
if (sheetConf.fixArray && typeof sheetConf.fixArray === 'function') {
const fn = sheetConf.fixArray;
dataArray = fn(dataArray);
}

// Create sheet
workbook.SheetNames.push(name);
worksheet = XLSX.utils.aoa_to_sheet(dataArray, {sheet: name});
Expand Down
63 changes: 63 additions & 0 deletions index.filters.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<html>
<head>
<meta charset="utf-8">
<title>Export to excel test</title>
<script src="dist/excellentexport.js"></script>
<style>
table, tr, td {
border: 1px black solid;
}
</style>
<script>
function newApi(format) {
return ExcellentExport.convert({
anchor: 'anchorNewApi-' + format,
filename: 'data_123.' + format,
format: format
}, [{
name: 'Sheet Name Here 1',
from: {
table: 'datatable'
},
fixValue: (value, row, col) => {
let v = value.replace(/<br>/gi, "\n");
let strippedString = v.replace(/(<([^>]+)>)/gi, "");
return strippedString;
}
}]);
}
</script>
</head>
<body>
<h1>ExcellentExport.js</h1>

Check on <a href="http://jordiburgos.com">jordiburgos.com</a> and <a href="https://github.com/jmaister/excellentexport">GitHub</a>.

<h3>Test page</h3>

Test table:
<table id="datatable">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
<tr>
<td>hello<br>world</td>
<td><span>text in span</span></td>
<td>abc</td>
<td>def</td>
</tr>
</table>

<br/>

<a href="#" id="anchorNewApi-xlsx" onclick="return newApi('xlsx');">Export to XLSX from array</a>
<br>
<a href="#" id="anchorNewApi-xls" onclick="return newApi('xls');">Export to XLS from array</a>
<br>
<a href="#" id="anchorNewApi-csv" onclick="return newApi('csv');">Export to CSV from array</a>

</body>
</html>
72 changes: 72 additions & 0 deletions test/fixdata.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const assert = require('assert');

import ExcellentExport from '../excellentexport';


describe('Fix data', function() {
beforeEach(() => {
document.body.innerHTML = '';
const element = document.createElement("div");
element.innerHTML = '<a id="anchor">Link</a>';

document.body.appendChild(element);
});

it('should fix values', function() {
const options = {
anchor: 'anchor',
filename: 'data_from_array',
format: 'xlsx'
};

const sheets = [
{
name: 'Sheet Name Here 1',
from: {
array: [
['hello', '<td>hello</td>', 'bye'],
]
},
fixValue: (value, row, col) => {
let v = value.replace(/<br>/gi, "\n");
let strippedString = v.replace(/(<([^>]+)>)/gi, "");
return strippedString;
}
}
];

const workbook = ExcellentExport.convert(options, sheets);
assert.ok(workbook, 'Result must not be null');
});

it('should process the whole array', function() {
const options = {
anchor: 'anchor',
filename: 'data_from_array',
format: 'xlsx'
};

const sheets = [
{
name: 'Sheet Name Here 1',
from: {
array: [
['hello', '<td>hello</td>', 'bye'],
]
},
fixData: (array) => {
return array.map(r => {
return r.map(v => {
return "fixed-" + v;
})
});
}
}
];

const workbook = ExcellentExport.convert(options, sheets);
assert.ok(workbook, 'Result must not be null');
});

});

0 comments on commit f29f80f

Please sign in to comment.