diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 89568f0b5..c58795075 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -279,13 +279,12 @@ public class XSSFRichTextString implements RichTextString { * @return the number of characters this format run covers */ public int getLengthOfFormattingRun(int index) { - if(st.sizeOfRArray() == 0) return length(); - - for(int i = 0; i < st.sizeOfRArray(); i++){ - CTRElt r = st.getRArray(i); - if(i == index) return r.getT().length(); + if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) { + return -1; } - return -1; + + CTRElt r = st.getRArray(index); + return r.getT().length(); } /** @@ -342,16 +341,17 @@ public class XSSFRichTextString implements RichTextString { * @return A copy of the font used or null if no formatting is applied to the specified text run. */ public XSSFFont getFontOfFormattingRun(int index) { - if(st.sizeOfRArray() == 0) return null; - - for(int i = 0; i < st.sizeOfRArray(); i++){ - CTRElt r = st.getRArray(i); - if(i == index && r.getRPr() != null) { - XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr())); - fnt.setThemesTable(getThemesTable()); - return fnt; - } + if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) { + return null; } + + CTRElt r = st.getRArray(index); + if(r.getRPr() != null) { + XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr())); + fnt.setThemesTable(getThemesTable()); + return fnt; + } + return null; } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java index 6d053bea1..3daa570b9 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java @@ -400,18 +400,35 @@ public final class TestXSSFRichTextString extends TestCase { Row row = sheet.getRow(0); // verify the values to ensure future changes keep the returned information equal - assertEquals(0, row.getCell(0).getRichStringCellValue().numFormattingRuns()); - assertEquals(0, row.getCell(1).getRichStringCellValue().numFormattingRuns()); + XSSFRichTextString rt = (XSSFRichTextString) row.getCell(0).getRichStringCellValue(); + assertEquals(0, rt.numFormattingRuns()); + assertNull(rt.getFontOfFormattingRun(0)); + assertEquals(-1, rt.getLengthOfFormattingRun(0)); - XSSFRichTextString rt = (XSSFRichTextString) row.getCell(2).getRichStringCellValue(); + rt = (XSSFRichTextString) row.getCell(1).getRichStringCellValue(); + assertEquals(0, row.getCell(1).getRichStringCellValue().numFormattingRuns()); + assertNull(rt.getFontOfFormattingRun(1)); + assertEquals(-1, rt.getLengthOfFormattingRun(1)); + + rt = (XSSFRichTextString) row.getCell(2).getRichStringCellValue(); assertEquals(2, rt.numFormattingRuns()); assertNotNull(rt.getFontOfFormattingRun(0)); + assertEquals(4, rt.getLengthOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + assertEquals(9, rt.getLengthOfFormattingRun(1)); + + assertNull(rt.getFontOfFormattingRun(2)); rt = (XSSFRichTextString) row.getCell(3).getRichStringCellValue(); assertEquals(3, rt.numFormattingRuns()); assertNull(rt.getFontOfFormattingRun(0)); + assertEquals(1, rt.getLengthOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + assertEquals(3, rt.getLengthOfFormattingRun(1)); + assertNotNull(rt.getFontOfFormattingRun(2)); + assertEquals(9, rt.getLengthOfFormattingRun(2)); } }