Lines 1-13
Link Here
|
1 |
package org.apache.poi.xssf.usermodel.helpers; |
1 |
package org.apache.poi.xssf.usermodel.helpers; |
2 |
|
2 |
|
|
|
3 |
import java.util.Comparator; |
4 |
import java.util.Iterator; |
5 |
import java.util.Map; |
6 |
import java.util.SortedMap; |
7 |
import java.util.TreeMap; |
8 |
|
3 |
import org.apache.poi.ss.formula.FormulaShifter; |
9 |
import org.apache.poi.ss.formula.FormulaShifter; |
4 |
import org.apache.poi.ss.usermodel.CellType; |
10 |
import org.apache.poi.ss.usermodel.CellType; |
5 |
import org.apache.poi.ss.usermodel.Row; |
11 |
import org.apache.poi.ss.usermodel.Row; |
6 |
import org.apache.poi.ss.usermodel.Sheet; |
12 |
import org.apache.poi.ss.usermodel.Sheet; |
7 |
import org.apache.poi.ss.usermodel.helpers.ColumnShifter; |
13 |
import org.apache.poi.ss.usermodel.helpers.ColumnShifter; |
|
|
14 |
import org.apache.poi.ss.util.CellReference; |
8 |
import org.apache.poi.util.POILogFactory; |
15 |
import org.apache.poi.util.POILogFactory; |
9 |
import org.apache.poi.util.POILogger; |
16 |
import org.apache.poi.util.POILogger; |
|
|
17 |
import org.apache.poi.xssf.model.CommentsTable; |
10 |
import org.apache.poi.xssf.usermodel.XSSFCell; |
18 |
import org.apache.poi.xssf.usermodel.XSSFCell; |
|
|
19 |
import org.apache.poi.xssf.usermodel.XSSFComment; |
20 |
import org.apache.poi.xssf.usermodel.XSSFRow; |
21 |
import org.apache.poi.xssf.usermodel.XSSFVMLDrawing; |
22 |
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; |
23 |
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; |
11 |
|
24 |
|
12 |
public class XSSFColumnShifter extends ColumnShifter{ |
25 |
public class XSSFColumnShifter extends ColumnShifter{ |
13 |
|
26 |
|
Lines 31-36
public class XSSFColumnShifter extends ColumnShifter{
Link Here
|
31 |
shiftColumnsRight(); |
44 |
shiftColumnsRight(); |
32 |
//else if(shiftStep < 0) |
45 |
//else if(shiftStep < 0) |
33 |
//shiftColumnsLeft(); |
46 |
//shiftColumnsLeft(); |
|
|
47 |
formulaShiftingManager.updateFormulas(); |
34 |
} |
48 |
} |
35 |
/** |
49 |
/** |
36 |
* Inserts shiftStep empty columns at firstShiftColumnIndex-th position, and shifts rest columns to the right |
50 |
* Inserts shiftStep empty columns at firstShiftColumnIndex-th position, and shifts rest columns to the right |
Lines 45-53
public class XSSFColumnShifter extends ColumnShifter{
Link Here
|
45 |
XSSFCell oldCell = (XSSFCell)row.getCell(columnIndex); |
59 |
XSSFCell oldCell = (XSSFCell)row.getCell(columnIndex); |
46 |
if(oldCell == null) |
60 |
if(oldCell == null) |
47 |
continue; |
61 |
continue; |
48 |
if(oldCell.getCellTypeEnum() == CellType.FORMULA) // recalculate formula |
|
|
49 |
formulaShiftingManager.updateCellFormula(row, oldCell); |
50 |
|
51 |
if(columnIndex >= firstShiftColumnIndex){ // shift existing cell |
62 |
if(columnIndex >= firstShiftColumnIndex){ // shift existing cell |
52 |
org.apache.poi.ss.usermodel.Cell newCell = null; |
63 |
org.apache.poi.ss.usermodel.Cell newCell = null; |
53 |
newCell = row.createCell(columnIndex + shiftStep, oldCell.getCellTypeEnum()); |
64 |
newCell = row.createCell(columnIndex + shiftStep, oldCell.getCellTypeEnum()); |
Lines 59-72
public class XSSFColumnShifter extends ColumnShifter{
Link Here
|
59 |
} |
70 |
} |
60 |
} |
71 |
} |
61 |
} |
72 |
} |
62 |
formulaShiftingManager.updateFormulas(); |
|
|
63 |
} |
73 |
} |
64 |
/*private void shiftColumnsLeft(){ |
74 |
private void shiftColumnsLeft(){ |
65 |
for(int rowNo = 0; rowNo <= shiftingSheet.getLastRowNum(); rowNo++) |
75 |
for(int rowNo = 0; rowNo <= shiftingSheet.getLastRowNum(); rowNo++) |
66 |
{ |
76 |
{ |
67 |
Row row = shiftingSheet.getRow(rowNo); |
77 |
XSSFRow row = (XSSFRow)shiftingSheet.getRow(rowNo); |
68 |
for (int columnIndex = 0; columnIndex < row.getLastCellNum(); columnIndex++){ // process cells backwards, because of shifting |
78 |
for (int columnIndex = 0; columnIndex < row.getLastCellNum(); columnIndex++){ |
69 |
org.apache.poi.ss.usermodel.Cell oldCell = row.getCell(columnIndex); |
79 |
XSSFCell oldCell = (XSSFCell)row.getCell(columnIndex); |
70 |
if(columnIndex >= firstShiftColumnIndex + shiftStep && columnIndex < row.getLastCellNum() - shiftStep){ // shift existing cell |
80 |
if(columnIndex >= firstShiftColumnIndex + shiftStep && columnIndex < row.getLastCellNum() - shiftStep){ // shift existing cell |
71 |
org.apache.poi.ss.usermodel.Cell newCell = null; |
81 |
org.apache.poi.ss.usermodel.Cell newCell = null; |
72 |
newCell = row.getCell(columnIndex - shiftStep); |
82 |
newCell = row.getCell(columnIndex - shiftStep); |
Lines 74-81
public class XSSFColumnShifter extends ColumnShifter{
Link Here
|
74 |
if(newCell != null){ |
84 |
if(newCell != null){ |
75 |
oldCell.setCellType(newCell.getCellType()); |
85 |
oldCell.setCellType(newCell.getCellType()); |
76 |
cloneCellValue(newCell, oldCell); |
86 |
cloneCellValue(newCell, oldCell); |
77 |
if(oldCell.getCellTypeEnum() == CellType.FORMULA) // recalculate formula |
|
|
78 |
columnFormulaShifter.shiftFormula(oldCell); |
79 |
} |
87 |
} |
80 |
else { |
88 |
else { |
81 |
oldCell.setCellType(CellType.STRING); |
89 |
oldCell.setCellType(CellType.STRING); |
Lines 87-94
public class XSSFColumnShifter extends ColumnShifter{
Link Here
|
87 |
if(newCell != null){ |
95 |
if(newCell != null){ |
88 |
oldCell.setCellType(newCell.getCellType()); |
96 |
oldCell.setCellType(newCell.getCellType()); |
89 |
cloneCellValue(newCell, oldCell); |
97 |
cloneCellValue(newCell, oldCell); |
90 |
if(oldCell.getCellTypeEnum() == CellType.FORMULA) // recalculate formula |
|
|
91 |
columnFormulaShifter.shiftFormula(oldCell); |
92 |
} |
98 |
} |
93 |
else { |
99 |
else { |
94 |
oldCell.setCellType(CellType.STRING); |
100 |
oldCell.setCellType(CellType.STRING); |
Lines 98-105
public class XSSFColumnShifter extends ColumnShifter{
Link Here
|
98 |
} |
104 |
} |
99 |
} |
105 |
} |
100 |
} |
106 |
} |
101 |
columnFormulaShifter.processFormulasOnOtherSheets(); |
107 |
} |
102 |
}*/ |
|
|
103 |
|
108 |
|
104 |
public static void cloneCellValue(org.apache.poi.ss.usermodel.Cell oldCell, org.apache.poi.ss.usermodel.Cell newCell) { |
109 |
public static void cloneCellValue(org.apache.poi.ss.usermodel.Cell oldCell, org.apache.poi.ss.usermodel.Cell newCell) { |
105 |
switch (oldCell.getCellTypeEnum()) { |
110 |
switch (oldCell.getCellTypeEnum()) { |
Lines 123-126
public class XSSFColumnShifter extends ColumnShifter{
Link Here
|
123 |
} |
128 |
} |
124 |
} |
129 |
} |
125 |
|
130 |
|
|
|
131 |
private void processComments(CommentsTable sheetComments, XSSFVMLDrawing vml, int startRow, int endRow, final int n, boolean copyRowHeight){ |
132 |
SortedMap<XSSFComment, Integer> commentsToShift = new TreeMap<XSSFComment, Integer>(new Comparator<XSSFComment>() { |
133 |
@Override |
134 |
public int compare(XSSFComment o1, XSSFComment o2) { |
135 |
int column1Index = o1.getColumn(); |
136 |
int column2Index = o2.getColumn(); |
137 |
|
138 |
if(column1Index == column2Index) |
139 |
return o1.hashCode() - o2.hashCode(); |
140 |
|
141 |
if(n > 0) // when shifting right, sort higher column-values first |
142 |
return column1Index < column2Index ? 1 : -1; |
143 |
else // sort lower-column values first when shifting left |
144 |
return column1Index > column2Index ? 1 : -1; |
145 |
} |
146 |
}); |
147 |
|
148 |
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) { |
149 |
XSSFRow row = (XSSFRow)it.next(); |
150 |
int rownum = row.getRowNum(); |
151 |
|
152 |
if(sheetComments != null){ |
153 |
// calculate the new rownum |
154 |
int newrownum = shiftedRowNum(startRow, endRow, n, rownum); |
155 |
|
156 |
// is there a change necessary for the current row? |
157 |
if(newrownum != rownum) { |
158 |
CTCommentList lst = sheetComments.getCTComments().getCommentList(); |
159 |
for (CTComment comment : lst.getCommentArray()) { |
160 |
String oldRef = comment.getRef(); |
161 |
CellReference ref = new CellReference(oldRef); |
162 |
|
163 |
// is this comment part of the current row? |
164 |
if(ref.getRow() == rownum) { |
165 |
XSSFComment xssfComment = new XSSFComment(sheetComments, comment, |
166 |
vml == null ? null : vml.findCommentShape(rownum, ref.getCol())); |
167 |
|
168 |
// we should not perform the shifting right here as we would then find |
169 |
// already shifted comments and would shift them again... |
170 |
commentsToShift.put(xssfComment, newrownum); |
171 |
} |
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
if(rownum < startRow || rownum > endRow) { |
177 |
continue; |
178 |
} |
179 |
if (!copyRowHeight) { |
180 |
row.setHeight((short)-1); |
181 |
} |
182 |
row.shift(n); |
183 |
} |
184 |
|
185 |
// adjust all the affected comment-structures now |
186 |
// the Map is sorted and thus provides them in the order that we need here, |
187 |
// i.e. from down to up if shifting down, vice-versa otherwise |
188 |
for(Map.Entry<XSSFComment, Integer> entry : commentsToShift.entrySet()) { |
189 |
entry.getKey().setRow(entry.getValue()); |
190 |
} |
191 |
|
192 |
} |
193 |
|
126 |
} |
194 |
} |