Have XSSF rich test strings report if they have formatting applied or not, and fix them to only add the formatting child element when needed

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1693632 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-31 17:22:05 +00:00
parent ca35f594a7
commit 2ad973852c
4 changed files with 34 additions and 6 deletions

View File

@ -40,9 +40,6 @@ import org.apache.poi.ss.usermodel.FormulaEvaluator;
* For performance reasons, this class keeps a cache of all previously calculated intermediate
* cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
* calls to evaluate~ methods on this class.
*
* @author Amol S. Deshmukh < amolweb at ya hoo dot com >
* @author Josh Micich
*/
public class XSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluatorProvider {

View File

@ -203,8 +203,11 @@ public class XSSFRichTextString implements RichTextString {
CTRElt lt = st.addNewR();
lt.setT(text);
preserveSpaces(lt.xgetT());
if (font != null) {
CTRPrElt pr = lt.addNewRPr();
if(font != null) setRunAttributes(font.getCTFont(), pr);
setRunAttributes(font.getCTFont(), pr);
}
}
/**
@ -245,6 +248,22 @@ public class XSSFRichTextString implements RichTextString {
if(ctFont.sizeOfStrikeArray() > 0) pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal());
}
/**
* Does this string have any explicit formatting applied, or is
* it just text in the default style?
*/
public boolean hasFormatting() {
@SuppressWarnings("deprecation")
CTRElt[] rs = st.getRArray();
if (rs == null || rs.length == 0) {
return false;
}
for (CTRElt r : rs) {
if (r.isSetRPr()) return true;
}
return false;
}
/**
* Removes any formatting that may have been applied to the string.
*/

View File

@ -1629,7 +1629,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* </p>
* @return true if the date systems used in the workbook starts in 1904
*/
protected boolean isDate1904(){
@Internal
public boolean isDate1904(){
CTWorkbookPr workbookPr = workbook.getWorkbookPr();
return workbookPr != null && workbookPr.getDate1904();
}

View File

@ -41,16 +41,19 @@ public final class TestXSSFRichTextString extends TestCase {
public void testCreate() {
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
assertEquals("Apache POI", rt.getString());
assertEquals(false, rt.hasFormatting());
CTRst st = rt.getCTRst();
assertTrue(st.isSetT());
assertEquals("Apache POI", st.getT());
assertEquals(false, rt.hasFormatting());
rt.append(" is cool stuff");
assertEquals(2, st.sizeOfRArray());
assertFalse(st.isSetT());
assertEquals("Apache POI is cool stuff", rt.getString());
assertEquals(false, rt.hasFormatting());
}
public void testEmpty() {
@ -67,11 +70,13 @@ public final class TestXSSFRichTextString extends TestCase {
rt.append("89");
assertEquals("123456789", rt.getString());
assertEquals(false, rt.hasFormatting());
XSSFFont font1 = new XSSFFont();
font1.setBold(true);
rt.applyFont(2, 5, font1);
assertEquals(true, rt.hasFormatting());
assertEquals(4, rt.numFormattingRuns());
assertEquals(0, rt.getIndexOfFormattingRun(0));
@ -152,6 +157,7 @@ public final class TestXSSFRichTextString extends TestCase {
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
assertEquals("Apache POI", rt.getString());
assertEquals(false, rt.hasFormatting());
rt.clearFormatting();
@ -159,15 +165,20 @@ public final class TestXSSFRichTextString extends TestCase {
assertTrue(st.isSetT());
assertEquals("Apache POI", rt.getString());
assertEquals(0, rt.numFormattingRuns());
assertEquals(false, rt.hasFormatting());
XSSFFont font = new XSSFFont();
font.setBold(true);
rt.applyFont(7, 10, font);
assertEquals(2, rt.numFormattingRuns());
assertEquals(true, rt.hasFormatting());
rt.clearFormatting();
assertEquals("Apache POI", rt.getString());
assertEquals(0, rt.numFormattingRuns());
assertEquals(false, rt.hasFormatting());
}
public void testGetFonts() {