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

使用自动列宽LongestMatchColumnWidthStyleStrategy时,第二次发送同样sheet的邮件,列宽未设置 #1010

Closed
tony-steven opened this issue Dec 23, 2019 · 1 comment
Assignees
Labels
bug Something isn't working developing This feature will be added in future releases

Comments

@tony-steven
Copy link

触发场景描述
表格的sheet有三个,第一次发送邮件,列宽是根据数据自动列宽;第二次发送邮件,列宽未设置
触发Bug的代码

   // 此处生命的CACHE是静态的,类被初始化后,所有类都共享
   private static final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<Integer, Map<Integer, Integer>>(8);

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head,
        Integer relativeRowIndex, Boolean isHead) {
        boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        if (!needSetWidth) {
            return;
        }
        // 此处是从CACHE中获取,第一次发邮件时,CACHE中有3个key(0,1,2),第二次发邮件,sheetNo还是0,1,2,所以取出了上次发邮件的sheetNo对应的内容
        Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
        if (maxColumnWidthMap == null) {
            maxColumnWidthMap = new HashMap<Integer, Integer>(16);
            CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
        }
        Integer columnWidth = dataLength(cellDataList, cell, isHead);
        if (columnWidth < 0) {
            return;
        }
        if (columnWidth > MAX_COLUMN_WIDTH) {
            columnWidth = MAX_COLUMN_WIDTH;
        }
        // maxColumnWidthMap变量根据上面代码逻辑,就是上次发邮件sheet的map,此时columnIndex也是一样的,第二次发邮件时,maxColumnWidth不会为null,且与columnWidth基本相等,就不会进入设置宽度的方法,所以第二次发邮件时,宽度就未设置
        Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
        if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
            maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
        }
    }

提示的异常或者没有达到的效果
期望第二次发邮件时,也应该自动设置列宽,而不是从静态的CACHE获取,因为sheetNo,两次邮件会一样的,只是根据源码逻辑推断,如果是我使用错了,还望告知方案,感谢

@tony-steven tony-steven added the bug Something isn't working label Dec 23, 2019
@zhuangjiaju zhuangjiaju added the pending verification This problem needs to be confirmed label Dec 28, 2019
@zhuangjiaju
Copy link
Collaborator

zhuangjiaju commented Mar 17, 2020

这个就是个bug,cache不能定义static

@zhuangjiaju zhuangjiaju reopened this Mar 17, 2020
@zhuangjiaju zhuangjiaju added developing This feature will be added in future releases and removed pending verification This problem needs to be confirmed labels Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working developing This feature will be added in future releases
Projects
None yet
Development

No branches or pull requests

2 participants