简体   繁体   English

当行中的数据相同时如何合并单元格?

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

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:我有一个使用excelJS生成的 excel 文件,当date字段相同时,我必须合并单元格,例如:

 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 3A - Date列中A - Date C - row 2 and 32 and 3行中的A - Date同时,我必须合并C - row 2 and 3单元格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,但它只适用于 2 个相同的日期,而不适用于更多相同的日期,

can someone help me?有人能帮我吗?

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.问题是,在我删除一行后,下一行被跳过,因为i一直在向上计数。 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--)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM