Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.logging.Logger;

import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static String getStringValueFromFormula(String formula,
Spreadsheet spreadsheet) {
List<String> strings = new ArrayList<String>();

for (CellReference ref : getAllReferencedCells(formula)) {
for (CellReference ref : getAllReferencedCells(spreadsheet.getWorkbook().getSpreadsheetVersion(), formula)) {
strings.add(getStringValue(ref, spreadsheet));
}

Expand All @@ -80,9 +81,9 @@ public static String join(final List<String> array,
return buf.toString();
}

public static List<CellReference> getAllReferencedCells(String formula) {
public static List<CellReference> getAllReferencedCells(SpreadsheetVersion version, String formula) {
ArrayList<CellReference> cellRefs = new ArrayList<CellReference>();
for (AreaReference area : getAreaReferences(formula)) {
for (AreaReference area : getAreaReferences(version, formula)) {
cellRefs.addAll(Arrays.asList(area.getAllReferencedCells()));
}
return cellRefs;
Expand All @@ -92,13 +93,13 @@ public static List<CellReference> getAllReferencedCells(String formula) {
* Returns an array of contiguous area references addressed by the given
* formula.
*/
public static AreaReference[] getAreaReferences(String formula) {
public static AreaReference[] getAreaReferences(SpreadsheetVersion version, String formula) {
// generateContiguous cannot parse a formula in parentheses
if (formula.startsWith("(") && formula.endsWith(")")) {
formula = formula.substring(1, formula.length() - 1);
}

return AreaReference.generateContiguous(formula);
return AreaReference.generateContiguous(version, formula);
}

/**
Expand Down Expand Up @@ -127,7 +128,7 @@ public static List<CellReference> getAllReferencedVisibleCells(
*/
public static List<CellReference> getAllReferencedCells(String formula,
Spreadsheet spreadsheet, boolean includeHiddenCells) {
final List<CellReference> cellRefs = getAllReferencedCells(formula);
final List<CellReference> cellRefs = getAllReferencedCells(spreadsheet.getWorkbook().getSpreadsheetVersion(), formula);

if (includeHiddenCells) {
return cellRefs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected void createSeriesDataPoints(CTNumDataSource val,
@Override
public void dataSelected() {
AreaReference[] areaReferences = Utils
.getAreaReferences(formula);
.getAreaReferences(spreadsheet.getWorkbook().getSpreadsheetVersion(), formula);

spreadsheet.setSelectionRange(
areaReferences[0].getFirstCell().getRow(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private String getTitle(XSSFChart chart, List<AbstractSeriesData> plotData) {
final CTChart ctChart = chart.getCTChart();

if (ctChart.isSetTitle()) {
title = "" + chart.getTitle();
title = "" + chart.getTitleText();

// default title
if (title.isEmpty() && plotData.size() > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1099,12 +1099,14 @@ protected ArrayList<CellData> loadCellDataForRowAndColumnRange(
final Collection<String> customComponentCells = (Collection<String>) (componentIDtoCellKeysMap == null ? Collections
.emptyList() : componentIDtoCellKeysMap.values());

spreadsheet.getConditionalFormatter().evaluateBatch(formatter -> {
for (int r = firstRow - 1; r < lastRow; r++) {
// iterate in reverse row/column order (bottom right to top left) to match CSS order for border calculations,
// to avoid issues like #651 where cell values are not updated in the proper sequence.
spreadsheet.getConditionalFormatter().evaluateBatch(formatter -> {
for (int r = lastRow - 1; r >= lastRow -1; r--) {
Row row = activeSheet.getRow(r);
if (row != null && row.getLastCellNum() != -1
&& row.getLastCellNum() >= firstColumn) {
for (int c = firstColumn - 1; c < lastColumn; c++) {
for (int c = lastColumn - 1; c >= firstColumn -1; c--) {
final String key = SpreadsheetUtil.toKey(c + 1, r + 1);
if (!customComponentCells.contains(key)
&& !sentCells.contains(key)
Expand Down Expand Up @@ -1156,13 +1158,15 @@ protected void updateMarkedCellValues() {

// update all cached formula cell values on client side, because they
// might have changed. also make sure all marked cells are updated
Iterator<Row> rows = sheet.rowIterator();
// iterate in reverse row/column order (bottom right to top left) to match CSS order for border calculations,
// to avoid issues like #651 where cell values are not updated in the proper sequence.
spreadsheet.getConditionalFormatter().evaluateBatch(formatter -> {
while (rows.hasNext()) {
final Row r = rows.next();
final Iterator<Cell> cells = r.cellIterator();
while (cells.hasNext()) {
final Cell cell = cells.next();
for (int ri = sheet.getLastRowNum(); ri >= 0; ri--) {
final Row r = sheet.getRow(ri);
if (r == null) continue;
for (int ci = r.getLastCellNum() - 1; ci >= 0; ci--) {
final Cell cell = r.getCell(ci);
if (cell == null) continue;
int rowIndex = cell.getRowIndex();
int columnIndex = cell.getColumnIndex();
final String key = SpreadsheetUtil.toKey(columnIndex + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -56,7 +55,6 @@
@SuppressWarnings("serial")
public class ConditionalFormatter implements Serializable {

@SuppressWarnings("unused")
private static final Logger LOGGER = Logger
.getLogger(ConditionalFormatter.class.getName());

Expand Down Expand Up @@ -99,9 +97,21 @@ public static interface ConditionalFormatterEvaluator extends Serializable {

/**
* starting index for conditional formatting CSS styles.
* Should be high enough to avoid conflicts with other types.
* Should be high enough to avoid conflicts with other types.
* <p>
* Currently we have:
* <br>0-255 = cell styles by index
* <br>1000000000-1999999999 = conditional formats
* <p>
* Eventually this needs to include table styles and change to:
* <p>
* <br>0-99999999 = table styles
* <br>100000000-100000255 = cell styles
* <br>1000000000-1999999999 = conditional formats
* <p>
* This is the order Excel applies styles.
*/
public static final int BASE_CONDITIONAL_FORMAT_CSS_INDEX = 9000000;
public static final int BASE_CONDITIONAL_FORMAT_CSS_INDEX = 1000000000;

private Spreadsheet spreadsheet;

Expand Down Expand Up @@ -355,10 +365,10 @@ protected void buildStylesForSheet(Sheet sheet) {
protected int getCssIndex(EvaluationConditionalFormatRule rule, IncrementalStyleBuilder.StyleType type) {
// each rule has 3 possible styles (StyleType). Start at 9M just in case.
return BASE_CONDITIONAL_FORMAT_CSS_INDEX
+ spreadsheet.getWorkbook().getSheetIndex(rule.getSheet().getSheetName()) * 100000 // 899 sheets
+ spreadsheet.getWorkbook().getSheetIndex(rule.getSheet().getSheetName()) * 1000000 // 999 sheets
+ rule.getFormattingIndex() * 10000 // room for 99 formatting indexes per sheet (mostly 1:1 with regions)
+ rule.getRuleIndex() * 10 // room for 999 rules per formatting (HSSF max 3, XSSF unlimited)
+ type.ordinal(); // 0-2
+ type.ordinal(); // 0-7
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderFormatting;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Color;
Expand All @@ -45,7 +46,7 @@ public class HSSFColorConverter implements ColorConverter {
private String defaultBackgroundColor;
private String defaultColor;

private static final HSSFColor HSSF_AUTO = new HSSFColor.AUTOMATIC();
private static final HSSFColor HSSF_AUTO = HSSFColorPredefined.AUTOMATIC.getColor();

public HSSFColorConverter(HSSFWorkbook wb) {
this.wb = wb;
Expand Down Expand Up @@ -92,13 +93,13 @@ public void colorStyles(final CellStyle cellStyle, final StringBuilder sb) {
String backgroundColor = null;
HSSFColor fillForegroundColorColor = cs.getFillForegroundColorColor();
if (fillForegroundColorColor != null
&& fillForegroundColor != HSSFColor.AUTOMATIC.index) {
&& fillForegroundColor != HSSFColorPredefined.AUTOMATIC.getIndex()) {
backgroundColor = styleColor(fillForegroundColor);
} else {
HSSFColor fillBackgroundColorColor = cs
.getFillBackgroundColorColor();
if (fillBackgroundColorColor != null
&& fillBackgroundColor != HSSFColor.AUTOMATIC.index) {
&& fillBackgroundColor != HSSFColorPredefined.AUTOMATIC.getIndex()) {
backgroundColor = styleColor(fillBackgroundColor);
}
}
Expand Down Expand Up @@ -141,13 +142,13 @@ public boolean hasBackgroundColor(CellStyle cellStyle) {

HSSFColor fillForegroundColorColor = cs.getFillForegroundColorColor();
if (fillForegroundColorColor != null
&& fillForegroundColor != HSSFColor.AUTOMATIC.index) {
&& fillForegroundColor != HSSFColorPredefined.AUTOMATIC.getIndex()) {
return true;
} else {
HSSFColor fillBackgroundColorColor = cs
.getFillBackgroundColorColor();
if (fillBackgroundColorColor != null
&& fillBackgroundColor != HSSFColor.AUTOMATIC.index) {
&& fillBackgroundColor != HSSFColorPredefined.AUTOMATIC.getIndex()) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.SheetVisibility;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress;
Expand Down Expand Up @@ -982,7 +983,7 @@ public void setSheetHidden(int sheetPOIIndex, int hidden)
boolean isHidden = workbook.isSheetHidden(sheetPOIIndex);
boolean isVeryHidden = workbook.isSheetVeryHidden(sheetPOIIndex);
int activeSheetIndex = workbook.getActiveSheetIndex();
workbook.setSheetHidden(sheetPOIIndex, hidden);
workbook.setSheetVisibility(sheetPOIIndex, hidden == 2 ? SheetVisibility.VERY_HIDDEN : (hidden == 1 ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE));

// skip component reload if "nothing changed"
if (hidden == 0 && (isHidden || isVeryHidden) || hidden != 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vaadin.addon.spreadsheet.test;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import com.vaadin.addon.spreadsheet.test.pageobjects.SpreadsheetPage;

public class ConditionalFormattingFormulaUpdateOrderTest
extends AbstractSpreadsheetTestCase {

private static final String FALSE_CONDITION_COLOR = "rgba(255, 255, 255, 1)";
private static final String TRUE_CONDITION_COLOR = "rgba(255, 199, 206, 1)";

private SpreadsheetPage spreadsheetPage;

@Override
public void setUp() throws Exception {
super.setUp();
spreadsheetPage = headerPage.loadFile(
"conditional.formatting.-.formula.cells.xlsx", this);
spreadsheetPage.selectSheetAt(0);
}

@Test
public void loadSpreadsheetWithConditionalFormatting_MakeConditionTrue_CellB1FilledRed() {
spreadsheetPage.setCellValue("A1", "6");
assertEquals(TRUE_CONDITION_COLOR, spreadsheetPage.getCellColor("B1"));
}

}
Binary file not shown.