Line 0
Link Here
|
|
|
1 |
package org.apache.poi.xssf.usermodel.helpers; |
2 |
|
3 |
import org.apache.poi.ss.formula.FormulaParseException; |
4 |
import org.apache.poi.ss.formula.FormulaParser; |
5 |
import org.apache.poi.ss.formula.FormulaRenderer; |
6 |
import org.apache.poi.ss.formula.FormulaShifter; |
7 |
import org.apache.poi.ss.formula.FormulaType; |
8 |
import org.apache.poi.ss.formula.ptg.Ptg; |
9 |
import org.apache.poi.ss.usermodel.Cell; |
10 |
import org.apache.poi.ss.usermodel.Row; |
11 |
import org.apache.poi.ss.usermodel.Sheet; |
12 |
import org.apache.poi.ss.usermodel.Workbook; |
13 |
import org.apache.poi.util.Internal; |
14 |
import org.apache.poi.util.POILogFactory; |
15 |
import org.apache.poi.util.POILogger; |
16 |
import org.apache.poi.xssf.usermodel.XSSFCell; |
17 |
import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook; |
18 |
import org.apache.poi.xssf.usermodel.XSSFRow; |
19 |
import org.apache.poi.xssf.usermodel.XSSFSheet; |
20 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
21 |
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; |
22 |
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; |
23 |
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType; |
24 |
|
25 |
public class XSSFFormulaShiftingManager { |
26 |
|
27 |
private static final POILogger logger = POILogFactory.getLogger(XSSFRowShifter.class); |
28 |
|
29 |
protected final Sheet shiftingSheet; |
30 |
protected FormulaShifter shifter; |
31 |
|
32 |
public XSSFFormulaShiftingManager(Sheet shiftingSheet, FormulaShifter shifter){ |
33 |
this.shiftingSheet = shiftingSheet; |
34 |
this.shifter = shifter; |
35 |
} |
36 |
|
37 |
public void updateFormulas() { |
38 |
//update formulas on the parent sheet |
39 |
updateSheetFormulas(shiftingSheet); |
40 |
|
41 |
//update formulas on other sheets |
42 |
Workbook wb = shiftingSheet.getWorkbook(); |
43 |
for (Sheet sh : wb) { |
44 |
if (shiftingSheet == sh) continue; |
45 |
updateSheetFormulas(sh); |
46 |
} |
47 |
} |
48 |
|
49 |
private void updateSheetFormulas(Sheet sh) { |
50 |
for (Row r : sh) { |
51 |
XSSFRow row = (XSSFRow) r; |
52 |
updateRowFormulas(row); |
53 |
} |
54 |
} |
55 |
|
56 |
/** |
57 |
* Update the formulas in specified row using the formula shifting policy specified by shifter |
58 |
* |
59 |
* @param row the row to update the formulas on |
60 |
* @param shifter the formula shifting policy |
61 |
*/ |
62 |
@Internal |
63 |
public void updateRowFormulas(Row row) { |
64 |
for (Cell c : row) { |
65 |
updateCellFormula(row, (XSSFCell) c); |
66 |
} |
67 |
} |
68 |
|
69 |
public void updateCellFormula(Row row, XSSFCell cell){ |
70 |
CTCell ctCell = cell.getCTCell(); |
71 |
if (ctCell.isSetF()) { |
72 |
CTCellFormula f = ctCell.getF(); |
73 |
String formula = f.getStringValue(); |
74 |
if (formula.length() > 0) { |
75 |
String shiftedFormula = shiftFormula(row, formula); |
76 |
if (shiftedFormula != null) { |
77 |
f.setStringValue(shiftedFormula); |
78 |
if(f.getT() == STCellFormulaType.SHARED){ |
79 |
int si = (int)f.getSi(); |
80 |
XSSFSheet sheet = (XSSFSheet) row.getSheet(); |
81 |
CTCellFormula sf = sheet.getSharedFormula(si); |
82 |
sf.setStringValue(shiftedFormula); |
83 |
updateRefInCTCellFormula(row, sf); |
84 |
} |
85 |
} |
86 |
} |
87 |
//Range of cells which the formula applies to. |
88 |
updateRefInCTCellFormula(row, f); |
89 |
} |
90 |
} |
91 |
private void updateRefInCTCellFormula(Row row, CTCellFormula f) { |
92 |
if (f.isSetRef()) { //Range of cells which the formula applies to. |
93 |
String ref = f.getRef(); |
94 |
String shiftedRef = shiftFormula(row, ref); |
95 |
if (shiftedRef != null) f.setRef(shiftedRef); |
96 |
} |
97 |
} |
98 |
|
99 |
/** |
100 |
* Shift a formula using the supplied FormulaShifter |
101 |
* |
102 |
* @param row the row of the cell this formula belongs to. Used to get a reference to the parent workbook. |
103 |
* @param formula the formula to shift |
104 |
* @param shifter the FormulaShifter object that operates on the parsed formula tokens |
105 |
* @return the shifted formula if the formula was changed, |
106 |
* <code>null</code> if the formula wasn't modified |
107 |
*/ |
108 |
private String shiftFormula(Row row, String formula) { |
109 |
Sheet sheet = row.getSheet(); |
110 |
Workbook wb = sheet.getWorkbook(); |
111 |
int sheetIndex = wb.getSheetIndex(sheet); |
112 |
final int rowIndex = row.getRowNum(); |
113 |
XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb); |
114 |
|
115 |
try { |
116 |
Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex); |
117 |
String shiftedFmla = null; |
118 |
if (shifter.adjustFormula(ptgs, sheetIndex)) { |
119 |
shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); |
120 |
} |
121 |
return shiftedFmla; |
122 |
} catch (FormulaParseException fpe) { |
123 |
// Log, but don't change, rather than breaking |
124 |
logger.log(POILogger.WARN, "Error shifting formula on row ", row.getRowNum(), fpe); |
125 |
return formula; |
126 |
} |
127 |
} |
128 |
|
129 |
|
130 |
} |