|
1 | | -/* |
2 | | - * Licensed to the Apache Software Foundation (ASF) under one |
3 | | - * or more contributor license agreements. See the NOTICE file |
4 | | - * distributed with this work for additional information |
5 | | - * regarding copyright ownership. The ASF licenses this file |
6 | | - * to you under the Apache License, Version 2.0 (the |
7 | | - * "License"); you may not use this file except in compliance |
8 | | - * with the License. You may obtain a copy of the License at |
9 | | - * |
10 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | - * |
12 | | - * Unless required by applicable law or agreed to in writing, |
13 | | - * software distributed under the License is distributed on an |
14 | | - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | - * KIND, either express or implied. See the License for the |
16 | | - * specific language governing permissions and limitations |
17 | | - * under the License. |
18 | | - */ |
19 | | - |
20 | | -package org.apache.fesod.sheet.analysis.v07.handlers; |
21 | | - |
22 | | -import org.apache.fesod.sheet.constant.ExcelXmlConstants; |
23 | | -import org.apache.fesod.sheet.context.xlsx.XlsxReadContext; |
24 | | -import org.apache.fesod.sheet.metadata.data.FormulaData; |
25 | | -import org.apache.fesod.sheet.read.metadata.holder.xlsx.XlsxReadSheetHolder; |
26 | | -import org.apache.fesod.sheet.util.StringUtils; |
27 | | -import org.apache.poi.ss.formula.FormulaParser; |
28 | | -import org.apache.poi.ss.formula.FormulaRenderer; |
29 | | -import org.apache.poi.ss.formula.FormulaType; |
30 | | -import org.apache.poi.ss.formula.SharedFormula; |
31 | | -import org.apache.poi.ss.formula.ptg.Ptg; |
32 | | -import org.apache.poi.ss.SpreadsheetVersion; |
33 | | -import org.xml.sax.Attributes; |
34 | | - |
35 | | -/** |
36 | | - * Cell Handler |
37 | | - * |
38 | | - */ |
39 | | -public class CellFormulaTagHandler extends AbstractXlsxTagHandler { |
40 | | - |
41 | | - @Override |
42 | | - public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
43 | | - XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
44 | | - xlsxReadSheetHolder.setTempFormula(new StringBuilder()); |
45 | | - |
46 | | - String formulaType = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_T); |
47 | | - String sharedIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_SI); |
48 | | - |
49 | | - xlsxReadSheetHolder.setTempFormulaType(formulaType); |
50 | | - xlsxReadSheetHolder.setTempFormulaSharedIndex(sharedIndex != null ? Integer.parseInt(sharedIndex) : null); |
51 | | - } |
52 | | - |
53 | | - @Override |
54 | | - public void endElement(XlsxReadContext xlsxReadContext, String name) { |
55 | | - XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
56 | | - String formulaText = xlsxReadSheetHolder.getTempFormula().toString(); |
57 | | - String formulaType = xlsxReadSheetHolder.getTempFormulaType(); |
58 | | - Integer sharedIndex = xlsxReadSheetHolder.getTempFormulaSharedIndex(); |
59 | | - |
60 | | - if (ExcelXmlConstants.ATTRIBUTE_SHARED.equals(formulaType) && sharedIndex != null) { |
61 | | - formulaText = handleSharedFormula(xlsxReadSheetHolder, formulaText, sharedIndex); |
62 | | - } |
63 | | - |
64 | | - FormulaData formulaData = new FormulaData(); |
65 | | - formulaData.setFormulaValue(formulaText); |
66 | | - xlsxReadSheetHolder.getTempCellData().setFormulaData(formulaData); |
67 | | - } |
68 | | - |
69 | | - @Override |
70 | | - public void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length) { |
71 | | - xlsxReadContext.xlsxReadSheetHolder().getTempFormula().append(ch, start, length); |
72 | | - } |
73 | | - |
74 | | - /** |
75 | | - * Handles shared formula when reading from Excel |
76 | | - */ |
77 | | - private String handleSharedFormula(XlsxReadSheetHolder xlsxReadSheetHolder, String formulaText, int sharedIndex) { |
78 | | - Integer currentRow = xlsxReadSheetHolder.getRowIndex(); |
79 | | - Integer currentCol = xlsxReadSheetHolder.getColumnIndex(); |
80 | | - |
81 | | - if (currentRow == null || currentCol == null) { |
82 | | - return formulaText; |
83 | | - } |
84 | | - |
85 | | - // If formula text exists then this is the master cell |
86 | | - if (!StringUtils.isEmpty(formulaText)) { |
87 | | - xlsxReadSheetHolder |
88 | | - .getSharedFormulaMap() |
89 | | - .put(sharedIndex, new XlsxReadSheetHolder.SharedFormulaInfo(formulaText, currentRow, currentCol)); |
90 | | - return formulaText; |
91 | | - } else { |
92 | | - // No formula text means this is a shared reference cell |
93 | | - XlsxReadSheetHolder.SharedFormulaInfo masterInfo = |
94 | | - xlsxReadSheetHolder.getSharedFormulaMap().get(sharedIndex); |
95 | | - |
96 | | - if (masterInfo == null) { |
97 | | - return ""; |
98 | | - } |
99 | | - |
100 | | - return convertSharedFormula(masterInfo, currentRow, currentCol); |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * Converts shared formula based on row and column offset |
106 | | - */ |
107 | | - private String convertSharedFormula( |
108 | | - XlsxReadSheetHolder.SharedFormulaInfo masterInfo, int currentRow, int currentCol) { |
109 | | - try { |
110 | | - // Parse the master formula text into tokens |
111 | | - Ptg[] masterPtgs = FormulaParser.parse(masterInfo.getFormulaText(), null, FormulaType.CELL, 0); |
112 | | - |
113 | | - // Calculate offset from the master cell position |
114 | | - int rowOffset = currentRow - masterInfo.getFirstRow(); |
115 | | - int colOffset = currentCol - masterInfo.getFirstCol(); |
116 | | - |
117 | | - // Use POI SharedFormula to convert with offset |
118 | | - SharedFormula sharedFormula = new SharedFormula(SpreadsheetVersion.EXCEL2007); |
119 | | - Ptg[] convertedPtgs = sharedFormula.convertSharedFormulas(masterPtgs, rowOffset, colOffset); |
120 | | - |
121 | | - // Convert tokens back to formula string |
122 | | - return FormulaRenderer.toFormulaString(null, convertedPtgs); |
123 | | - } catch (Exception e) { |
124 | | - // If conversion fails, return the master formula as-is |
125 | | - // This handles cases like volatile functions with no cell references |
126 | | - // where the formula should be identical across all cells anyway |
127 | | - return masterInfo.getFormulaText(); |
128 | | - } |
129 | | - } |
130 | | -} |
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.fesod.sheet.analysis.v07.handlers; |
| 21 | + |
| 22 | +import org.apache.fesod.sheet.constant.ExcelXmlConstants; |
| 23 | +import org.apache.fesod.sheet.context.xlsx.XlsxReadContext; |
| 24 | +import org.apache.fesod.sheet.metadata.data.FormulaData; |
| 25 | +import org.apache.fesod.sheet.read.metadata.holder.xlsx.XlsxReadSheetHolder; |
| 26 | +import org.apache.fesod.sheet.util.StringUtils; |
| 27 | +import org.apache.poi.ss.SpreadsheetVersion; |
| 28 | +import org.apache.poi.ss.formula.FormulaParser; |
| 29 | +import org.apache.poi.ss.formula.FormulaRenderer; |
| 30 | +import org.apache.poi.ss.formula.FormulaType; |
| 31 | +import org.apache.poi.ss.formula.SharedFormula; |
| 32 | +import org.apache.poi.ss.formula.ptg.Ptg; |
| 33 | +import org.xml.sax.Attributes; |
| 34 | + |
| 35 | +/** |
| 36 | + * Cell Handler |
| 37 | + * |
| 38 | + */ |
| 39 | +public class CellFormulaTagHandler extends AbstractXlsxTagHandler { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
| 43 | + XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
| 44 | + xlsxReadSheetHolder.setTempFormula(new StringBuilder()); |
| 45 | + |
| 46 | + String formulaType = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_T); |
| 47 | + String sharedIndex = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_SI); |
| 48 | + |
| 49 | + xlsxReadSheetHolder.setTempFormulaType(formulaType); |
| 50 | + xlsxReadSheetHolder.setTempFormulaSharedIndex(sharedIndex != null ? Integer.parseInt(sharedIndex) : null); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void endElement(XlsxReadContext xlsxReadContext, String name) { |
| 55 | + XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
| 56 | + String formulaText = xlsxReadSheetHolder.getTempFormula().toString(); |
| 57 | + String formulaType = xlsxReadSheetHolder.getTempFormulaType(); |
| 58 | + Integer sharedIndex = xlsxReadSheetHolder.getTempFormulaSharedIndex(); |
| 59 | + |
| 60 | + if (ExcelXmlConstants.ATTRIBUTE_SHARED.equals(formulaType) && sharedIndex != null) { |
| 61 | + formulaText = handleSharedFormula(xlsxReadSheetHolder, formulaText, sharedIndex); |
| 62 | + } |
| 63 | + |
| 64 | + FormulaData formulaData = new FormulaData(); |
| 65 | + formulaData.setFormulaValue(formulaText); |
| 66 | + xlsxReadSheetHolder.getTempCellData().setFormulaData(formulaData); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length) { |
| 71 | + xlsxReadContext.xlsxReadSheetHolder().getTempFormula().append(ch, start, length); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Handles shared formula when reading from Excel |
| 76 | + */ |
| 77 | + private String handleSharedFormula(XlsxReadSheetHolder xlsxReadSheetHolder, String formulaText, int sharedIndex) { |
| 78 | + Integer currentRow = xlsxReadSheetHolder.getRowIndex(); |
| 79 | + Integer currentCol = xlsxReadSheetHolder.getColumnIndex(); |
| 80 | + |
| 81 | + if (currentRow == null || currentCol == null) { |
| 82 | + return formulaText; |
| 83 | + } |
| 84 | + |
| 85 | + // If formula text exists then this is the master cell |
| 86 | + if (!StringUtils.isEmpty(formulaText)) { |
| 87 | + xlsxReadSheetHolder |
| 88 | + .getSharedFormulaMap() |
| 89 | + .put(sharedIndex, new XlsxReadSheetHolder.SharedFormulaInfo(formulaText, currentRow, currentCol)); |
| 90 | + return formulaText; |
| 91 | + } else { |
| 92 | + // No formula text means this is a shared reference cell |
| 93 | + XlsxReadSheetHolder.SharedFormulaInfo masterInfo = |
| 94 | + xlsxReadSheetHolder.getSharedFormulaMap().get(sharedIndex); |
| 95 | + |
| 96 | + if (masterInfo == null) { |
| 97 | + return ""; |
| 98 | + } |
| 99 | + |
| 100 | + return convertSharedFormula(masterInfo, currentRow, currentCol); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Converts shared formula based on row and column offset |
| 106 | + */ |
| 107 | + private String convertSharedFormula( |
| 108 | + XlsxReadSheetHolder.SharedFormulaInfo masterInfo, int currentRow, int currentCol) { |
| 109 | + try { |
| 110 | + // Parse the master formula text into tokens |
| 111 | + Ptg[] masterPtgs = FormulaParser.parse(masterInfo.getFormulaText(), null, FormulaType.CELL, 0); |
| 112 | + |
| 113 | + // Calculate offset from the master cell position |
| 114 | + int rowOffset = currentRow - masterInfo.getFirstRow(); |
| 115 | + int colOffset = currentCol - masterInfo.getFirstCol(); |
| 116 | + |
| 117 | + // Use POI SharedFormula to convert with offset |
| 118 | + SharedFormula sharedFormula = new SharedFormula(SpreadsheetVersion.EXCEL2007); |
| 119 | + Ptg[] convertedPtgs = sharedFormula.convertSharedFormulas(masterPtgs, rowOffset, colOffset); |
| 120 | + |
| 121 | + // Convert tokens back to formula string |
| 122 | + return FormulaRenderer.toFormulaString(null, convertedPtgs); |
| 123 | + } catch (Exception e) { |
| 124 | + // If conversion fails, return the master formula as-is |
| 125 | + // This handles cases like volatile functions with no cell references |
| 126 | + // where the formula should be identical across all cells anyway |
| 127 | + return masterInfo.getFormulaText(); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments