From 5319125c39204257b8f5ee22bc6a70ec94f107b3 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 16 Apr 2008 08:43:08 +0000 Subject: [PATCH] Rich text in HSSFTextbox must have at least one format run. Make sure it is so and apply th default fopnt if no formats were applied. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@648623 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/HSSFTextbox.java | 3 +++ .../poi/hssf/usermodel/TestHSSFTextbox.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java b/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java index 3a8fedd696..c9d4fbf111 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFTextbox.java @@ -81,6 +81,9 @@ public class HSSFTextbox */ public void setString( HSSFRichTextString string ) { + //if font is not set we must set the default one + if (string.numFormattingRuns() == 0) string.applyFont((short)0); + this.string = string; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFTextbox.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFTextbox.java index f7ce61faf2..f836d4284e 100755 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFTextbox.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFTextbox.java @@ -49,4 +49,23 @@ public final class TestHSSFTextbox extends TestCase{ assertEquals(HSSFTextbox.VERTICAL_ALIGNMENT_CENTER, textbox.getVerticalAlignment()); } + /** + * Excel requires at least one format run in HSSFTextbox. + * When inserting text make sure that if font is not set we must set the default one. + */ + public void testSetDeafultTextFormat() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); + + HSSFTextbox textbox1 = patriarch.createTextbox(new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)3,3)); + HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!"); + assertEquals(0, rt1.numFormattingRuns()); + textbox1.setString(rt1); + + HSSFRichTextString rt2 = textbox1.getString(); + assertEquals(1, rt2.numFormattingRuns()); + assertEquals(HSSFRichTextString.NO_FONT, rt2.getFontOfFormattingRun(0)); + } + } -- 2.39.5