import org.apache.poi.hssf.record.cf.ColorGradientFormatting;
import org.apache.poi.hssf.record.cf.ColorGradientThreshold;
+import org.apache.poi.hssf.record.cf.DataBarFormatting;
+import org.apache.poi.hssf.record.cf.DataBarThreshold;
import org.apache.poi.hssf.record.cf.IconMultiStateFormatting;
import org.apache.poi.hssf.record.cf.IconMultiStateThreshold;
import org.apache.poi.hssf.record.cf.Threshold;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.formula.Formula;
import org.apache.poi.ss.formula.ptg.Ptg;
+import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.HexDump;
private byte template_param_length;
private byte[] template_params;
+ private DataBarFormatting data_bar;
private IconMultiStateFormatting multistate;
private ColorGradientFormatting color_gradient;
-
- // TODO Parse these
- private byte[] databar_data;
+ // TODO Parse this, see #58150
private byte[] filter_data;
/** Creates new CFRuleRecord */
return new CFRule12Record(CONDITION_TYPE_CELL_VALUE_IS, comparisonOperation,
formula1, formula2, formula3);
}
+ /**
+ * Creates a new Data Bar formatting
+ */
+ public static CFRule12Record create(HSSFSheet sheet, ExtendedColor color) {
+ CFRule12Record r = new CFRule12Record(CONDITION_TYPE_DATA_BAR,
+ ComparisonOperator.NO_COMPARISON);
+ DataBarFormatting dbf = r.createDataBarFormatting();
+ dbf.setColor(color);
+ dbf.setPercentMin((byte)50);
+ dbf.setPercentMax((byte)50);
+
+ DataBarThreshold min = new DataBarThreshold();
+ min.setType(RangeType.MIN.id);
+ dbf.setThresholdMin(min);
+
+ DataBarThreshold max = new DataBarThreshold();
+ max.setType(RangeType.MAX.id);
+ dbf.setThresholdMax(max);
+
+ return r;
+ }
/**
* Creates a new Icon Set / Multi-State formatting
*/
cgf.setColors(colors);
return r;
}
- // TODO Static creators for Data Bars
public CFRule12Record(RecordInputStream in) {
futureHeader = new FtrHeader(in);
if (type == CONDITION_TYPE_COLOR_SCALE) {
color_gradient = new ColorGradientFormatting(in);
} else if (type == CONDITION_TYPE_DATA_BAR) {
- databar_data = in.readRemainder();
+ data_bar = new DataBarFormatting(in);
} else if (type == CONDITION_TYPE_FILTER) {
filter_data = in.readRemainder();
} else if (type == CONDITION_TYPE_ICON_SET) {
}
}
+ public boolean containsDataBarBlock() {
+ return (data_bar != null);
+ }
+ public DataBarFormatting getDataBarFormatting() {
+ return data_bar;
+ }
+ public DataBarFormatting createDataBarFormatting() {
+ if (data_bar != null) return data_bar;
+
+ // Convert, setup and return
+ setConditionType(CONDITION_TYPE_DATA_BAR);
+ data_bar = new DataBarFormatting();
+ return data_bar;
+ }
+
public boolean containsMultiStateBlock() {
return (multistate != null);
}
if (type == CONDITION_TYPE_COLOR_SCALE) {
color_gradient.serialize(out);
} else if (type == CONDITION_TYPE_DATA_BAR) {
- out.write(databar_data);
+ data_bar.serialize(out);
} else if (type == CONDITION_TYPE_FILTER) {
out.write(filter_data);
} else if (type == CONDITION_TYPE_ICON_SET) {
if (type == CONDITION_TYPE_COLOR_SCALE) {
len += color_gradient.getDataLength();
} else if (type == CONDITION_TYPE_DATA_BAR) {
- len += databar_data.length;
+ len += data_bar.getDataLength();
} else if (type == CONDITION_TYPE_FILTER) {
len += filter_data.length;
} else if (type == CONDITION_TYPE_ICON_SET) {
buffer.append(" .priority =").append(priority).append("\n");
buffer.append(" .template_type =").append(template_type).append("\n");
buffer.append(" .template_params=").append(HexDump.toHex(template_params)).append("\n");
- buffer.append(" .databar_data =").append(HexDump.toHex(databar_data)).append("\n");
buffer.append(" .filter_data =").append(HexDump.toHex(filter_data)).append("\n");
if (color_gradient != null) {
buffer.append(color_gradient);
if (multistate != null) {
buffer.append(multistate);
}
+ if (data_bar != null) {
+ buffer.append(data_bar);
+ }
buffer.append("[/CFRULE12]\n");
return buffer.toString();
}
rec.template_params = new byte[template_param_length];
System.arraycopy(template_params, 0, rec.template_params, 0, template_param_length);
- // TODO Clone the rgbCT data like Gradients, Databars etc
+ if (color_gradient != null) {
+ rec.color_gradient = (ColorGradientFormatting)color_gradient.clone();
+ }
+ if (multistate != null) {
+ rec.multistate = (IconMultiStateFormatting)multistate.clone();
+ }
+ if (data_bar != null) {
+ rec.data_bar = (DataBarFormatting)data_bar.clone();
+ }
+ if (filter_data != null) {
+ rec.filter_data = new byte[filter_data.length];
+ System.arraycopy(filter_data, 0, rec.filter_data, 0, filter_data.length);
+ }
return rec;
}
--- /dev/null
+/* ====================================================================
+ 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.poi.hssf.record.cf;
+
+import org.apache.poi.hssf.record.common.ExtendedColor;
+import org.apache.poi.util.BitField;
+import org.apache.poi.util.BitFieldFactory;
+import org.apache.poi.util.LittleEndianInput;
+import org.apache.poi.util.LittleEndianOutput;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+
+/**
+ * Data Bar Conditional Formatting Rule Record.
+ */
+public final class DataBarFormatting implements Cloneable {
+ private static POILogger log = POILogFactory.getLogger(DataBarFormatting.class);
+
+ private byte options = 0;
+ private byte percentMin = 0;
+ private byte percentMax = 0;
+ private ExtendedColor color;
+ private DataBarThreshold thresholdMin;
+ private DataBarThreshold thresholdMax;
+
+ private static BitField iconOnly = BitFieldFactory.getInstance(0x01);
+ private static BitField reversed = BitFieldFactory.getInstance(0x04);
+
+ public DataBarFormatting() {
+ options = 2;
+ }
+ public DataBarFormatting(LittleEndianInput in) {
+ in.readShort(); // Ignored
+ in.readByte(); // Reserved
+ options = in.readByte();
+
+ percentMin = in.readByte();
+ percentMax = in.readByte();
+ if (percentMin < 0 || percentMin > 100)
+ log.log(POILogger.WARN, "Inconsistent Minimum Percentage found " + percentMin);
+ if (percentMax < 0 || percentMax > 100)
+ log.log(POILogger.WARN, "Inconsistent Minimum Percentage found " + percentMin);
+
+ color = new ExtendedColor(in);
+ thresholdMin = new DataBarThreshold(in);
+ thresholdMax = new DataBarThreshold(in);
+ }
+
+ public boolean isIconOnly() {
+ return getOptionFlag(iconOnly);
+ }
+ public void setIconOnly(boolean only) {
+ setOptionFlag(only, iconOnly);
+ }
+
+ public boolean isReversed() {
+ return getOptionFlag(reversed);
+ }
+ public void setReversed(boolean rev) {
+ setOptionFlag(rev, reversed);
+ }
+
+ private boolean getOptionFlag(BitField field) {
+ int value = field.getValue(options);
+ return value==0 ? false : true;
+ }
+ private void setOptionFlag(boolean option, BitField field) {
+ options = field.setByteBoolean(options, option);
+ }
+
+ public byte getPercentMin() {
+ return percentMin;
+ }
+ public void setPercentMin(byte percentMin) {
+ this.percentMin = percentMin;
+ }
+
+ public byte getPercentMax() {
+ return percentMax;
+ }
+ public void setPercentMax(byte percentMax) {
+ this.percentMax = percentMax;
+ }
+
+ public ExtendedColor getColor() {
+ return color;
+ }
+ public void setColor(ExtendedColor color) {
+ this.color = color;
+ }
+
+ public DataBarThreshold getThresholdMin() {
+ return thresholdMin;
+ }
+ public void setThresholdMin(DataBarThreshold thresholdMin) {
+ this.thresholdMin = thresholdMin;
+ }
+
+ public DataBarThreshold getThresholdMax() {
+ return thresholdMax;
+ }
+ public void setThresholdMax(DataBarThreshold thresholdMax) {
+ this.thresholdMax = thresholdMax;
+ }
+
+ public String toString() {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append(" [Data Bar Formatting]\n");
+ buffer.append(" .icon_only= ").append(isIconOnly()).append("\n");
+ buffer.append(" .reversed = ").append(isReversed()).append("\n");
+ buffer.append(color);
+ buffer.append(thresholdMin);
+ buffer.append(thresholdMax);
+ buffer.append(" [/Data Bar Formatting]\n");
+ return buffer.toString();
+ }
+
+ public Object clone() {
+ DataBarFormatting rec = new DataBarFormatting();
+ rec.options = options;
+ rec.percentMin = percentMin;
+ rec.percentMax = percentMax;
+ rec.color = (ExtendedColor)color.clone();
+ rec.thresholdMin = (DataBarThreshold)thresholdMin.clone();
+ rec.thresholdMax = (DataBarThreshold)thresholdMax.clone();
+ return rec;
+ }
+
+ public int getDataLength() {
+ return 6 + color.getDataLength() +
+ thresholdMin.getDataLength() +
+ thresholdMax.getDataLength();
+ }
+
+ public void serialize(LittleEndianOutput out) {
+ out.writeShort(0);
+ out.writeByte(0);
+ out.writeByte(options);
+ out.writeByte(percentMin);
+ out.writeByte(percentMax);
+ color.serialize(out);
+ thresholdMin.serialize(out);
+ thresholdMax.serialize(out);
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.poi.hssf.record.cf;
+
+import org.apache.poi.util.LittleEndianInput;
+
+/**
+ * Data Bar specific Threshold / value (CFVO),
+ * for changes in Conditional Formatting
+ */
+public final class DataBarThreshold extends Threshold {
+ public DataBarThreshold() {
+ super();
+ }
+
+ /** Creates new Data Bar Threshold */
+ public DataBarThreshold(LittleEndianInput in) {
+ super(in);
+ }
+
+ public Object clone() {
+ DataBarThreshold rec = new DataBarThreshold();
+ super.copyTo(rec);
+ return rec;
+ }
+}
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.ConditionalFormatting;
import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
+import org.apache.poi.ss.usermodel.ExtendedColor;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
import org.apache.poi.ss.util.CellRangeAddress;
return new HSSFConditionalFormattingRule(_sheet, rr);
}
+ /**
+ * Create a Databar conditional formatting rule.
+ * <p>The thresholds and colour for it will be created, but will be
+ * empty and require configuring with
+ * {@link HSSFConditionalFormattingRule#getDataBarFormatting()}
+ * then
+ * {@link HSSFDataBarFormatting#getMinThreshold()}
+ * and
+ * {@link HSSFDataBarFormatting#getMaxThreshold()}
+ */
+ public HSSFConditionalFormattingRule createConditionalFormattingRule(HSSFExtendedColor color) {
+ CFRule12Record rr = CFRule12Record.create(_sheet, color.getExtendedColor());
+ return new HSSFConditionalFormattingRule(_sheet, rr);
+ }
+ public HSSFConditionalFormattingRule createConditionalFormattingRule(ExtendedColor color) {
+ return createConditionalFormattingRule((HSSFExtendedColor)color);
+ }
+
/**
* Create a Color Scale / Color Gradient conditional formatting rule.
* <p>The thresholds and colours for it will be created, but will be
return new HSSFConditionalFormattingRule(_sheet, rr);
}
- /**
- * Create a Databar conditional formatting rule.
- * <p>The thresholds and colour for it will be created, but will be
- * empty and require configuring with
- * {@link HSSFConditionalFormattingRule#getDataBarFormatting()}
- * then
- * {@link HSSFDataBarFormatting#getMinThreshold()}
- * and
- * {@link HSSFDataBarFormatting#getMaxThreshold()}
- * and
- * {@link HSSFDataBarFormatting#getColor()}
- */
- public HSSFConditionalFormattingRule createConditionalFormattingDataBarRule() {
- throw new IllegalStateException("Not Implemented Yet!"); // TODO Implement
- }
-
/**
* Adds a copy of HSSFConditionalFormatting object to the sheet
* <p>This method could be used to copy HSSFConditionalFormatting object
*/\r
ConditionalFormattingRule createConditionalFormattingRule(String formula);\r
\r
+ /**\r
+ * Create a Databar conditional formatting rule.\r
+ * <p>The thresholds and colour for it will be created, but will be \r
+ * empty and require configuring with \r
+ * {@link ConditionalFormattingRule#getDataBarFormatting()}\r
+ * then\r
+ * {@link DataBarFormatting#getMinThreshold()}\r
+ * and \r
+ * {@link DataBarFormatting#getMaxThreshold()}\r
+ */\r
+ ConditionalFormattingRule createConditionalFormattingRule(ExtendedColor color);\r
+ \r
/**\r
* Create an Icon Set / Multi-State conditional formatting rule.\r
* <p>The thresholds for it will be created, but will be empty\r
*/\r
ConditionalFormattingRule createConditionalFormattingColorScaleRule();\r
\r
- /**\r
- * Create a Databar conditional formatting rule.\r
- * <p>The thresholds and colour for it will be created, but will be \r
- * empty and require configuring with \r
- * {@link ConditionalFormattingRule#getDataBarFormatting()}\r
- * then\r
- * {@link DataBarFormatting#getMinThreshold()}\r
- * and \r
- * {@link DataBarFormatting#getMaxThreshold()}\r
- * and\r
- * {@link DataBarFormatting#getColor()}\r
- */\r
- ConditionalFormattingRule createConditionalFormattingDataBarRule();\r
- \r
/**\r
* Gets Conditional Formatting object at a particular index\r
*\r
import org.apache.poi.ss.usermodel.ComparisonOperator;\r
import org.apache.poi.ss.usermodel.ConditionalFormatting;\r
import org.apache.poi.ss.usermodel.ConditionalFormattingRule;\r
+import org.apache.poi.ss.usermodel.ExtendedColor;\r
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;\r
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;\r
import org.apache.poi.ss.util.CellRangeAddress;\r
return rule;\r
}\r
\r
+ /**\r
+ * Create a Databar conditional formatting rule.\r
+ * <p>The thresholds and colour for it will be created, but will be \r
+ * empty and require configuring with \r
+ * {@link XSSFConditionalFormattingRule#getDataBarFormatting()}\r
+ * then\r
+ * {@link XSSFDataBarFormatting#getMinThreshold()}\r
+ * and \r
+ * {@link XSSFDataBarFormatting#getMaxThreshold()}\r
+ */\r
+ public XSSFConditionalFormattingRule createConditionalFormattingRule(XSSFColor color) {\r
+ throw new IllegalStateException("Not Implemented Yet!"); // TODO Implement\r
+ }\r
+ public XSSFConditionalFormattingRule createConditionalFormattingRule(ExtendedColor color) {\r
+ return createConditionalFormattingRule((XSSFColor)color);\r
+ }\r
+ \r
/**\r
* A factory method allowing the creation of conditional formatting\r
* rules using an Icon Set / Multi-State formatting.\r
return rule;\r
}\r
\r
- /**\r
- * Create a Databar conditional formatting rule.\r
- * <p>The thresholds and colour for it will be created, but will be \r
- * empty and require configuring with \r
- * {@link XSSFConditionalFormattingRule#getDataBarFormatting()}\r
- * then\r
- * {@link XSSFDataBarFormatting#getMinThreshold()}\r
- * and \r
- * {@link XSSFDataBarFormatting#getMaxThreshold()}\r
- * and\r
- * {@link XSSFDataBarFormatting#getColor()}\r
- */\r
- public XSSFConditionalFormattingRule createConditionalFormattingDataBarRule() {\r
- throw new IllegalStateException("Not Implemented Yet!"); // TODO Implement\r
- }\r
- \r
@SuppressWarnings("deprecation")\r
public int addConditionalFormatting(CellRangeAddress[] regions, ConditionalFormattingRule[] cfRules) {\r
if (regions == null) {\r