2008-03-19 08:28:56 -04:00
|
|
|
/* ====================================================================
|
|
|
|
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;
|
|
|
|
|
2014-05-20 10:01:22 -04:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2008-12-23 11:19:07 -05:00
|
|
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
2008-10-30 16:17:04 -04:00
|
|
|
import org.apache.poi.ss.formula.Formula;
|
2014-05-20 10:01:22 -04:00
|
|
|
import org.apache.poi.ss.formula.ptg.Ptg;
|
2008-11-13 20:25:16 -05:00
|
|
|
import org.apache.poi.util.LittleEndianOutput;
|
2008-03-19 08:28:56 -04:00
|
|
|
|
|
|
|
/**
|
2015-07-12 13:06:45 -04:00
|
|
|
* Conditional Formatting Rule Record (0x01B1).
|
|
|
|
*
|
|
|
|
* <p>This is for the older-style Excel conditional formattings,
|
2015-07-12 13:31:54 -04:00
|
|
|
* new-style (Excel 2007+) also make use of {@link CFRule12Record}
|
2015-11-23 18:57:40 -05:00
|
|
|
* for their rules.</p>
|
2008-03-19 08:28:56 -04:00
|
|
|
*/
|
2015-09-28 19:16:58 -04:00
|
|
|
public final class CFRuleRecord extends CFRuleBase implements Cloneable {
|
2015-07-12 15:28:27 -04:00
|
|
|
public static final short sid = 0x01B1;
|
|
|
|
|
|
|
|
/** Creates new CFRuleRecord */
|
2015-07-12 16:38:57 -04:00
|
|
|
private CFRuleRecord(byte conditionType, byte comparisonOperation) {
|
|
|
|
super(conditionType, comparisonOperation);
|
|
|
|
setDefaults();
|
|
|
|
}
|
2015-07-12 15:28:27 -04:00
|
|
|
|
2015-07-12 16:38:57 -04:00
|
|
|
private CFRuleRecord(byte conditionType, byte comparisonOperation, Ptg[] formula1, Ptg[] formula2) {
|
|
|
|
super(conditionType, comparisonOperation, formula1, formula2);
|
|
|
|
setDefaults();
|
|
|
|
}
|
|
|
|
private void setDefaults() {
|
2015-07-12 15:28:27 -04:00
|
|
|
// Set modification flags to 1: by default options are not modified
|
2015-07-13 12:07:39 -04:00
|
|
|
formatting_options = modificationBits.setValue(formatting_options, -1);
|
2015-07-12 15:28:27 -04:00
|
|
|
// Set formatting block flags to 0 (no formatting blocks)
|
2015-07-13 12:07:39 -04:00
|
|
|
formatting_options = fmtBlockBits.setValue(formatting_options, 0);
|
|
|
|
formatting_options = undocumented.clear(formatting_options);
|
2015-07-12 15:28:27 -04:00
|
|
|
|
2015-07-13 12:07:39 -04:00
|
|
|
formatting_not_used = (short)0x8002; // Excel seems to write this value, but it doesn't seem to care what it reads
|
2015-07-13 10:51:36 -04:00
|
|
|
_fontFormatting = null;
|
|
|
|
_borderFormatting = null;
|
|
|
|
_patternFormatting = null;
|
2015-07-12 15:28:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new comparison operation rule
|
2016-07-04 19:45:19 -04:00
|
|
|
*
|
|
|
|
* @param sheet the sheet
|
|
|
|
* @param formulaText the formula text
|
|
|
|
*
|
|
|
|
* @return a new comparison operation rule
|
2015-07-12 15:28:27 -04:00
|
|
|
*/
|
2008-12-23 11:19:07 -05:00
|
|
|
public static CFRuleRecord create(HSSFSheet sheet, String formulaText) {
|
|
|
|
Ptg[] formula1 = parseFormula(formulaText, sheet);
|
|
|
|
return new CFRuleRecord(CONDITION_TYPE_FORMULA, ComparisonOperator.NO_COMPARISON,
|
|
|
|
formula1, null);
|
|
|
|
}
|
2015-07-12 15:28:27 -04:00
|
|
|
/**
|
|
|
|
* Creates a new comparison operation rule
|
2016-07-04 19:45:19 -04:00
|
|
|
*
|
|
|
|
* @param sheet the sheet
|
|
|
|
* @param comparisonOperation the comparison operation
|
|
|
|
* @param formulaText1 the first formula text
|
|
|
|
* @param formulaText2 the second formula text
|
|
|
|
*
|
|
|
|
* @return a new comparison operation rule
|
2015-07-12 15:28:27 -04:00
|
|
|
*/
|
|
|
|
public static CFRuleRecord create(HSSFSheet sheet, byte comparisonOperation,
|
|
|
|
String formulaText1, String formulaText2) {
|
|
|
|
Ptg[] formula1 = parseFormula(formulaText1, sheet);
|
|
|
|
Ptg[] formula2 = parseFormula(formulaText2, sheet);
|
|
|
|
return new CFRuleRecord(CONDITION_TYPE_CELL_VALUE_IS, comparisonOperation, formula1, formula2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public CFRuleRecord(RecordInputStream in) {
|
2015-07-12 16:38:57 -04:00
|
|
|
setConditionType(in.readByte());
|
|
|
|
setComparisonOperation(in.readByte());
|
2015-07-12 15:28:27 -04:00
|
|
|
int field_3_formula1_len = in.readUShort();
|
|
|
|
int field_4_formula2_len = in.readUShort();
|
2015-07-13 10:51:36 -04:00
|
|
|
readFormatOptions(in);
|
2015-07-12 15:28:27 -04:00
|
|
|
|
|
|
|
// "You may not use unions, intersections or array constants in Conditional Formatting criteria"
|
2015-07-12 16:38:57 -04:00
|
|
|
setFormula1(Formula.read(field_3_formula1_len, in));
|
|
|
|
setFormula2(Formula.read(field_4_formula2_len, in));
|
2015-07-12 15:28:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2015-07-12 15:28:27 -04:00
|
|
|
public short getSid() {
|
|
|
|
return sid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* called by the class that is responsible for writing this sucker.
|
|
|
|
* Subclasses should implement this so that their data is passed back in a
|
|
|
|
* byte array.
|
|
|
|
*
|
|
|
|
* @param out the stream to write to
|
|
|
|
*/
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2015-07-12 15:28:27 -04:00
|
|
|
public void serialize(LittleEndianOutput out) {
|
2015-07-12 16:38:57 -04:00
|
|
|
int formula1Len=getFormulaSize(getFormula1());
|
|
|
|
int formula2Len=getFormulaSize(getFormula2());
|
2015-07-12 15:28:27 -04:00
|
|
|
|
2015-07-12 16:38:57 -04:00
|
|
|
out.writeByte(getConditionType());
|
|
|
|
out.writeByte(getComparisonOperation());
|
2015-07-12 15:28:27 -04:00
|
|
|
out.writeShort(formula1Len);
|
|
|
|
out.writeShort(formula2Len);
|
2015-07-13 12:07:39 -04:00
|
|
|
|
|
|
|
serializeFormattingBlock(out);
|
2015-07-12 15:28:27 -04:00
|
|
|
|
2015-07-12 16:38:57 -04:00
|
|
|
getFormula1().serializeTokens(out);
|
|
|
|
getFormula2().serializeTokens(out);
|
2015-07-12 15:28:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2015-07-12 15:28:27 -04:00
|
|
|
protected int getDataSize() {
|
2015-07-13 14:09:12 -04:00
|
|
|
return 6 + getFormattingBlockSize() +
|
2015-07-13 12:07:39 -04:00
|
|
|
getFormulaSize(getFormula1())+
|
|
|
|
getFormulaSize(getFormula2());
|
2015-07-12 15:28:27 -04:00
|
|
|
}
|
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2015-07-12 15:28:27 -04:00
|
|
|
public String toString() {
|
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
|
buffer.append("[CFRULE]\n");
|
2015-07-12 16:38:57 -04:00
|
|
|
buffer.append(" .condition_type =").append(getConditionType()).append("\n");
|
2015-07-12 15:28:27 -04:00
|
|
|
buffer.append(" OPTION FLAGS=0x").append(Integer.toHexString(getOptions())).append("\n");
|
|
|
|
if (containsFontFormattingBlock()) {
|
|
|
|
buffer.append(_fontFormatting.toString()).append("\n");
|
|
|
|
}
|
|
|
|
if (containsBorderFormattingBlock()) {
|
|
|
|
buffer.append(_borderFormatting.toString()).append("\n");
|
|
|
|
}
|
|
|
|
if (containsPatternFormattingBlock()) {
|
|
|
|
buffer.append(_patternFormatting.toString()).append("\n");
|
|
|
|
}
|
2015-07-12 16:38:57 -04:00
|
|
|
buffer.append(" Formula 1 =").append(Arrays.toString(getFormula1().getTokens())).append("\n");
|
|
|
|
buffer.append(" Formula 2 =").append(Arrays.toString(getFormula2().getTokens())).append("\n");
|
2015-07-12 15:28:27 -04:00
|
|
|
buffer.append("[/CFRULE]\n");
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:16:58 -04:00
|
|
|
@Override
|
|
|
|
public CFRuleRecord clone() {
|
2015-07-12 16:38:57 -04:00
|
|
|
CFRuleRecord rec = new CFRuleRecord(getConditionType(), getComparisonOperation());
|
2015-07-13 10:51:36 -04:00
|
|
|
super.copyTo(rec);
|
2015-07-12 15:28:27 -04:00
|
|
|
return rec;
|
|
|
|
}
|
2008-03-19 08:28:56 -04:00
|
|
|
}
|