'exceljs mergeCell by first and last value on each row
for example i have console log and the print is look like these
workSheet.eachRow({ includeEmpty: true }, function(row, rowNumber){
row.eachCell(function(cell, colNumber){
if (rowNumber >= 7 && colNumber == 1) {
console.log(`rowNumber : ${rowNumber} || cell.text : ${cell.text}`);
}
}
}
rowNumber : 7 || cell.text : YOGI
rowNumber : 8 || cell.text : YOGI
rowNumber : 9 || cell.text : YOGI
rowNumber : 10 || cell.text : ARIF
rowNumber : 11 || cell.text : WIDODO
rowNumber : 12 || cell.text : WIDODO
rowNumber : 13 || cell.text : JOHN
rowNumber : 14 || cell.text : EXCEL
rowNumber : 15 || cell.text : EXCEL
then how i can manipulation to get first and last of rownumber by on same value, if i do on manual its would like these
workSheet.mergeCells(`A7`, `A9`);
// workSheet.mergeCells(`A10`, `A10`); // on single would skip
workSheet.mergeCells(`A11`, `A12`);
// workSheet.mergeCells(`A13`, `A13`); // on single would skip
workSheet.mergeCells(`A14`, `A15`);
similar on https://stackoverflow.com/a/62231988/8122500 i have manipulation the output code like these
const array= [
{id:"7", name:"YOGI"},
{id:"8", name:"YOGI"},
{id:"9", name:"YOGI"},
{id:"10", name:"YOGI"},
{id:"11", name:"YOGI"},
{id:"12", name:"WIDODO"},
{id:"13", name:"WIDODO"},
{id:"14", name:"WIDODO"},
];
const output = array.reduce((acc, item, key) => {
if (key > 0 && array[key-1].name === item.name) {
if (acc.length && acc[acc.length-1].end === key-1) {
acc[acc.length-1].end = key;
} else {
acc.push({
start: array[key-1].id,
end: key
});
}
}
return acc;
}, []);
console.log(output);
output.forEach(el => {
console.log(`start : ${el.start} , end ${el.end}`);
})
but the print is look like these
start : 7 , end 9
start : 9 , end 11
start : 12 , end 14
what i expected :
start : 7 , end 11
start : 12 , end 14
Solution 1:[1]
already solved i got tricky on last sytnx and modified like these
console.log(`start : ${el.start} , end ${array[el.end].id}`);
in similar comment on https://stackoverflow.com/a/62231988/8122500 , i modified on if else body {}, these the part of code.
if (acc.length && acc[acc.length-1].end === key-1) {
+ acc[acc.length-1].end = key;
- // acc[acc.length-1].end = key;
} else {
- // acc.push({start: key-1, end: key});
+ acc.push({
+ start: array[key-1].id,
+ end: key
+ });
}
the result is
start : 7 , end 11
start : 12 , end 14
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yogi Arif Widodo |
