diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index af3bbee4c..86f2696b3 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,8 @@ + 52576 - support changing external file references in HSSFWorkbook + 49896 - support external references in FormulaRenderer 52527 - avoid exception when matching shared formula records in HSSF 52568 - Added methods to set/get an XWPFRun's text color 52566 - Added methods to set/get vertical alignment and color in XWPFTableCell diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index 93d37acc4..c1ec938cf 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -2457,4 +2457,18 @@ public final class InternalWorkbook { } return record; } + + + /** + * Changes an external referenced file to another file. + * A formular in Excel which refers a cell in another file is saved in two parts: + * The referenced file is stored in an reference table. the row/cell information is saved separate. + * This method invokation will only change the reference in the lookup-table itself. + * @param oldUrl The old URL to search for and which is to be replaced + * @param newUrl The URL replacement + * @return true if the oldUrl was found and replaced with newUrl. Otherwise false + */ + public boolean changeExternalReference(String oldUrl, String newUrl) { + return linkTable.changeExternalReference(oldUrl, newUrl); + } } diff --git a/src/java/org/apache/poi/hssf/model/LinkTable.java b/src/java/org/apache/poi/hssf/model/LinkTable.java index 732ab1480..9236a625b 100644 --- a/src/java/org/apache/poi/hssf/model/LinkTable.java +++ b/src/java/org/apache/poi/hssf/model/LinkTable.java @@ -552,4 +552,27 @@ final class LinkTable { private int findRefIndexFromExtBookIndex(int extBookIndex) { return _externSheetRecord.findRefIndexFromExtBookIndex(extBookIndex); } + + /** + * Changes an external referenced file to another file. + * A formular in Excel which refers a cell in another file is saved in two parts: + * The referenced file is stored in an reference table. the row/cell information is saved separate. + * This method invokation will only change the reference in the lookup-table itself. + * @param oldUrl The old URL to search for and which is to be replaced + * @param newUrl The URL replacement + * @return true if the oldUrl was found and replaced with newUrl. Otherwise false + */ + public boolean changeExternalReference(String oldUrl, String newUrl) { + for(ExternalBookBlock ex : _externalBookBlocks) { + SupBookRecord externalRecord = ex.getExternalBookRecord(); + if (externalRecord.isExternalReferences() + && externalRecord.getURL().equals(oldUrl)) { + + externalRecord.setURL(newUrl); + return true; + } + } + return false; + } + } diff --git a/src/java/org/apache/poi/hssf/record/SupBookRecord.java b/src/java/org/apache/poi/hssf/record/SupBookRecord.java index b8be5deab..29fa4d91c 100644 --- a/src/java/org/apache/poi/hssf/record/SupBookRecord.java +++ b/src/java/org/apache/poi/hssf/record/SupBookRecord.java @@ -18,6 +18,8 @@ package org.apache.poi.hssf.record; import org.apache.poi.util.LittleEndianOutput; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.apache.poi.util.StringUtil; /** @@ -31,6 +33,8 @@ import org.apache.poi.util.StringUtil; */ public final class SupBookRecord extends StandardRecord { + private final static POILogger logger = POILogFactory.getLogger(SupBookRecord.class); + public final static short sid = 0x01AE; private static final short SMALL_RECORD_SIZE = 4; @@ -42,6 +46,15 @@ public final class SupBookRecord extends StandardRecord { private String[] field_3_sheet_names; private boolean _isAddInFunctions; + protected static final char CH_VOLUME = 1; + protected static final char CH_SAME_VOLUME = 2; + protected static final char CH_DOWN_DIR = 3; + protected static final char CH_UP_DIR = 4; + protected static final char CH_LONG_VOLUME = 5; + protected static final char CH_STARTUP_DIR = 6; + protected static final char CH_ALT_STARTUP_DIR = 7; + protected static final char CH_LIB_DIR = 8; + protected static final String PATH_SEPERATOR = System.getProperty("file.separator"); public static SupBookRecord createInternalReferences(short numberOfSheets) { return new SupBookRecord(false, numberOfSheets); @@ -192,21 +205,51 @@ public final class SupBookRecord extends StandardRecord { return encodedUrl; } private static String decodeFileName(String encodedUrl) { - return encodedUrl.substring(1); - // TODO the following special characters may appear in the rest of the string, and need to get interpreted - /* see "MICROSOFT OFFICE EXCEL 97-2007 BINARY FILE FORMAT SPECIFICATION" - chVolume 1 - chSameVolume 2 - chDownDir 3 - chUpDir 4 - chLongVolume 5 - chStartupDir 6 - chAltStartupDir 7 - chLibDir 8 - - */ + /* see "MICROSOFT OFFICE EXCEL 97-2007 BINARY FILE FORMAT SPECIFICATION" */ + StringBuilder sb = new StringBuilder(); + for(int i=1; i