Skip to content

Commit

Permalink
fix single cell formulas updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tinhol committed Aug 5, 2015
1 parent ecc00a8 commit a87298e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ protected void updateFormula(Cell cellWithFormula, Range originalFormulaRange, R
CTCellFormula formula = cellWithFormula.getF();
formula.setValue(formula.getValue().replace(originalFormulaRange.toRange(), formulaRange.toRange()));
if (originalFormulaRange.isOneCellRange() && formulaRange.isOneCellRange()) {
formula.setValue(formula.getValue().replace(originalFormulaRange.toFirstCellReference(), formulaRange.toFirstCellReference()));
//here we check that there are no alpha-numeric symbols around the single reference
String pattern = "(?<!\\w+)" + originalFormulaRange.toFirstCellReference() + "(?!\\w+)";
formula.setValue(formula.getValue().replaceAll(pattern, formulaRange.toFirstCellReference()));
}

if (calculationChain != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.haulmont.yarg.formatters.impl;

import com.haulmont.yarg.formatters.CustomReport;
import com.haulmont.yarg.formatters.factory.FormatterFactoryInput;
import com.haulmont.yarg.formatters.impl.xlsx.Range;
import com.haulmont.yarg.structure.BandData;
import com.haulmont.yarg.structure.ReportOutputType;
import com.haulmont.yarg.structure.ReportTemplate;
import org.junit.Assert;
import org.junit.Test;
import org.xlsx4j.sml.CTCellFormula;
import org.xlsx4j.sml.Cell;

import java.io.InputStream;

/**
* @author degtyarjov
* @version $Id$
*/
public class XlsxFormulaUpdateTest {
private ReportTemplate reportTemplate = new ReportTemplate() {
@Override
public String getCode() {
return null;
}

@Override
public String getDocumentName() {
return null;
}

@Override
public String getDocumentPath() {
return null;
}

@Override
public InputStream getDocumentContent() {
return null;
}

@Override
public ReportOutputType getOutputType() {
return null;
}

@Override
public String getOutputNamePattern() {
return null;
}

@Override
public boolean isCustom() {
return false;
}

@Override
public CustomReport getCustomReport() {
return null;
}
};

//todo test formulas grows
@Test
public void testFormulaShifts() throws Exception {
XlsxFormatter xlsxFormatter = new XlsxFormatter(
new FormatterFactoryInput("xls", new BandData(""), reportTemplate, null));

Cell cellWithFormula = cellWithFormula("SUM(A9:A9)/B9");
xlsxFormatter.updateFormula(cellWithFormula, Range.fromRange("Sheet", "A9:A9"), Range.fromRange("Sheet", "A90:A90"), null, 0);
xlsxFormatter.updateFormula(cellWithFormula, Range.fromRange("Sheet", "B9:B9"), Range.fromRange("Sheet", "B90:B90"), null, 0);
Assert.assertEquals("SUM(A90:A90)/B90", cellWithFormula.getF().getValue());

cellWithFormula = cellWithFormula("SUM(A9:B9)");
xlsxFormatter.updateFormula(cellWithFormula, Range.fromRange("Sheet", "A9:B9"), Range.fromRange("Sheet", "A90:B90"), null, 0);
Assert.assertEquals("SUM(A90:B90)", cellWithFormula.getF().getValue());

cellWithFormula = cellWithFormula("A9*SUM(A9:A9)");
xlsxFormatter.updateFormula(cellWithFormula, Range.fromRange("Sheet", "A9:A9"), Range.fromRange("Sheet", "A90:A90"), null, 0);
Assert.assertEquals("A90*SUM(A90:A90)", cellWithFormula.getF().getValue());

cellWithFormula = cellWithFormula("CA9*SUM(A9:A9)");
xlsxFormatter.updateFormula(cellWithFormula, Range.fromRange("Sheet", "A9:A9"), Range.fromRange("Sheet", "A90:A90"), null, 0);
Assert.assertEquals("CA9*SUM(A90:A90)", cellWithFormula.getF().getValue());
}

private Cell cellWithFormula(String formula) {
Cell cellWithFormula = new Cell();
CTCellFormula ctCellFormula = new CTCellFormula();
ctCellFormula.setValue(formula);
cellWithFormula.setF(ctCellFormula);
return cellWithFormula;
}
}

0 comments on commit a87298e

Please sign in to comment.