From 7832ad993c31e8455b9fbed741a95f72eb9b3b12 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 18 Oct 2014 19:58:05 +0000 Subject: [PATCH] SheetUtil helper for finding the main cell of a merged region, if in one git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1632833 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/util/SheetUtil.java | 37 +++++++++++ .../org/apache/poi/ss/util/TestSheetUtil.java | 65 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/testcases/org/apache/poi/ss/util/TestSheetUtil.java diff --git a/src/java/org/apache/poi/ss/util/SheetUtil.java b/src/java/org/apache/poi/ss/util/SheetUtil.java index 71c3c3cb40..6b55e223fd 100644 --- a/src/java/org/apache/poi/ss/util/SheetUtil.java +++ b/src/java/org/apache/poi/ss/util/SheetUtil.java @@ -293,4 +293,41 @@ public class SheetUtil { return false; } + /** + * Return the cell, taking account of merged regions. Allows you to find the + * cell who's contents are shown in a given position in the sheet. + * + *

If the cell at the given co-ordinates is a merged cell, this will + * return the primary (top-left) most cell of the merged region. + *

If the cell at the given co-ordinates is not in a merged region, + * then will return the cell itself. + *

If there is no cell defined at the given co-ordinates, will return + * null. + */ + public static Cell getCellWithMerges(Sheet sheet, int rowIx, int colIx) { + Row r = sheet.getRow(rowIx); + if (r != null) { + Cell c = r.getCell(colIx); + if (c != null) { + // Normal, non-merged cell + return c; + } + } + + for (int mr=0; mr