]> source.dussan.org Git - poi.git/commitdiff
Fix setting active cell in .xls by populating field_6_refs whenever row/column change...
authorDominik Stadler <centic@apache.org>
Wed, 25 Apr 2018 20:14:07 +0000 (20:14 +0000)
committerDominik Stadler <centic@apache.org>
Wed, 25 Apr 2018 20:14:07 +0000 (20:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1830115 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/SelectionRecord.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

index 12928b95f5a0720c9eaba32d5c37a64ff854853f..904fe041092511879f21a2543243f31d062b6b85 100644 (file)
@@ -79,6 +79,7 @@ public final class SelectionRecord extends StandardRecord {
      */
     public void setActiveCellRow(int row) {
         field_2_row_active_cell = row;
+        resetField6();
     }
 
     /**
@@ -87,6 +88,14 @@ public final class SelectionRecord extends StandardRecord {
      */
     public void setActiveCellCol(short col) {
         field_3_col_active_cell = col;
+        resetField6();
+    }
+
+    private void resetField6() {
+        // this is necessary in Excel to actually make Workbook.setActiveCell() take effect
+        field_6_refs = new CellRangeAddress8Bit[] {
+                new CellRangeAddress8Bit(field_2_row_active_cell, field_2_row_active_cell, field_3_col_active_cell, field_3_col_active_cell),
+        };
     }
 
     /**
@@ -130,17 +139,15 @@ public final class SelectionRecord extends StandardRecord {
 
     @Override
     public String toString() {
-        StringBuffer sb = new StringBuffer();
-
-        sb.append("[SELECTION]\n");
-        sb.append("    .pane            = ").append(HexDump.byteToHex(getPane())).append("\n");
-        sb.append("    .activecellrow   = ").append(HexDump.shortToHex(getActiveCellRow())).append("\n");
-        sb.append("    .activecellcol   = ").append(HexDump.shortToHex(getActiveCellCol())).append("\n");
-        sb.append("    .activecellref   = ").append(HexDump.shortToHex(getActiveCellRef())).append("\n");
-        sb.append("    .numrefs         = ").append(HexDump.shortToHex(field_6_refs.length)).append("\n");
-        sb.append("[/SELECTION]\n");
-        return sb.toString();
+        return "[SELECTION]\n" +
+                "    .pane            = " + HexDump.byteToHex(getPane()) + "\n" +
+                "    .activecellrow   = " + HexDump.shortToHex(getActiveCellRow()) + "\n" +
+                "    .activecellcol   = " + HexDump.shortToHex(getActiveCellCol()) + "\n" +
+                "    .activecellref   = " + HexDump.shortToHex(getActiveCellRef()) + "\n" +
+                "    .numrefs         = " + HexDump.shortToHex(field_6_refs.length) + "\n" +
+                "[/SELECTION]\n";
     }
+
     @Override
     protected int getDataSize() {
         return 9 // 1 byte + 4 shorts
index 06751cf2e50b4122f8d6071e62648412fcb0762e..ef853ccc2df5a131a998094a8a3f3ebc6f9f6baf 100644 (file)
@@ -50,6 +50,7 @@ import org.apache.poi.POIXMLDocumentPart.RelationPart;
 import org.apache.poi.POIXMLException;
 import org.apache.poi.POIXMLProperties;
 import org.apache.poi.common.usermodel.HyperlinkType;
+import org.apache.poi.hssf.HSSFITestDataProvider;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -64,6 +65,7 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
 import org.apache.poi.openxml4j.util.ZipSecureFile;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.ss.ITestDataProvider;
 import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
 import org.apache.poi.ss.formula.EvaluationConditionalFormatRule;
@@ -79,6 +81,7 @@ import org.apache.poi.ss.formula.functions.Function;
 import org.apache.poi.ss.formula.ptg.Ptg;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.AreaReference;
+import org.apache.poi.ss.util.CellAddress;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.ss.util.CellUtil;
@@ -3293,4 +3296,31 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
             sheet.autoSizeColumn(i);
         }
     }
+
+    @Test
+    public void test61905xlsx() throws IOException {
+        Workbook wb = new XSSFWorkbook();
+        checkActiveSheet(wb, XSSFITestDataProvider.instance);
+        wb.close();
+    }
+
+    @Test
+    public void test61905xls() throws IOException {
+        Workbook wb = new HSSFWorkbook();
+        checkActiveSheet(wb, HSSFITestDataProvider.instance);
+        wb.close();
+    }
+
+    private void checkActiveSheet(Workbook wb, ITestDataProvider instance) throws IOException {
+        Sheet sheet = wb.createSheet("new sheet");
+        sheet.setActiveCell(new CellAddress("E11"));
+        assertEquals("E11", sheet.getActiveCell().formatAsString());
+
+        Workbook wbBack = instance.writeOutAndReadBack(wb);
+        sheet = wbBack.getSheetAt(0);
+        assertEquals("E11", sheet.getActiveCell().formatAsString());
+        wbBack.close();
+
+        //wb.write(new FileOutputStream("c:/temp/61905." + instance.getStandardFileNameExtension()));
+    }
 }