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.

ArrayRecord.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.formula.Formula;
  20. import org.apache.poi.ss.formula.ptg.Ptg;
  21. import org.apache.poi.util.GenericRecordUtil;
  22. import org.apache.poi.util.LittleEndianOutput;
  23. /**
  24. * ARRAY (0x0221)<p>
  25. *
  26. * Treated in a similar way to SharedFormulaRecord
  27. */
  28. public final class ArrayRecord extends SharedValueRecordBase {
  29. public static final short sid = 0x0221;
  30. private static final int OPT_ALWAYS_RECALCULATE = 0x0001;
  31. private static final int OPT_CALCULATE_ON_OPEN = 0x0002;
  32. private int _options;
  33. private int _field3notUsed;
  34. private Formula _formula;
  35. public ArrayRecord(ArrayRecord other) {
  36. super(other);
  37. _options = other._options;
  38. _field3notUsed = other._field3notUsed;
  39. _formula = (other._formula == null) ? null : other._formula.copy();
  40. }
  41. public ArrayRecord(RecordInputStream in) {
  42. super(in);
  43. _options = in.readUShort();
  44. _field3notUsed = in.readInt();
  45. int formulaTokenLen = in.readUShort();
  46. int totalFormulaLen = in.available();
  47. _formula = Formula.read(formulaTokenLen, in, totalFormulaLen);
  48. }
  49. public ArrayRecord(Formula formula, CellRangeAddress8Bit range) {
  50. super(range);
  51. _options = 0; //YK: Excel 2007 leaves this field unset
  52. _field3notUsed = 0;
  53. _formula = formula;
  54. }
  55. public boolean isAlwaysRecalculate() {
  56. return (_options & OPT_ALWAYS_RECALCULATE) != 0;
  57. }
  58. public boolean isCalculateOnOpen() {
  59. return (_options & OPT_CALCULATE_ON_OPEN) != 0;
  60. }
  61. public Ptg[] getFormulaTokens() {
  62. return _formula.getTokens();
  63. }
  64. protected int getExtraDataSize() {
  65. return 2 + 4 + _formula.getEncodedSize();
  66. }
  67. protected void serializeExtraData(LittleEndianOutput out) {
  68. out.writeShort(_options);
  69. out.writeInt(_field3notUsed);
  70. _formula.serialize(out);
  71. }
  72. public short getSid() {
  73. return sid;
  74. }
  75. @Override
  76. public ArrayRecord copy() {
  77. return new ArrayRecord(this);
  78. }
  79. @Override
  80. public HSSFRecordTypes getGenericRecordType() {
  81. return HSSFRecordTypes.ARRAY;
  82. }
  83. @Override
  84. public Map<String, Supplier<?>> getGenericProperties() {
  85. return GenericRecordUtil.getGenericProperties(
  86. "range", this::getRange,
  87. "options", () -> _options,
  88. "notUsed", () -> _field3notUsed,
  89. "formula", () -> _formula
  90. );
  91. }
  92. }