You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CFRuleRecord.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record;
  16. import org.apache.poi.hssf.usermodel.HSSFSheet;
  17. import org.apache.poi.ss.formula.Formula;
  18. import org.apache.poi.ss.formula.ptg.Ptg;
  19. import org.apache.poi.util.LittleEndianOutput;
  20. /**
  21. * Conditional Formatting Rule Record (0x01B1).
  22. *
  23. * <p>This is for the older-style Excel conditional formattings,
  24. * new-style (Excel 2007+) also make use of {@link CFRule12Record}
  25. * for their rules.</p>
  26. */
  27. public final class CFRuleRecord extends CFRuleBase {
  28. public static final short sid = 0x01B1;
  29. public CFRuleRecord(CFRuleRecord other) {
  30. super(other);
  31. }
  32. private CFRuleRecord(byte conditionType, byte comparisonOperation, Ptg[] formula1, Ptg[] formula2) {
  33. super(conditionType, comparisonOperation, formula1, formula2);
  34. setDefaults();
  35. }
  36. private void setDefaults() {
  37. // Set modification flags to 1: by default options are not modified
  38. formatting_options = modificationBits.setValue(formatting_options, -1);
  39. // Set formatting block flags to 0 (no formatting blocks)
  40. formatting_options = fmtBlockBits.setValue(formatting_options, 0);
  41. formatting_options = undocumented.clear(formatting_options);
  42. formatting_not_used = (short)0x8002; // Excel seems to write this value, but it doesn't seem to care what it reads
  43. _fontFormatting = null;
  44. _borderFormatting = null;
  45. _patternFormatting = null;
  46. }
  47. /**
  48. * Creates a new comparison operation rule
  49. *
  50. * @param sheet the sheet
  51. * @param formulaText the formula text
  52. *
  53. * @return a new comparison operation rule
  54. */
  55. public static CFRuleRecord create(HSSFSheet sheet, String formulaText) {
  56. Ptg[] formula1 = parseFormula(formulaText, sheet);
  57. return new CFRuleRecord(CONDITION_TYPE_FORMULA, ComparisonOperator.NO_COMPARISON,
  58. formula1, null);
  59. }
  60. /**
  61. * Creates a new comparison operation rule
  62. *
  63. * @param sheet the sheet
  64. * @param comparisonOperation the comparison operation
  65. * @param formulaText1 the first formula text
  66. * @param formulaText2 the second formula text
  67. *
  68. * @return a new comparison operation rule
  69. */
  70. public static CFRuleRecord create(HSSFSheet sheet, byte comparisonOperation,
  71. String formulaText1, String formulaText2) {
  72. Ptg[] formula1 = parseFormula(formulaText1, sheet);
  73. Ptg[] formula2 = parseFormula(formulaText2, sheet);
  74. return new CFRuleRecord(CONDITION_TYPE_CELL_VALUE_IS, comparisonOperation, formula1, formula2);
  75. }
  76. public CFRuleRecord(RecordInputStream in) {
  77. setConditionType(in.readByte());
  78. setComparisonOperation(in.readByte());
  79. int field_3_formula1_len = in.readUShort();
  80. int field_4_formula2_len = in.readUShort();
  81. readFormatOptions(in);
  82. // "You may not use unions, intersections or array constants in Conditional Formatting criteria"
  83. setFormula1(Formula.read(field_3_formula1_len, in));
  84. setFormula2(Formula.read(field_4_formula2_len, in));
  85. }
  86. @Override
  87. public short getSid() {
  88. return sid;
  89. }
  90. /**
  91. * called by the class that is responsible for writing this sucker.
  92. * Subclasses should implement this so that their data is passed back in a
  93. * byte array.
  94. *
  95. * @param out the stream to write to
  96. */
  97. @Override
  98. public void serialize(LittleEndianOutput out) {
  99. int formula1Len=getFormulaSize(getFormula1());
  100. int formula2Len=getFormulaSize(getFormula2());
  101. out.writeByte(getConditionType());
  102. out.writeByte(getComparisonOperation());
  103. out.writeShort(formula1Len);
  104. out.writeShort(formula2Len);
  105. serializeFormattingBlock(out);
  106. getFormula1().serializeTokens(out);
  107. getFormula2().serializeTokens(out);
  108. }
  109. @Override
  110. protected int getDataSize() {
  111. return 6 + getFormattingBlockSize() +
  112. getFormulaSize(getFormula1())+
  113. getFormulaSize(getFormula2());
  114. }
  115. @Override
  116. public CFRuleRecord copy() {
  117. return new CFRuleRecord(this);
  118. }
  119. @Override
  120. public HSSFRecordTypes getGenericRecordType() {
  121. return HSSFRecordTypes.CF_RULE;
  122. }
  123. }