/** * Returns the list of merged regions. If you want multiple regions, this is * faster than calling {@link #getMergedRegion(int)} each time. * * @return the list of merged regions */ @Override public List<CellRangeAddress> getMergedRegions() { List<CellRangeAddress> addresses = new ArrayList<>(); CTMergeCells ctMergeCells = worksheet.getMergeCells(); if(ctMergeCells == null) { return addresses; } for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) { String ref = ctMergeCell.getRef(); addresses.add(CellRangeAddress.valueOf(ref)); } return addresses; }
/** * Adds a merged region of cells (hence those cells form one). * If validate is true, check to make sure adding the merged region to the sheet doesn't create a corrupt workbook * If validate is false, skips the expensive merged region checks, but may produce a corrupt workbook. * * @param region to merge * @param validate whether to validate merged region * @return index of this region * @throws IllegalArgumentException if region contains fewer than 2 cells (this check is inexpensive and is performed regardless of <tt>validate</tt>) * @throws IllegalStateException if region intersects with a multi-cell array formula * @throws IllegalStateException if region intersects with an existing region on this sheet */ private int addMergedRegion(CellRangeAddress region, boolean validate) { if (region.getNumberOfCells() < 2) { throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells"); } region.validate(SpreadsheetVersion.EXCEL2007); if (validate) { // throw IllegalStateException if the argument CellRangeAddress intersects with // a multi-cell array formula defined in this sheet validateArrayFormulas(region); // Throw IllegalStateException if the argument CellRangeAddress intersects with // a merged region already in this sheet validateMergedRegions(region); } CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells(); CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); ctMergeCell.setRef(region.formatAsString()); return ctMergeCells.sizeOfMergeCellArray(); }
/** * Returns the merged region at the specified index. If you want multiple * regions, it is faster to call {@link #getMergedRegions()} than to call * this each time. * * @return the merged region at the specified index */ @Override public CellRangeAddress getMergedRegion(int index) { CTMergeCells ctMergeCells = worksheet.getMergeCells(); if(ctMergeCells == null) { throw new IllegalStateException("This worksheet does not contain merged regions"); } CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index); String ref = ctMergeCell.getRef(); return CellRangeAddress.valueOf(ref); }
@Override public int addMergedRegionUnsafe(CellRangeAddress region) { CTWorksheet worksheet = _sh.getCTWorksheet(); CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells(); CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); ctMergeCell.setRef(region.formatAsString()); // Don't invoke ctMergeCells.sizeOfMergeCellArray() because it's very slow and not needed in our case. // See https://bz.apache.org/bugzilla/show_bug.cgi?id=60397#c5 for details. return 0; } }
/** * @return the merged region at the specified index * @throws IllegalStateException if this worksheet does not contain merged regions */ public CellRangeAddress getMergedRegion(int index) { CTMergeCells ctMergeCells = worksheet.getMergeCells(); if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions"); CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index); String ref = ctMergeCell.getRef(); return CellRangeAddress.valueOf(ref); }
/** * Adds a merged region of cells (hence those cells form one). * * @param region (rowfrom/colfrom-rowto/colto) to merge * @return index of this region */ public int addMergedRegion(CellRangeAddress region) { region.validate(SpreadsheetVersion.EXCEL2007); // throw IllegalStateException if the argument CellRangeAddress intersects with // a multi-cell array formula defined in this sheet validateArrayFormulas(region); CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells(); CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); ctMergeCell.setRef(region.formatAsString()); return ctMergeCells.sizeOfMergeCellArray(); }
private void collectMergedCells() { mergedCellMap = new HashMap<String, MergedRegion>(); if (sheet.getCTWorksheet().getMergeCells() == null) { return; } for (CTMergeCell mergeCell : sheet.getCTWorksheet().getMergeCells().getMergeCellArray()) { String ref = mergeCell.getRef(); String refBase = ref.split(":")[0]; mergedCellMap.put(refBase, new MergedRegion(ref)); } mergedRegions = mergedCellMap.values(); }
/** * Adds a merged region of cells (hence those cells form one). * If validate is true, check to make sure adding the merged region to the sheet doesn't create a corrupt workbook * If validate is false, skips the expensive merged region checks, but may produce a corrupt workbook. * * @param region to merge * @param validate whether to validate merged region * @return index of this region * @throws IllegalArgumentException if region contains fewer than 2 cells (this check is inexpensive and is performed regardless of <tt>validate</tt>) * @throws IllegalStateException if region intersects with a multi-cell array formula * @throws IllegalStateException if region intersects with an existing region on this sheet */ private int addMergedRegion(CellRangeAddress region, boolean validate) { if (region.getNumberOfCells() < 2) { throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells"); } region.validate(SpreadsheetVersion.EXCEL2007); if (validate) { // throw IllegalStateException if the argument CellRangeAddress intersects with // a multi-cell array formula defined in this sheet validateArrayFormulas(region); // Throw IllegalStateException if the argument CellRangeAddress intersects with // a merged region already in this sheet validateMergedRegions(region); } CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells(); CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); ctMergeCell.setRef(region.formatAsString()); return ctMergeCells.sizeOfMergeCellArray(); }
/** * Returns the list of merged regions. If you want multiple regions, this is * faster than calling {@link #getMergedRegion(int)} each time. * * @return the list of merged regions */ @Override public List<CellRangeAddress> getMergedRegions() { List<CellRangeAddress> addresses = new ArrayList<>(); CTMergeCells ctMergeCells = worksheet.getMergeCells(); if(ctMergeCells == null) { return addresses; } for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) { String ref = ctMergeCell.getRef(); addresses.add(CellRangeAddress.valueOf(ref)); } return addresses; }
/** * Returns the merged region at the specified index. If you want multiple * regions, it is faster to call {@link #getMergedRegions()} than to call * this each time. * * @return the merged region at the specified index */ @Override public CellRangeAddress getMergedRegion(int index) { CTMergeCells ctMergeCells = worksheet.getMergeCells(); if(ctMergeCells == null) { throw new IllegalStateException("This worksheet does not contain merged regions"); } CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index); String ref = ctMergeCell.getRef(); return CellRangeAddress.valueOf(ref); }