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.

SharedFormula.java 4.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.formula;
  16. import org.apache.poi.ss.SpreadsheetVersion;
  17. /**
  18. * Encapsulates logic to convert shared formulaa into non shared equivalent
  19. */
  20. public class SharedFormula {
  21. private final int _columnWrappingMask;
  22. private final int _rowWrappingMask;
  23. public SharedFormula(SpreadsheetVersion ssVersion){
  24. _columnWrappingMask = ssVersion.getLastColumnIndex(); //"IV" for .xls and "XFD" for .xlsx
  25. _rowWrappingMask = ssVersion.getLastRowIndex();
  26. }
  27. /**
  28. * Creates a non shared formula from the shared formula counterpart, i.e.
  29. * Converts the shared formula into the equivalent {@link Ptg} array that it would have,
  30. * were it not shared.
  31. *
  32. * @param ptgs parsed tokens of the shared formula
  33. * @param formulaRow
  34. * @param formulaColumn
  35. */
  36. public Ptg[] convertSharedFormulas(Ptg[] ptgs, int formulaRow, int formulaColumn) {
  37. Ptg[] newPtgStack = new Ptg[ptgs.length];
  38. for (int k = 0; k < ptgs.length; k++) {
  39. Ptg ptg = ptgs[k];
  40. byte originalOperandClass = -1;
  41. if (!ptg.isBaseToken()) {
  42. originalOperandClass = ptg.getPtgClass();
  43. }
  44. if (ptg instanceof RefPtgBase) {
  45. RefPtgBase refNPtg = (RefPtgBase)ptg;
  46. ptg = new RefPtg(fixupRelativeRow(formulaRow,refNPtg.getRow(),refNPtg.isRowRelative()),
  47. fixupRelativeColumn(formulaColumn,refNPtg.getColumn(),refNPtg.isColRelative()),
  48. refNPtg.isRowRelative(),
  49. refNPtg.isColRelative());
  50. ptg.setClass(originalOperandClass);
  51. } else if (ptg instanceof AreaPtgBase) {
  52. AreaPtgBase areaNPtg = (AreaPtgBase)ptg;
  53. ptg = new AreaPtg(fixupRelativeRow(formulaRow,areaNPtg.getFirstRow(),areaNPtg.isFirstRowRelative()),
  54. fixupRelativeRow(formulaRow,areaNPtg.getLastRow(),areaNPtg.isLastRowRelative()),
  55. fixupRelativeColumn(formulaColumn,areaNPtg.getFirstColumn(),areaNPtg.isFirstColRelative()),
  56. fixupRelativeColumn(formulaColumn,areaNPtg.getLastColumn(),areaNPtg.isLastColRelative()),
  57. areaNPtg.isFirstRowRelative(),
  58. areaNPtg.isLastRowRelative(),
  59. areaNPtg.isFirstColRelative(),
  60. areaNPtg.isLastColRelative());
  61. ptg.setClass(originalOperandClass);
  62. } else if (ptg instanceof OperandPtg) {
  63. // Any subclass of OperandPtg is mutable, so it's safest to not share these instances.
  64. ptg = ((OperandPtg) ptg).copy();
  65. } else {
  66. // all other Ptgs are immutable and can be shared
  67. }
  68. newPtgStack[k] = ptg;
  69. }
  70. return newPtgStack;
  71. }
  72. private int fixupRelativeColumn(int currentcolumn, int column, boolean relative) {
  73. if(relative) {
  74. // mask out upper bits to produce 'wrapping' at the maximum column ("IV" for .xls and "XFD" for .xlsx)
  75. return (column + currentcolumn) & _columnWrappingMask;
  76. }
  77. return column;
  78. }
  79. private int fixupRelativeRow(int currentrow, int row, boolean relative) {
  80. if(relative) {
  81. return (row+currentrow) & _rowWrappingMask;
  82. }
  83. return row;
  84. }
  85. }