aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/hssf
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2008-01-09 14:27:51 +0000
committerNick Burch <nick@apache.org>2008-01-09 14:27:51 +0000
commit9093d916584d0e29f582731182ab78cb17f82a82 (patch)
treee72abc68fe0e87452b13e5164951ff7f579de06b /src/java/org/apache/poi/hssf
parent6957c755c043f29c0f28860f3b791241d2ba1496 (diff)
downloadpoi-9093d916584d0e29f582731182ab78cb17f82a82.tar.gz
poi-9093d916584d0e29f582731182ab78cb17f82a82.zip
Fix bug #43008, by deprecating setCellNum() on HSSFCell, and adding moveCell() to HSSFRow, which correctly updates all the references
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610392 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/hssf')
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFCell.java40
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFRow.java46
2 files changed, 69 insertions, 17 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
index 9cc4550719..b3b4fc9297 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
@@ -24,17 +24,33 @@
*/
package org.apache.poi.hssf.usermodel;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+
import org.apache.poi.hssf.model.FormulaParser;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
-import org.apache.poi.hssf.record.*;
+import org.apache.poi.hssf.record.BlankRecord;
+import org.apache.poi.hssf.record.BoolErrRecord;
+import org.apache.poi.hssf.record.CellValueRecordInterface;
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
+import org.apache.poi.hssf.record.ExtendedFormatRecord;
+import org.apache.poi.hssf.record.FormulaRecord;
+import org.apache.poi.hssf.record.LabelSSTRecord;
+import org.apache.poi.hssf.record.NoteRecord;
+import org.apache.poi.hssf.record.NumberRecord;
+import org.apache.poi.hssf.record.ObjRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.SubRecord;
+import org.apache.poi.hssf.record.TextObjectRecord;
+import org.apache.poi.hssf.record.UnicodeString;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.Ptg;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.*;
-
/**
* High level representation of a cell in a row of a spreadsheet.
* Cells can be numeric, formula-based or string-based (text). The cell type
@@ -266,14 +282,24 @@ public class HSSFCell
}
/**
- * set the cell's number within the row (0 based)
+ * Set the cell's number within the row (0 based).
* @param num short the cell number
+ * @deprecated Doesn't update the row's idea of what cell this is, use {@link HSSFRow#moveCell(HSSFCell, short)} instead
*/
-
public void setCellNum(short num)
{
record.setColumn(num);
}
+
+ /**
+ * Updates the cell record's idea of what
+ * column it belongs in (0 based)
+ * @param num the new cell number
+ */
+ protected void updateCellNum(short num)
+ {
+ record.setColumn(num);
+ }
/**
* get the cell's number within the row
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
index 6cea11fa0a..ae5727bc68 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
@@ -22,15 +22,14 @@
*/
package org.apache.poi.hssf.usermodel;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.RowRecord;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
/**
* High level representation of a row of a spreadsheet.
*
@@ -157,11 +156,15 @@ public class HSSFRow
* remove the HSSFCell from this row.
* @param cell to remove
*/
- public void removeCell(HSSFCell cell)
- {
- CellValueRecordInterface cval = cell.getCellValueRecord();
-
- sheet.removeValueRecord(getRowNum(), cval);
+ public void removeCell(HSSFCell cell) {
+ removeCell(cell, true);
+ }
+ private void removeCell(HSSFCell cell, boolean alsoRemoveRecords) {
+ if(alsoRemoveRecords) {
+ CellValueRecordInterface cval = cell.getCellValueRecord();
+ sheet.removeValueRecord(getRowNum(), cval);
+ }
+
short column=cell.getCellNum();
if(cell!=null && column<cells.length)
{
@@ -233,11 +236,34 @@ public class HSSFRow
protected int getOutlineLevel() {
return row.getOutlineLevel();
}
+
+ /**
+ * Moves the supplied cell to a new column, which
+ * must not already have a cell there!
+ * @param cell The cell to move
+ * @param newColumn The new column number (0 based)
+ */
+ public void moveCell(HSSFCell cell, short newColumn) {
+ // Ensure the destination is free
+ if(cells.length > newColumn && cells[newColumn] != null) {
+ throw new IllegalArgumentException("Asked to move cell to column " + newColumn + " but there's already a cell there");
+ }
+
+ // Check it's one of ours
+ if(! cells[cell.getCellNum()].equals(cell)) {
+ throw new IllegalArgumentException("Asked to move a cell, but it didn't belong to our row");
+ }
+
+ // Move the cell to the new position
+ // (Don't remove the records though)
+ removeCell(cell, false);
+ cell.updateCellNum(newColumn);
+ addCell(cell);
+ }
/**
* used internally to add a cell.
*/
-
private void addCell(HSSFCell cell)
{
short column=cell.getCellNum();