|
13 | 13 | import org.apache.poi.ss.SpreadsheetVersion; |
14 | 14 | import org.apache.poi.ss.formula.FormulaParser; |
15 | 15 | import org.apache.poi.ss.formula.FormulaRenderer; |
16 | | -import org.apache.poi.ss.formula.FormulaShifter; |
17 | 16 | import org.apache.poi.ss.formula.FormulaType; |
18 | 17 | import org.apache.poi.ss.formula.ptg.Ptg; |
19 | 18 | import org.apache.poi.ss.usermodel.*; |
@@ -356,25 +355,27 @@ && isSpreadsheetTag(event.asEndElement().getName())) { |
356 | 355 | if (sf == null) { |
357 | 356 | LOG.warn("No SharedFormula found for si={}", formulaSI); |
358 | 357 | } else { |
359 | | - CurrentRowEvaluationWorkbook evaluationWorkbook = |
| 358 | + final CurrentRowEvaluationWorkbook evaluationWorkbook = |
360 | 359 | new CurrentRowEvaluationWorkbook(wb, currentRow); |
361 | 360 | int sheetIndex = wb.getSheetIndex(sheet); |
362 | 361 | if (sheetIndex < 0) { |
363 | 362 | LOG.warn("Failed to find correct sheet index; defaulting to zero"); |
364 | 363 | sheetIndex = 0; |
365 | 364 | } |
366 | 365 | try { |
367 | | - Ptg[] ptgs = FormulaParser.parse(sf.getFormula(), evaluationWorkbook, FormulaType.CELL, sheetIndex, currentRow.getRowNum()); |
| 366 | + Ptg[] ptgs = FormulaParser.parse( |
| 367 | + sf.getFormula(), evaluationWorkbook, FormulaType.CELL, sheetIndex, currentRow.getRowNum()); |
368 | 368 | final int rowsToMove = currentRowNum - sf.getCellAddress().getRow(); |
369 | | - FormulaShifter formulaShifter = FormulaShifter.createForRowShift(sheetIndex, sheet.getSheetName(), |
370 | | - 0, SpreadsheetVersion.EXCEL2007.getLastRowIndex(), rowsToMove, SpreadsheetVersion.EXCEL2007); |
371 | | - formulaShifter.adjustFormula(ptgs, sheetIndex); |
| 369 | + final int colsToMove = currentColNum - sf.getCellAddress().getColumn(); |
| 370 | + org.apache.poi.ss.formula.SharedFormula formulaShifter = new org.apache.poi.ss.formula.SharedFormula( |
| 371 | + SpreadsheetVersion.EXCEL2007); |
| 372 | + ptgs = formulaShifter.convertSharedFormulas(ptgs, rowsToMove, colsToMove); |
372 | 373 | // There are some corner cases regarding formulas in this PR |
373 | 374 | // https://github.com/pjfanning/excel-streaming-reader/issues/151 |
374 | 375 | String shiftedFmla = FormulaRenderer.toFormulaString(evaluationWorkbook, ptgs); |
375 | 376 |
|
376 | | - LOG.debug("cell {} should have formula {} based on shared formula {} (rowsToMove={})", |
377 | | - currentCell.getAddress(), shiftedFmla, sf.getFormula(), rowsToMove); |
| 377 | + LOG.debug("cell {} should have formula {} based on shared formula {} (rowsToMove={} colsToMove={})", |
| 378 | + currentCell.getAddress(), shiftedFmla, sf.getFormula(), rowsToMove, colsToMove); |
378 | 379 | currentCell.setFormula(shiftedFmla); |
379 | 380 | } catch (Exception e) { |
380 | 381 | LOG.warn("cell {} has a shared formula but excel-streaming-reader has an issue parsing it - will ignore the formula", |
|
0 commit comments