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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.util.HexDump;
  20. import org.apache.poi.util.LittleEndianOutput;
  21. /**
  22. * Title: SHAREDFMLA (0x04BC) SharedFormulaRecord
  23. * Description: Primarily used as an excel optimization so that multiple similar formulas
  24. * are not written out too many times. We should recognize this record and
  25. * serialize as is since this is used when reading templates.
  26. * <p>
  27. * Note: the documentation says that the SID is BC where biffviewer reports 4BC. The hex dump shows
  28. * that the two byte sid representation to be 'BC 04' that is consistent with the other high byte
  29. * record types.
  30. * @author Danny Mui at apache dot org
  31. */
  32. public final class SharedFormulaRecord extends SharedValueRecordBase {
  33. public final static short sid = 0x04BC;
  34. private int field_5_reserved;
  35. private Formula field_7_parsed_expr;
  36. // for testing only
  37. public SharedFormulaRecord() {
  38. this(new CellRangeAddress8Bit(0,0,0,0));
  39. }
  40. private SharedFormulaRecord(CellRangeAddress8Bit range) {
  41. super(range);
  42. field_7_parsed_expr = Formula.create(Ptg.EMPTY_PTG_ARRAY);
  43. }
  44. /**
  45. * @param in the RecordInputstream to read the record from
  46. */
  47. public SharedFormulaRecord(RecordInputStream in) {
  48. super(in);
  49. field_5_reserved = in.readShort();
  50. int field_6_expression_len = in.readShort();
  51. int nAvailableBytes = in.available();
  52. field_7_parsed_expr = Formula.read(field_6_expression_len, in, nAvailableBytes);
  53. }
  54. protected void serializeExtraData(LittleEndianOutput out) {
  55. out.writeShort(field_5_reserved);
  56. field_7_parsed_expr.serialize(out);
  57. }
  58. protected int getExtraDataSize() {
  59. return 2 + field_7_parsed_expr.getEncodedSize();
  60. }
  61. /**
  62. * print a sort of string representation ([SHARED FORMULA RECORD] id = x [/SHARED FORMULA RECORD])
  63. */
  64. public String toString()
  65. {
  66. StringBuffer buffer = new StringBuffer();
  67. buffer.append("[SHARED FORMULA (").append(HexDump.intToHex(sid)).append("]\n");
  68. buffer.append(" .range = ").append(getRange().toString()).append("\n");
  69. buffer.append(" .reserved = ").append(HexDump.shortToHex(field_5_reserved)).append("\n");
  70. Ptg[] ptgs = field_7_parsed_expr.getTokens();
  71. for (int k = 0; k < ptgs.length; k++ ) {
  72. buffer.append("Formula[").append(k).append("]");
  73. Ptg ptg = ptgs[k];
  74. buffer.append(ptg.toString()).append(ptg.getRVAType()).append("\n");
  75. }
  76. buffer.append("[/SHARED FORMULA]\n");
  77. return buffer.toString();
  78. }
  79. public short getSid() {
  80. return sid;
  81. }
  82. /**
  83. * Creates a non shared formula from the shared formula counterpart<br/>
  84. *
  85. * Perhaps this functionality could be implemented in terms of the raw
  86. * byte array inside {@link Formula}.
  87. */
  88. public static Ptg[] convertSharedFormulas(Ptg[] ptgs, int formulaRow, int formulaColumn) {
  89. Ptg[] newPtgStack = new Ptg[ptgs.length];
  90. for (int k = 0; k < ptgs.length; k++) {
  91. Ptg ptg = ptgs[k];
  92. byte originalOperandClass = -1;
  93. if (!ptg.isBaseToken()) {
  94. originalOperandClass = ptg.getPtgClass();
  95. }
  96. if (ptg instanceof RefPtgBase) {
  97. RefPtgBase refNPtg = (RefPtgBase)ptg;
  98. ptg = new RefPtg(fixupRelativeRow(formulaRow,refNPtg.getRow(),refNPtg.isRowRelative()),
  99. fixupRelativeColumn(formulaColumn,refNPtg.getColumn(),refNPtg.isColRelative()),
  100. refNPtg.isRowRelative(),
  101. refNPtg.isColRelative());
  102. } else if (ptg instanceof AreaPtgBase) {
  103. AreaPtgBase areaNPtg = (AreaPtgBase)ptg;
  104. ptg = new AreaPtg(fixupRelativeRow(formulaRow,areaNPtg.getFirstRow(),areaNPtg.isFirstRowRelative()),
  105. fixupRelativeRow(formulaRow,areaNPtg.getLastRow(),areaNPtg.isLastRowRelative()),
  106. fixupRelativeColumn(formulaColumn,areaNPtg.getFirstColumn(),areaNPtg.isFirstColRelative()),
  107. fixupRelativeColumn(formulaColumn,areaNPtg.getLastColumn(),areaNPtg.isLastColRelative()),
  108. areaNPtg.isFirstRowRelative(),
  109. areaNPtg.isLastRowRelative(),
  110. areaNPtg.isFirstColRelative(),
  111. areaNPtg.isLastColRelative());
  112. } else {
  113. if (false) {// do we need a ptg clone here?
  114. ptg = ptg.copy();
  115. }
  116. }
  117. if (!ptg.isBaseToken()) {
  118. ptg.setClass(originalOperandClass);
  119. }
  120. newPtgStack[k] = ptg;
  121. }
  122. return newPtgStack;
  123. }
  124. /**
  125. * @return the equivalent {@link Ptg} array that the formula would have, were it not shared.
  126. */
  127. public Ptg[] getFormulaTokens(FormulaRecord formula) {
  128. int formulaRow = formula.getRow();
  129. int formulaColumn = formula.getColumn();
  130. //Sanity checks
  131. if (!isInRange(formulaRow, formulaColumn)) {
  132. throw new RuntimeException("Shared Formula Conversion: Coding Error");
  133. }
  134. return convertSharedFormulas(field_7_parsed_expr.getTokens(), formulaRow, formulaColumn);
  135. }
  136. private static int fixupRelativeColumn(int currentcolumn, int column, boolean relative) {
  137. if(relative) {
  138. // mask out upper bits to produce 'wrapping' at column 256 ("IV")
  139. return (column + currentcolumn) & 0x00FF;
  140. }
  141. return column;
  142. }
  143. private static int fixupRelativeRow(int currentrow, int row, boolean relative) {
  144. if(relative) {
  145. // mask out upper bits to produce 'wrapping' at row 65536
  146. return (row+currentrow) & 0x00FFFF;
  147. }
  148. return row;
  149. }
  150. public Object clone() {
  151. SharedFormulaRecord result = new SharedFormulaRecord(getRange());
  152. result.field_5_reserved = field_5_reserved;
  153. result.field_7_parsed_expr = field_7_parsed_expr.copy();
  154. return result;
  155. }
  156. public boolean isFormulaSame(SharedFormulaRecord other) {
  157. return field_7_parsed_expr.isSame(other.field_7_parsed_expr);
  158. }
  159. }