Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

复杂表头合并时,在某些场景下报错 #1662

Closed
liubenlong opened this issue Nov 16, 2020 · 2 comments
Closed

复杂表头合并时,在某些场景下报错 #1662

liubenlong opened this issue Nov 16, 2020 · 2 comments
Assignees
Labels
bug Something isn't working development completed Development completed, waiting for release pending verification This problem needs to be confirmed

Comments

@liubenlong
Copy link
Contributor

建议先去看文档
复杂表头合并时,在某些场景下报错
触发场景描述

当head是这种数据时,列头合并会出错:

private List<List<String>> head() {
        List<List<String>> list = new ArrayList<>();
        List<String> head1 = new ArrayList<>();
        head1.add("xx" );
        head1.add("日期" );
        List<String> head2 = new ArrayList<>();
        head2.add("日期" );
        list.add(head1);
        list.add(head2);
        return list;
    }

原因是在计算合并区域的时候,向右扩展计算不严谨,应该校验一下其父节点是否一致,如果一致,则进行合并,如果不一致则不合并

触发Bug的代码

/**
     * Calculate all cells that need to be merged
     *
     * @return cells that need to be merged
     */
    public List<CellRange> headCellRangeList() {
        List<CellRange> cellRangeList = new ArrayList<CellRange>();
        Set<String> alreadyRangeSet = new HashSet<String>();
        List<Head> headList = new ArrayList<Head>(getHeadMap().values());
        for (int i = 0; i < headList.size(); i++) {
            Head head = headList.get(i);
            List<String> headNameList = head.getHeadNameList();
            for (int j = 0; j < headNameList.size(); j++) {
                if (alreadyRangeSet.contains(i + "-" + j)) {
                    continue;
                }
                alreadyRangeSet.add(i + "-" + j);
                String headName = headNameList.get(j);
                int lastCol = i;
                int lastRow = j;
                for (int k = i + 1; k < headList.size(); k++) {
                    if (headList.get(k).getHeadNameList().get(j).equals(headName)) {
                        alreadyRangeSet.add(k + "-" + j);
                        lastCol = k;
                    } else {
                        break;
                    }
                }
                Set<String> tempAlreadyRangeSet = new HashSet<String>();
                outer:
                for (int k = j + 1; k < headNameList.size(); k++) {
                    for (int l = i; l <= lastCol; l++) {
                        if (headList.get(l).getHeadNameList().get(k).equals(headName)) {
                            tempAlreadyRangeSet.add(l + "-" + k);
                        } else {
                            break outer;
                        }
                    }
                    lastRow = k;
                    alreadyRangeSet.addAll(tempAlreadyRangeSet);
                }
                if (j == lastRow && i == lastCol) {
                    continue;
                }
                cellRangeList.add(new CellRange(j, lastRow, i, lastCol));
            }
        }
        return cellRangeList;
    }

上述代码中 if (headList.get(k).getHeadNameList().get(j).equals(headName))这个判断,应该递归向上计算,直至第0行表头。当前第一个的所有父级单元格不同的时候不可以合并单元格

提示的异常或者没有达到的效果

@liubenlong liubenlong added the bug Something isn't working label Nov 16, 2020
@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label Nov 19, 2020
@SmireG SmireG mentioned this issue May 19, 2021
2 tasks
@SmireG
Copy link
Contributor

SmireG commented May 19, 2021

@zhuangjiaju @liubenlong 尝试解决了这个BUG,并提了个pr

@lethal233 lethal233 mentioned this issue May 20, 2021
3 tasks
zhuangjiaju added a commit that referenced this issue Sep 15, 2021
@zhuangjiaju zhuangjiaju added the development completed Development completed, waiting for release label Sep 15, 2021
@zhuangjiaju
Copy link
Collaborator

已经在3.0.0-beta1 版本修复,beta版本会在一个月内升级成正式版。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working development completed Development completed, waiting for release pending verification This problem needs to be confirmed
Projects
None yet
Development

No branches or pull requests

3 participants