Skip to content

Commit

Permalink
ignore hidden table cells
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty1s committed Oct 26, 2016
1 parent 61ee766 commit 6b3a55a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
4 changes: 2 additions & 2 deletions dist/table2excel.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "table2excel",
"version": "1.0.2",
"version": "1.0.3",
"description": "convert and download html tables to a xlsx-file that can be opened in Microsoft Excel",
"keywords": [
"html table",
Expand Down
70 changes: 37 additions & 33 deletions src/helpers/table-to-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,47 @@ export default function tableToData(table) {
cells.push([]);

// iterate over all cells in the row
Array.from(row.querySelectorAll('td, th')).forEach(cell => {
ranges.forEach(range => {
if ( // we are in a rowspan (already saved in ranges)
rowIndex >= range.s.r &&
rowIndex <= range.e.r &&
cells[rowIndex].length >= range.s.c &&
cells[rowIndex].length <= range.e.c
) {
// ... fill the cells with empty values
for (let i = range.s.c; i < range.e.c; i++) cells[rowIndex].push(null);
}
});
Array.from(row.querySelectorAll('td, th'))
.filter(cell => cell.style.display !== 'none')
.forEach(cell => {
ranges.forEach(range => {
if ( // we are in a rowspan (already saved in ranges)
rowIndex >= range.s.r &&
rowIndex <= range.e.r &&
cells[rowIndex].length >= range.s.c &&
cells[rowIndex].length <= range.e.c
) {
// ... fill the cells with empty values
for (let i = range.s.c; i < range.e.c; i++) {
cells[rowIndex].push(null);
}
}
});

// detect rowspan or colspan
const colspan = parseInt(cell.colSpan, 10) || 1;
const rowspan = parseInt(cell.rowSpan, 10) || 1;
// detect rowspan or colspan
const colspan = parseInt(cell.colSpan, 10) || 1;
const rowspan = parseInt(cell.rowSpan, 10) || 1;

if (rowspan > 1 || colspan > 1) {
ranges.push({
s: {
r: rowIndex,
c: cells[rowIndex].length,
},
e: {
r: rowIndex + rowspan - 1,
c: cells[rowIndex].length + colspan - 1,
},
});
}
if (rowspan > 1 || colspan > 1) {
ranges.push({
s: {
r: rowIndex,
c: cells[rowIndex].length,
},
e: {
r: rowIndex + rowspan - 1,
c: cells[rowIndex].length + colspan - 1,
},
});
}

cells[rowIndex].push(cell);
cells[rowIndex].push(cell);

// if we are in a following colspan ...
if (colspan > 1) {
for (let i = 1; i < colspan; i++) cells[rowIndex].push(null);
}
});
// if we are in a following colspan ...
if (colspan > 1) {
for (let i = 1; i < colspan; i++) cells[rowIndex].push(null);
}
});
});

return { cells, ranges };
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Tests', () => {
'<td>07 19, 2016 13:37:00</td>' +
'</tr><tr>' +
'<td><input type="text" value="input" /></td>' +
'<td style="display: none">this should not appear</td>' +
'<td><select>' +
'<option value="1">option 1</option>' +
'<option value="2">option 2</option>' +
Expand Down

0 comments on commit 6b3a55a

Please sign in to comment.