From 852964d9d389eac035b2910694bd8202449e3779 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 8 Jan 2008 17:49:08 +0000 Subject: [PATCH] Support for unicode named named ranges (patch and test from bug #42033) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610082 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/record/NameRecord.java | 42 ++++++++++--------- .../poi/hssf/usermodel/TestNamedRange.java | 20 ++++++++- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index ff7a39729..59bd2eab7 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,6 +36,7 @@ + 42033 - Add support for named ranges with unicode names 34023 - When shifting rows, update formulas on that sheet to point to the new location of those rows Support getting all the cells referenced by an AreaReference, not just the corner ones 43510 - Add support for named ranges in formulas, including non-contiguous named ranges diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 575bb6ba3..584482100 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 42033 - Add support for named ranges with unicode names 34023 - When shifting rows, update formulas on that sheet to point to the new location of those rows Support getting all the cells referenced by an AreaReference, not just the corner ones 43510 - Add support for named ranges in formulas, including non-contiguous named ranges diff --git a/src/java/org/apache/poi/hssf/record/NameRecord.java b/src/java/org/apache/poi/hssf/record/NameRecord.java index 0d0d7f1cd..3b1c413d5 100644 --- a/src/java/org/apache/poi/hssf/record/NameRecord.java +++ b/src/java/org/apache/poi/hssf/record/NameRecord.java @@ -270,6 +270,9 @@ public class NameRecord extends Record { */ public void setNameText(String name){ field_12_name_text = name; + setCompressedUnicodeFlag( + StringUtil.hasMultibyte(name) ? (byte)1 : (byte)0 + ); } // public void setNameDefintion(String definition){ @@ -318,12 +321,24 @@ public class NameRecord extends Record { return field_2_keyboard_shortcut ; } - /** gets the name length + /** + * gets the name length, in characters * @return name length */ public byte getNameTextLength(){ return field_3_length_name_text; } + + /** + * gets the name length, in bytes + * @return raw name length + */ + public byte getRawNameTextLength(){ + if( (field_11_compressed_unicode_flag & 0x01) == 1 ) { + return (byte)(2 * field_3_length_name_text); + } + return field_3_length_name_text; + } /** get the definition length * @return definition length @@ -511,27 +526,16 @@ public class NameRecord extends Record { data[17 + offset] = getStatusBarLength(); data[18 + offset] = getCompressedUnicodeFlag(); - /* temp: gjs - if (isBuiltInName()) - { - LittleEndian.putShort( data, 2 + offset, (short) ( 16 + field_13_raw_name_definition.length ) ); - - data[19 + offset] = field_12_builtIn_name; - System.arraycopy( field_13_raw_name_definition, 0, data, 20 + offset, field_13_raw_name_definition.length ); - - return 20 + field_13_raw_name_definition.length; - } - else - { */ - int start_of_name_definition = 19 + field_3_length_name_text; if (this.isBuiltInName()) { //can send the builtin name directly in data [19 + offset] = this.getBuiltInName(); + } else if ((this.getCompressedUnicodeFlag() & 0x01) == 1) { + StringUtil.putUnicodeLE( getNameText(), data, 19 + offset ); + start_of_name_definition = 19 + (2 * field_3_length_name_text); } else { StringUtil.putCompressedUnicode( getNameText(), data, 19 + offset ); - } @@ -554,15 +558,15 @@ public class NameRecord extends Record { /* } */ } - /** gets the length of all texts + /** + * Gets the length of all texts, in bytes * @return total length */ public int getTextsLength(){ int result; - result = getNameTextLength() + getDescriptionTextLength() + - getHelpTopicLength() + getStatusBarLength(); - + result = getRawNameTextLength() + getDescriptionTextLength() + + getHelpTopicLength() + getStatusBarLength(); return result; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java b/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java index 8af9b2f3c..f804822d2 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java @@ -26,6 +26,8 @@ import org.apache.poi.hssf.util.CellReference; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.TempFile; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -316,7 +318,23 @@ public class TestNamedRange assertTrue("Name is "+nm2.getNameName(),"RangeTest2".equals(nm2.getNameName())); assertTrue("Reference is "+nm2.getReference(),(wb.getSheetName(1)+"!$A$1:$O$21").equals(nm2.getReference())); } - + + public void testUnicodeNamedRange() throws Exception { + HSSFWorkbook workBook = new HSSFWorkbook(); + HSSFSheet sheet = workBook.createSheet("Test"); + HSSFName name = workBook.createName(); + name.setNameName("\u03B1"); + name.setReference("Test!$D$3:$E$8"); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + workBook.write(out); + + HSSFWorkbook workBook2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray())); + HSSFName name2 = workBook2.getNameAt(0); + + assertEquals("\u03B1", name2.getNameName()); + assertEquals("Test!$D$3:$E$8", name2.getReference()); + } /** * Test to see if the print areas can be retrieved/created in memory