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 515eb1010..89568f0b5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -346,7 +346,7 @@ public class XSSFRichTextString implements RichTextString { for(int i = 0; i < st.sizeOfRArray(); i++){ CTRElt r = st.getRArray(i); - if(i == index) { + if(i == index && r.getRPr() != null) { XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr())); fnt.setThemesTable(getThemesTable()); return fnt; 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 5a45219ac..6d053bea1 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java @@ -22,6 +22,10 @@ import java.util.TreeMap; import junit.framework.TestCase; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.XSSFTestDataSamples; +import org.junit.Test; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring; @@ -357,4 +361,57 @@ public final class TestXSSFRichTextString extends TestCase { assertEquals("New Line", t2.xmlText()); assertEquals("\n\n", t3.xmlText()); } + + + @Test + public void testBug56511() { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56511.xlsx"); + for (XSSFSheet sheet : wb) { + int lastRow = sheet.getLastRowNum(); + for (int rowIdx = sheet.getFirstRowNum(); rowIdx <= lastRow; rowIdx++) { + XSSFRow row = sheet.getRow(rowIdx); + if(row != null) { + int lastCell = row.getLastCellNum(); + + for (int cellIdx = row.getFirstCellNum(); cellIdx <= lastCell; cellIdx++) { + + XSSFCell cell = row.getCell(cellIdx); + if (cell != null) { + //System.out.println("row " + rowIdx + " column " + cellIdx + ": " + cell.getCellType() + ": " + cell.toString()); + + XSSFRichTextString richText = cell.getRichStringCellValue(); + int anzFormattingRuns = richText.numFormattingRuns(); + for (int run = 0; run < anzFormattingRuns; run++) { + /*XSSFFont font =*/ richText.getFontOfFormattingRun(run); + //System.out.println(" run " + run + // + " font " + (font == null ? "" : font.getFontName())); + } + } + } + } + } + } + } + + @Test + public void testBug56511_values() { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56511.xlsx"); + Sheet sheet = wb.getSheetAt(0); + 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(2).getRichStringCellValue(); + assertEquals(2, rt.numFormattingRuns()); + assertNotNull(rt.getFontOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + + rt = (XSSFRichTextString) row.getCell(3).getRichStringCellValue(); + assertEquals(3, rt.numFormattingRuns()); + assertNull(rt.getFontOfFormattingRun(0)); + assertNotNull(rt.getFontOfFormattingRun(1)); + assertNotNull(rt.getFontOfFormattingRun(2)); + } }