您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SharedFormulaRecord.java 4.7KB

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