How to merge cells when data in the row is this same?

695 views Asked by At

I have an excel file generated with using excelJS and i must to merge cells when the date field is this same, so for example:

 Date | Hours | Total hours
14.10 |   3   |    3
15.10 |   4   |    [THIS CELL MUST BE MERGED WITH CELL BELOW]
15.10 |   1   |    [THIS CELL MUST BE MERGED WITH CELL ABOVE]
17.10 |   8   |    8
19.10 |   5   |    5

when date in column A - Date is this same in rows 2 and 3 then i must merge cells in column C - row 2 and 3

I try do it in this way:

let data = await this.getDataToExcel();

        for(let i=0; i<data.length; i++) {
            if(moment(data[i].date_service).isSame(data[i+1 == data.length ? i-1 : i+1].date_service)){
                workSheet.mergeCells(`C${6+i}:C${7+i}`);
            } 
        }

but it work only for 2 this same date and not for many more this same dates,

can someone help me?

1

There are 1 answers

2
Seven Up On

I once had a similar issue, where I was iterating through rows from top to bottom and deleted some. The problem was, that after I deleted a row the next one was skipped since i kept counting upwards. For me, it solved the problem to iterate through the rows from bottom to top. So maybe you could try:

for(let i=data.length; i>0; i--)