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.

SharedFormulaRecord.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.hssf.util.CellRangeAddress8Bit;
  19. import org.apache.poi.ss.SpreadsheetVersion;
  20. import org.apache.poi.ss.formula.Formula;
  21. import org.apache.poi.ss.formula.SharedFormula;
  22. import org.apache.poi.ss.formula.ptg.Ptg;
  23. import org.apache.poi.util.GenericRecordUtil;
  24. import org.apache.poi.util.LittleEndianOutput;
  25. /**
  26. * Primarily used as an excel optimization so that multiple similar formulas are not
  27. * written out too many times. We should recognize this record and serialize as is
  28. * since this is used when reading templates.
  29. * <p>
  30. * Note: the documentation says that the SID is BC where biffviewer reports 4BC. The hex dump shows
  31. * that the two byte sid representation to be 'BC 04' that is consistent with the other high byte
  32. * record types.
  33. */
  34. public final class SharedFormulaRecord extends SharedValueRecordBase {
  35. public static final short sid = 0x04BC;
  36. private int field_5_reserved;
  37. private Formula field_7_parsed_expr;
  38. // for testing only
  39. public SharedFormulaRecord() {
  40. this(new CellRangeAddress8Bit(0,0,0,0));
  41. }
  42. public SharedFormulaRecord(SharedFormulaRecord other) {
  43. super(other);
  44. field_5_reserved = other.field_5_reserved;
  45. field_7_parsed_expr = (other.field_7_parsed_expr == null) ? null : other.field_7_parsed_expr.copy();
  46. }
  47. private SharedFormulaRecord(CellRangeAddress8Bit range) {
  48. super(range);
  49. field_7_parsed_expr = Formula.create(Ptg.EMPTY_PTG_ARRAY);
  50. }
  51. /**
  52. * @param in the RecordInputstream to read the record from
  53. */
  54. public SharedFormulaRecord(RecordInputStream in) {
  55. super(in);
  56. field_5_reserved = in.readShort();
  57. int field_6_expression_len = in.readShort();
  58. int nAvailableBytes = in.available();
  59. field_7_parsed_expr = Formula.read(field_6_expression_len, in, nAvailableBytes);
  60. }
  61. protected void serializeExtraData(LittleEndianOutput out) {
  62. out.writeShort(field_5_reserved);
  63. field_7_parsed_expr.serialize(out);
  64. }
  65. protected int getExtraDataSize() {
  66. return 2 + field_7_parsed_expr.getEncodedSize();
  67. }
  68. public short getSid() {
  69. return sid;
  70. }
  71. /**
  72. * Convert formula into an array of {@link Ptg} tokens.
  73. *
  74. * @param formula The record to break into tokens, cannot be null
  75. *
  76. * @return the equivalent {@link Ptg} array that the formula would have, were it not shared.
  77. */
  78. public Ptg[] getFormulaTokens(FormulaRecord formula) {
  79. int formulaRow = formula.getRow();
  80. int formulaColumn = formula.getColumn();
  81. //Sanity checks
  82. if (!isInRange(formulaRow, formulaColumn)) {
  83. throw new RuntimeException("Shared Formula Conversion: Coding Error");
  84. }
  85. SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL97);
  86. return sf.convertSharedFormulas(field_7_parsed_expr.getTokens(), formulaRow, formulaColumn);
  87. }
  88. @Override
  89. public SharedFormulaRecord copy() {
  90. return new SharedFormulaRecord(this);
  91. }
  92. public boolean isFormulaSame(SharedFormulaRecord other) {
  93. return field_7_parsed_expr.isSame(other.field_7_parsed_expr);
  94. }
  95. @Override
  96. public HSSFRecordTypes getGenericRecordType() {
  97. return HSSFRecordTypes.SHARED_FORMULA;
  98. }
  99. @Override
  100. public Map<String, Supplier<?>> getGenericProperties() {
  101. return GenericRecordUtil.getGenericProperties(
  102. "range", this::getRange,
  103. "reserved", () -> field_5_reserved,
  104. "formula", () -> field_7_parsed_expr
  105. );
  106. }
  107. }