html-to-xlsx
Advanced tools
Comparing version 0.6.3 to 0.6.4
@@ -72,5 +72,6 @@ var path = require("path"), | ||
function searchArray(val) { | ||
if (val.col === j) { | ||
return val; | ||
if (val.col <= tmpCol) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
@@ -83,38 +84,34 @@ | ||
var curr_row = 0; | ||
var curr_col = 0; | ||
var col_offset = 0; | ||
var row_offset = []; | ||
for (var i = 0; i < table.rows.length; i++) { | ||
var maxHeight = 0; | ||
for (var j = 0; j < table.rows[i].length; j++) { | ||
//set at the row level column offsets and column position | ||
var maxHeight = 0; | ||
var tmp_offsets = []; | ||
var curr_col = 0; | ||
var col_offset = 0; | ||
var tmpCol = 0; | ||
//On the start of each row, reset the column counters | ||
if(j===0){ | ||
curr_col = 0; | ||
col_offset = 0; | ||
} | ||
//Is the current column in the offset list? | ||
var offset = row_offset.find(searchArray) | ||
//clean out offsets that are no longer valid | ||
row_offset = row_offset.filter(function(offset) { | ||
return (offset.stop <= curr_row) ? false : true; | ||
}); | ||
if(offset) { | ||
//If we have should still be offsetting for a row merge... | ||
if (curr_row < offset.stop) { | ||
curr_col += offset.col_offset || 1; | ||
} | ||
//We should not check for an offset as we have passed the row | ||
//the merge stopped at. Delete that offset from the array | ||
else { | ||
row_offset = row_offset.filter(function (val) { | ||
if (val !== offset) { | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
}); | ||
} | ||
//Is the current column in the offset list? | ||
row_offset.map(function(item) { | ||
//if so add an offset to shift the column start | ||
if (curr_row <= item.stop) { | ||
col_offset += item.col_offset || 1; | ||
} | ||
}); | ||
} | ||
//column iterator | ||
for (var j = 0; j < table.rows[i].length; j++) { | ||
var cell = table.rows[i][j]; | ||
//On the start of each row, reset the column counters | ||
//Use tmpCol to manipulate the column value | ||
curr_col = (j===0) ? 1 : curr_col + 1; | ||
tmpCol = curr_col + col_offset; | ||
if (cell.height > maxHeight) { | ||
@@ -125,12 +122,12 @@ maxHeight = cell.height; | ||
if (cell.width > (maxWidths[j] || 0)) { | ||
sheet1.width(curr_col + 1, cell.width / 7); | ||
sheet1.width(curr_col, cell.width / 7); | ||
maxWidths[j] = cell.width; | ||
} | ||
sheet1.set(curr_col + 1, curr_row + 1, cell.value ? cell.value.replace(/&(?!amp;)/g, '&').replace(/&(?!amp;)/g, '&') : cell.value); | ||
sheet1.align(curr_col + 1, curr_row + 1, cell.horizontalAlign); | ||
sheet1.valign(curr_col + 1, curr_row + 1, cell.verticalAlign === "middle" ? "center" : cell.verticalAlign); | ||
sheet1.set(tmpCol, curr_row + 1, cell.value ? cell.value.replace(/&(?!amp;)/g, '&').replace(/&(?!amp;)/g, '&') : cell.value); | ||
sheet1.align(tmpCol, curr_row + 1, cell.horizontalAlign); | ||
sheet1.valign(tmpCol, curr_row + 1, cell.verticalAlign === "middle" ? "center" : cell.verticalAlign); | ||
if (isColorDefined(cell.backgroundColor)) { | ||
sheet1.fill(curr_col + 1, curr_row + 1, { | ||
sheet1.fill(tmpCol, curr_row + 1, { | ||
type: 'solid', | ||
@@ -141,3 +138,3 @@ fgColor: 'FF' + rgbToHex(cell.backgroundColor), | ||
} | ||
sheet1.font(curr_col + 1, curr_row + 1, { | ||
sheet1.font(tmpCol, curr_row + 1, { | ||
family: '3', | ||
@@ -150,3 +147,3 @@ scheme: 'minor', | ||
sheet1.border(curr_col + 1, curr_row + 1, { | ||
sheet1.border(tmpCol, curr_row + 1, { | ||
left: getBorderStyle(cell.border.left), | ||
@@ -161,4 +158,8 @@ top: getBorderStyle(cell.border.top), | ||
if (cell.rowspan > 1) { | ||
sheet1.merge({col: curr_col+1, row: curr_row+1},{col: curr_col+cell.colspan, row: curr_row+cell.rowspan}); | ||
row_offset.push({col: j, stop: curr_row+cell.rowspan, col_offset: cell.colspan}); | ||
//address colspan at the same time as rowspan | ||
var coloffset = (cell.colspan > 1) ? cell.colspan - 1 : 0 | ||
sheet1.merge({col: tmpCol, row: curr_row+1},{col: tmpCol + coloffset, row: curr_row+cell.rowspan}); | ||
//store the rowspan for later use to shift over the column starting point | ||
tmp_offsets.push({col: tmpCol, stop: curr_row+cell.rowspan, col_offset: cell.colspan}); | ||
curr_col += cell.colspan - 1 | ||
@@ -176,7 +177,8 @@ for (var k = curr_row + 1; k <= cell.rowspan; k++) { | ||
//If we already did rowspan, we did the colspan at the same time so this only does colspan. | ||
//No need to store the colspan as that doesn't carry over to another row | ||
if (cell.colspan > 1 && cell.rowspan === 1) { | ||
sheet1.merge({col: curr_col+1, row: curr_row+1},{col: curr_col+cell.colspan, row: curr_row+1}); | ||
col_offset += cell.colspan; | ||
for (var k = curr_col + 1; k <= cell.colspan; k++) { | ||
var coloffset = (cell.colspan > 1) ? cell.colspan - 1 : 0 | ||
sheet1.merge({col: tmpCol, row: curr_row+1},{col: tmpCol+coloffset, row: curr_row+1}); | ||
curr_col += cell.colspan - 1 | ||
for (var k = tmpCol; k <= cell.colspan; k++) { | ||
sheet1.border(k + 1, curr_row + 1, { | ||
@@ -190,4 +192,2 @@ left: getBorderStyle(cell.border.left), | ||
} | ||
curr_col += cell.colspan | ||
} | ||
@@ -198,9 +198,8 @@ | ||
if (!cell) { | ||
throw new Error('Cell not found, make sure there are td elements inside tr') | ||
throw new Error('Cell not found, make sure there are td elements inside tr') | ||
} | ||
curr_row += cell.rowspan; | ||
row_offset = row_offset.concat(tmp_offsets); | ||
} | ||
workbook.save(function (err) { | ||
@@ -212,3 +211,3 @@ if (err) { | ||
cb(null, fs.createReadStream(path.join(options.tmpDir, id + ".xlsx"))); | ||
}); | ||
}); | ||
} | ||
@@ -215,0 +214,0 @@ |
{ | ||
"name": "html-to-xlsx", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Jan Blaha", |
@@ -176,3 +176,20 @@ var should = require("should"), | ||
}); | ||
}); | ||
}); | ||
it("should parse complex rowspan", function (done) { | ||
strategy(options, | ||
"<table><tr><td rowspan='3'>Row 1 Col 1</td><td>Row 1 Col 2</td>" + | ||
"<td>Row 1 Col 3</td><td>Row 1 Col 4</td></tr><tr><td rowspan='2'>Row 2 Col 1</td>" + | ||
"<td rowspan='2'>Row 2 Col 2</td><td>Row 2 Col 3</td></tr><tr><td>Row 3 Col 3</td>" + | ||
"</tr></table>", | ||
"", function (err, table) { | ||
if (err) | ||
return done(err); | ||
table.rows[0][0].rowspan.should.be.eql(3); | ||
table.rows[0][0].value.should.be.eql("Row 1 Col 1"); | ||
table.rows[1][1].value.should.be.eql("Row 2 Col 2"); | ||
done(); | ||
}); | ||
}); | ||
} | ||
@@ -254,7 +271,7 @@ }); | ||
it("should callback error when row doesn't contain cells", function (done) { | ||
it("should callback error when row doesn't contain cells", function (done) { | ||
conversion("<table><tr>Hello</tr></table>", function (err, res) { | ||
if (err) | ||
return done(); | ||
done(new Error('It should have callback error')); | ||
@@ -261,0 +278,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36371
715