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:
parent
ca35f594a7
commit
2ad973852c
@ -40,9 +40,6 @@ import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
|||||||
* For performance reasons, this class keeps a cache of all previously calculated intermediate
|
* 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
|
* cell values. Be sure to call {@link #clearAllCachedResultValues()} if any workbook cells are changed between
|
||||||
* calls to evaluate~ methods on this class.
|
* 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 {
|
public class XSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluatorProvider {
|
||||||
|
|
||||||
|
@ -203,8 +203,11 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
CTRElt lt = st.addNewR();
|
CTRElt lt = st.addNewR();
|
||||||
lt.setT(text);
|
lt.setT(text);
|
||||||
preserveSpaces(lt.xgetT());
|
preserveSpaces(lt.xgetT());
|
||||||
CTRPrElt pr = lt.addNewRPr();
|
|
||||||
if(font != null) setRunAttributes(font.getCTFont(), pr);
|
if (font != null) {
|
||||||
|
CTRPrElt pr = lt.addNewRPr();
|
||||||
|
setRunAttributes(font.getCTFont(), pr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -244,6 +247,22 @@ public class XSSFRichTextString implements RichTextString {
|
|||||||
if(ctFont.sizeOfShadowArray() > 0) pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal());
|
if(ctFont.sizeOfShadowArray() > 0) pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal());
|
||||||
if(ctFont.sizeOfStrikeArray() > 0) pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal());
|
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.
|
* Removes any formatting that may have been applied to the string.
|
||||||
|
@ -1629,7 +1629,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
|
|||||||
* </p>
|
* </p>
|
||||||
* @return true if the date systems used in the workbook starts in 1904
|
* @return true if the date systems used in the workbook starts in 1904
|
||||||
*/
|
*/
|
||||||
protected boolean isDate1904(){
|
@Internal
|
||||||
|
public boolean isDate1904(){
|
||||||
CTWorkbookPr workbookPr = workbook.getWorkbookPr();
|
CTWorkbookPr workbookPr = workbook.getWorkbookPr();
|
||||||
return workbookPr != null && workbookPr.getDate1904();
|
return workbookPr != null && workbookPr.getDate1904();
|
||||||
}
|
}
|
||||||
|
@ -41,16 +41,19 @@ public final class TestXSSFRichTextString extends TestCase {
|
|||||||
public void testCreate() {
|
public void testCreate() {
|
||||||
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
|
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
|
||||||
assertEquals("Apache POI", rt.getString());
|
assertEquals("Apache POI", rt.getString());
|
||||||
|
assertEquals(false, rt.hasFormatting());
|
||||||
|
|
||||||
CTRst st = rt.getCTRst();
|
CTRst st = rt.getCTRst();
|
||||||
assertTrue(st.isSetT());
|
assertTrue(st.isSetT());
|
||||||
assertEquals("Apache POI", st.getT());
|
assertEquals("Apache POI", st.getT());
|
||||||
|
assertEquals(false, rt.hasFormatting());
|
||||||
|
|
||||||
rt.append(" is cool stuff");
|
rt.append(" is cool stuff");
|
||||||
assertEquals(2, st.sizeOfRArray());
|
assertEquals(2, st.sizeOfRArray());
|
||||||
assertFalse(st.isSetT());
|
assertFalse(st.isSetT());
|
||||||
|
|
||||||
assertEquals("Apache POI is cool stuff", rt.getString());
|
assertEquals("Apache POI is cool stuff", rt.getString());
|
||||||
|
assertEquals(false, rt.hasFormatting());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEmpty() {
|
public void testEmpty() {
|
||||||
@ -67,11 +70,13 @@ public final class TestXSSFRichTextString extends TestCase {
|
|||||||
rt.append("89");
|
rt.append("89");
|
||||||
|
|
||||||
assertEquals("123456789", rt.getString());
|
assertEquals("123456789", rt.getString());
|
||||||
|
assertEquals(false, rt.hasFormatting());
|
||||||
|
|
||||||
XSSFFont font1 = new XSSFFont();
|
XSSFFont font1 = new XSSFFont();
|
||||||
font1.setBold(true);
|
font1.setBold(true);
|
||||||
|
|
||||||
rt.applyFont(2, 5, font1);
|
rt.applyFont(2, 5, font1);
|
||||||
|
assertEquals(true, rt.hasFormatting());
|
||||||
|
|
||||||
assertEquals(4, rt.numFormattingRuns());
|
assertEquals(4, rt.numFormattingRuns());
|
||||||
assertEquals(0, rt.getIndexOfFormattingRun(0));
|
assertEquals(0, rt.getIndexOfFormattingRun(0));
|
||||||
@ -152,6 +157,7 @@ public final class TestXSSFRichTextString extends TestCase {
|
|||||||
|
|
||||||
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
|
XSSFRichTextString rt = new XSSFRichTextString("Apache POI");
|
||||||
assertEquals("Apache POI", rt.getString());
|
assertEquals("Apache POI", rt.getString());
|
||||||
|
assertEquals(false, rt.hasFormatting());
|
||||||
|
|
||||||
rt.clearFormatting();
|
rt.clearFormatting();
|
||||||
|
|
||||||
@ -159,15 +165,20 @@ public final class TestXSSFRichTextString extends TestCase {
|
|||||||
assertTrue(st.isSetT());
|
assertTrue(st.isSetT());
|
||||||
assertEquals("Apache POI", rt.getString());
|
assertEquals("Apache POI", rt.getString());
|
||||||
assertEquals(0, rt.numFormattingRuns());
|
assertEquals(0, rt.numFormattingRuns());
|
||||||
|
assertEquals(false, rt.hasFormatting());
|
||||||
|
|
||||||
XSSFFont font = new XSSFFont();
|
XSSFFont font = new XSSFFont();
|
||||||
font.setBold(true);
|
font.setBold(true);
|
||||||
|
|
||||||
rt.applyFont(7, 10, font);
|
rt.applyFont(7, 10, font);
|
||||||
assertEquals(2, rt.numFormattingRuns());
|
assertEquals(2, rt.numFormattingRuns());
|
||||||
|
assertEquals(true, rt.hasFormatting());
|
||||||
|
|
||||||
rt.clearFormatting();
|
rt.clearFormatting();
|
||||||
|
|
||||||
assertEquals("Apache POI", rt.getString());
|
assertEquals("Apache POI", rt.getString());
|
||||||
assertEquals(0, rt.numFormattingRuns());
|
assertEquals(0, rt.numFormattingRuns());
|
||||||
|
assertEquals(false, rt.hasFormatting());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetFonts() {
|
public void testGetFonts() {
|
||||||
|
Loading…
Reference in New Issue
Block a user