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.

OldFormulaRecord.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.ss.formula.Formula;
  19. import org.apache.poi.ss.formula.ptg.Ptg;
  20. import org.apache.poi.ss.usermodel.CellType;
  21. import org.apache.poi.util.GenericRecordUtil;
  22. /**
  23. * Formula Record (0x0006 / 0x0206 / 0x0406) - holds a formula in
  24. * encoded form, along with the value if a number
  25. */
  26. public final class OldFormulaRecord extends OldCellRecord {
  27. public static final short biff2_sid = 0x0006;
  28. public static final short biff3_sid = 0x0206;
  29. public static final short biff4_sid = 0x0406;
  30. public static final short biff5_sid = 0x0006;
  31. private FormulaSpecialCachedValue specialCachedValue;
  32. private double field_4_value;
  33. private short field_5_options;
  34. private Formula field_6_parsed_expr;
  35. public OldFormulaRecord(RecordInputStream ris) {
  36. super(ris, ris.getSid() == biff2_sid);
  37. if (isBiff2()) {
  38. field_4_value = ris.readDouble();
  39. } else {
  40. long valueLongBits = ris.readLong();
  41. specialCachedValue = FormulaSpecialCachedValue.create(valueLongBits);
  42. if (specialCachedValue == null) {
  43. field_4_value = Double.longBitsToDouble(valueLongBits);
  44. }
  45. }
  46. if (isBiff2()) {
  47. field_5_options = (short)ris.readUByte();
  48. } else {
  49. field_5_options = ris.readShort();
  50. }
  51. int expression_len = ris.readShort();
  52. int nBytesAvailable = ris.available();
  53. field_6_parsed_expr = Formula.read(expression_len, ris, nBytesAvailable);
  54. }
  55. /**
  56. * @deprecated POI 5.0.0, will be removed in 5.0, use getCachedResultTypeEnum until switch to enum is fully done
  57. */
  58. @Deprecated
  59. public int getCachedResultType() {
  60. if (specialCachedValue == null) {
  61. return CellType.NUMERIC.getCode();
  62. }
  63. return specialCachedValue.getValueType();
  64. }
  65. /**
  66. * Returns the type of the cached result
  67. * @return A CellType
  68. * @since POI 5.0.0
  69. */
  70. public CellType getCachedResultTypeEnum() {
  71. if (specialCachedValue == null) {
  72. return CellType.NUMERIC;
  73. }
  74. return specialCachedValue.getValueTypeEnum();
  75. }
  76. public boolean getCachedBooleanValue() {
  77. return specialCachedValue.getBooleanValue();
  78. }
  79. public int getCachedErrorValue() {
  80. return specialCachedValue.getErrorValue();
  81. }
  82. /**
  83. * get the calculated value of the formula
  84. *
  85. * @return calculated value
  86. */
  87. public double getValue() {
  88. return field_4_value;
  89. }
  90. /**
  91. * get the option flags
  92. *
  93. * @return bitmask
  94. */
  95. public short getOptions() {
  96. return field_5_options;
  97. }
  98. /**
  99. * @return the formula tokens. never <code>null</code>
  100. */
  101. public Ptg[] getParsedExpression() {
  102. return field_6_parsed_expr.getTokens();
  103. }
  104. public Formula getFormula() {
  105. return field_6_parsed_expr;
  106. }
  107. @Override
  108. public HSSFRecordTypes getGenericRecordType() {
  109. return HSSFRecordTypes.FORMULA;
  110. }
  111. @Override
  112. public Map<String, Supplier<?>> getGenericProperties() {
  113. return GenericRecordUtil.getGenericProperties(
  114. "base", super::getGenericProperties,
  115. "options", this::getOptions,
  116. "formula", this::getFormula,
  117. "value", this::getValue
  118. );
  119. }
  120. }