-
Notifications
You must be signed in to change notification settings - Fork 445
feat: Add support for reading shared formulas in XLSX files #693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
41eb660
421b0c1
6c87bba
c87c7b8
aaa5f31
333b212
369cb70
24049a8
5f0b552
3a240dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,9 @@ | |
| package org.apache.fesod.sheet.read.metadata.holder.xlsx; | ||
|
|
||
| import java.util.Deque; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedList; | ||
| import java.util.Map; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
@@ -54,6 +56,18 @@ public class XlsxReadSheetHolder extends ReadSheetHolder { | |
| * Formula for current label. | ||
| */ | ||
| private StringBuilder tempFormula; | ||
| /** | ||
| * Formula type for current label. | ||
| */ | ||
| private String tempFormulaType; | ||
| /** | ||
| * Formula shared index for current label. | ||
| */ | ||
| private Integer tempFormulaSharedIndex; | ||
| /** | ||
| * Map to store master shared formulas by their shared index. | ||
| */ | ||
| private Map<Integer, SharedFormulaInfo> sharedFormulaMap; | ||
|
||
| /** | ||
| * excel Relationship | ||
| */ | ||
|
|
@@ -62,8 +76,35 @@ public class XlsxReadSheetHolder extends ReadSheetHolder { | |
| public XlsxReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { | ||
| super(readSheet, readWorkbookHolder); | ||
| this.tagDeque = new LinkedList<String>(); | ||
| this.sharedFormulaMap = new HashMap<>(); | ||
| packageRelationshipCollection = ((XlsxReadWorkbookHolder) readWorkbookHolder) | ||
| .getPackageRelationshipCollectionMap() | ||
| .get(readSheet.getSheetNo()); | ||
| } | ||
|
|
||
| /** | ||
| * Information about a shared formula master cell. | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| public static class SharedFormulaInfo { | ||
| /** | ||
| * The master formula text. | ||
| */ | ||
| private String formulaText; | ||
| /** | ||
| * Row index of the master formula cell. | ||
| */ | ||
| private int firstRow; | ||
| /** | ||
| * Column index of the master formula cell. | ||
| */ | ||
| private int firstCol; | ||
|
|
||
| public SharedFormulaInfo(String formulaText, int firstRow, int firstCol) { | ||
| this.formulaText = formulaText; | ||
| this.firstRow = firstRow; | ||
| this.firstCol = firstCol; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fesod.sheet.formula; | ||
|
|
||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.apache.fesod.sheet.annotation.ExcelProperty; | ||
| import org.apache.fesod.sheet.metadata.data.ReadCellData; | ||
|
|
||
| /** | ||
| * Test data for shared formula reading | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| @EqualsAndHashCode | ||
| public class FormulaData { | ||
| @ExcelProperty("date") | ||
| private String date; | ||
|
|
||
| @ExcelProperty("value1") | ||
| private Integer value1; | ||
|
|
||
| @ExcelProperty("value2") | ||
| private Integer value2; | ||
|
|
||
| @ExcelProperty("volatileFormula") | ||
| private ReadCellData<?> volatileFormula; | ||
|
|
||
| @ExcelProperty("relativeFormula") | ||
| private ReadCellData<?> relativeFormula; | ||
|
|
||
| @ExcelProperty("columnAbsoluteFormula") | ||
| private ReadCellData<?> columnAbsoluteFormula; | ||
|
|
||
| @ExcelProperty("mixedAbsoluteFormula") | ||
| private ReadCellData<?> mixedAbsoluteFormula; | ||
|
|
||
| @ExcelProperty("additionFormula") | ||
| private ReadCellData<?> additionFormula; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fesod.sheet.formula; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.fesod.sheet.context.AnalysisContext; | ||
| import org.apache.fesod.sheet.event.AnalysisEventListener; | ||
| import org.junit.jupiter.api.Assertions; | ||
|
|
||
| /** | ||
| * Listener for formula data testing | ||
| */ | ||
| @Slf4j | ||
| public class FormulaDataListener extends AnalysisEventListener<FormulaData> { | ||
|
|
||
| @Override | ||
| public void invoke(FormulaData data, AnalysisContext context) { | ||
| // Verify that shared formulas are read correctly | ||
| if (data.getVolatileFormula() != null && data.getVolatileFormula().getFormulaData() != null) { | ||
| String formula = data.getVolatileFormula().getFormulaData().getFormulaValue(); | ||
| Assertions.assertNotNull(formula); | ||
| log.info("volatileFormula: {}", formula); | ||
| } | ||
|
|
||
| if (data.getRelativeFormula() != null && data.getRelativeFormula().getFormulaData() != null) { | ||
| String formula = data.getRelativeFormula().getFormulaData().getFormulaValue(); | ||
| Assertions.assertNotNull(formula); | ||
| log.info("relativeFormula: {}", formula); | ||
| } | ||
|
|
||
| if (data.getColumnAbsoluteFormula() != null | ||
| && data.getColumnAbsoluteFormula().getFormulaData() != null) { | ||
| String formula = data.getColumnAbsoluteFormula().getFormulaData().getFormulaValue(); | ||
| Assertions.assertNotNull(formula); | ||
| log.info("columnAbsoluteFormula: {}", formula); | ||
| } | ||
|
|
||
| if (data.getMixedAbsoluteFormula() != null | ||
| && data.getMixedAbsoluteFormula().getFormulaData() != null) { | ||
| String formula = data.getMixedAbsoluteFormula().getFormulaData().getFormulaValue(); | ||
| Assertions.assertNotNull(formula); | ||
| log.info("mixedAbsoluteFormula: {}", formula); | ||
| } | ||
|
|
||
| if (data.getAdditionFormula() != null && data.getAdditionFormula().getFormulaData() != null) { | ||
| String formula = data.getAdditionFormula().getFormulaData().getFormulaValue(); | ||
| Assertions.assertNotNull(formula); | ||
| log.info("additionFormula: {}", formula); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void doAfterAllAnalysed(AnalysisContext context) { | ||
| log.info("All formula data analysed"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.fesod.sheet.formula; | ||
|
|
||
| import java.io.File; | ||
| import org.apache.fesod.sheet.FesodSheet; | ||
| import org.apache.fesod.sheet.util.TestFileUtil; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Test for shared formula reading | ||
| */ | ||
| public class FormulaDataTest { | ||
|
|
||
| private static File file07; | ||
|
|
||
| @BeforeAll | ||
| public static void init() { | ||
| file07 = TestFileUtil.readFile("formula" + File.separator + "shared_formula.xlsx"); | ||
| } | ||
|
|
||
| @Test | ||
| public void t01ReadSharedFormula07() throws Exception { | ||
| FesodSheet.read(file07, FormulaData.class, new FormulaDataListener()) | ||
| .sheet() | ||
| .doRead(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding error handling for
Integer.parseInt(sharedIndex). If the shared index attribute contains an invalid integer value, this will throw aNumberFormatExceptionthat will propagate up the call stack. You should either wrap this in a try-catch block or validate the value before parsing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! I checked the codebase and found that
Integer.parseIntis used without try-catch in similar XML parsing contexts such as CellTagHandler.java:66. Meanwhile, according to the Office Open XML standard, thesiattribute is defined as:The spec guarantees
siis always an unsigned integer in valid Excel files. If it contains an invalid value, the Excel file is malformed and theNumberFormatExceptionshould propagate to fail fast rather than silently swallowing the error.