* @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();
}
/**
* @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;
+ if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) {
+ 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;
- }
+ CTRElt r = st.getRArray(index);
+ if(r.getRPr() != null) {
+ XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr()));
+ fnt.setThemesTable(getThemesTable());
+ return fnt;
}
+
return null;
}
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));
}
}