View | Details | Raw Unified | Return to bug 57893
Collapse All | Expand All

(-)a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (+11 lines)
Lines 868-873 public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { Link Here
868
    }
868
    }
869
869
870
    /**
870
    /**
871
     * @return the list of merged regions
872
     */
873
    public List<CellRangeAddress> getMergedRegions() {
874
        List<CellRangeAddress> addresses = new ArrayList<CellRangeAddress>();
875
        for (int i=0; i < _sheet.getNumMergedRegions(); i++) {
876
            addresses.add(_sheet.getMergedRegionAt(i));
877
        }
878
        return addresses;
879
    }
880
881
    /**
871
     * @return an iterator of the PHYSICAL rows.  Meaning the 3rd element may not
882
     * @return an iterator of the PHYSICAL rows.  Meaning the 3rd element may not
872
     *         be the third row if say for instance the second row is undefined.
883
     *         be the third row if say for instance the second row is undefined.
873
     *         Call getRowNum() on each row if you care which one it is.
884
     *         Call getRowNum() on each row if you care which one it is.
(-)a/src/java/org/apache/poi/ss/usermodel/Sheet.java (+7 lines)
Lines 321-326 public interface Sheet extends Iterable<Row> { Link Here
321
    public CellRangeAddress getMergedRegion(int index);
321
    public CellRangeAddress getMergedRegion(int index);
322
322
323
    /**
323
    /**
324
     * Returns the list of merged regions.
325
     *
326
     * @return the list of merged regions
327
     */
328
    public List<CellRangeAddress> getMergedRegions();
329
330
    /**
324
     *  Returns an iterator of the physical rows
331
     *  Returns an iterator of the physical rows
325
     *
332
     *
326
     * @return an iterator of the PHYSICAL rows.  Meaning the 3rd element may not
333
     * @return an iterator of the PHYSICAL rows.  Meaning the 3rd element may not
(-)a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (-1 / +14 lines)
Lines 431-437 public class SXSSFSheet implements Sheet, Cloneable Link Here
431
    }
431
    }
432
432
433
    /**
433
    /**
434
     * Returns the merged region at the specified index
434
     * Returns the merged region at the specified index. If you want multiple
435
     * regions, it is faster to call {@link #getMergedRegions()} than to call
436
     * this each time.
435
     *
437
     *
436
     * @return the merged region at the specified index
438
     * @return the merged region at the specified index
437
     */
439
     */
Lines 441-446 public class SXSSFSheet implements Sheet, Cloneable Link Here
441
    }
443
    }
442
444
443
    /**
445
    /**
446
     * Returns the list of merged regions. If you want multiple regions, this is
447
     * faster than calling {@link #getMergedRegion(int)} each time.
448
     *
449
     * @return the list of merged regions
450
     */
451
    @Override
452
    public List<CellRangeAddress> getMergedRegions() {
453
        return _sh.getMergedRegions();
454
    }
455
456
    /**
444
     *  Returns an iterator of the physical rows
457
     *  Returns an iterator of the physical rows
445
     *
458
     *
446
     * @return an iterator of the PHYSICAL rows.  Meaning the 3rd element may not
459
     * @return an iterator of the PHYSICAL rows.  Meaning the 3rd element may not
(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (+25 lines)
Lines 1073-1078 public class XSSFSheet extends POIXMLDocumentPart implements Sheet { Link Here
1073
    }
1073
    }
1074
1074
1075
    /**
1075
    /**
1076
     * Returns the merged region at the specified index. If you want multiple
1077
     * regions, it is faster to call {@link #getMergedRegions()} than to call
1078
     * this each time.
1079
     *
1076
     * @return the merged region at the specified index
1080
     * @return the merged region at the specified index
1077
     * @throws IllegalStateException if this worksheet does not contain merged regions
1081
     * @throws IllegalStateException if this worksheet does not contain merged regions
1078
     */
1082
     */
Lines 1087-1092 public class XSSFSheet extends POIXMLDocumentPart implements Sheet { Link Here
1087
    }
1091
    }
1088
1092
1089
    /**
1093
    /**
1094
     * Returns the list of merged regions. If you want multiple regions, this is
1095
     * faster than calling {@link #getMergedRegion(int)} each time.
1096
     *
1097
     * @return the list of merged regions
1098
     * @throws IllegalStateException if this worksheet does not contain merged regions
1099
     */
1100
    @SuppressWarnings("deprecation")
1101
    @Override
1102
    public List<CellRangeAddress> getMergedRegions() {
1103
        CTMergeCells ctMergeCells = worksheet.getMergeCells();
1104
        if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions");
1105
1106
        List<CellRangeAddress> addresses = new ArrayList<CellRangeAddress>();
1107
        for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) {
1108
            String ref = ctMergeCell.getRef();
1109
            addresses.add(CellRangeAddress.valueOf(ref));
1110
        }
1111
        return addresses;
1112
    }
1113
1114
    /**
1090
     * Returns the number of merged regions defined in this worksheet
1115
     * Returns the number of merged regions defined in this worksheet
1091
     *
1116
     *
1092
     * @return number of merged regions in this worksheet
1117
     * @return number of merged regions in this worksheet
(-)a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetMergeRegions.java (+48 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
package org.apache.poi.xssf.usermodel;
18
19
import static org.junit.Assert.*;
20
21
import java.io.IOException;
22
import java.util.List;
23
24
import org.apache.poi.ss.util.CellRangeAddress;
25
import org.apache.poi.xssf.XSSFTestDataSamples;
26
import org.junit.Test;
27
28
public class TestXSSFSheetMergeRegions {
29
    @Test
30
    public void testMergeRegionsSpeed() throws IOException {
31
        final XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57893-many-merges.xlsx");
32
        try {
33
            final XSSFSheet sheet = wb.getSheetAt(0);
34
            final long start = System.currentTimeMillis();
35
            final List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
36
            assertEquals(50000, mergedRegions.size());
37
            for (CellRangeAddress cellRangeAddress : mergedRegions) {
38
                assertEquals(cellRangeAddress.getFirstRow(), cellRangeAddress.getLastRow());
39
                assertEquals(2, cellRangeAddress.getNumberOfCells());
40
            }
41
            long millis = System.currentTimeMillis() - start;
42
            // This time is typically ~800ms, versus ~7800ms to iterate getMergedRegion(int).
43
            assertTrue("Should have taken <2000 ms to iterate 50k merged regions but took " + millis, millis < 2000);
44
        } finally {
45
            wb.close();
46
        }
47
    }
48
}

Return to bug 57893