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.

BaseRowColShifter.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.ss.usermodel.helpers;
  16. import org.apache.poi.ss.formula.FormulaShifter;
  17. import org.apache.poi.ss.formula.ptg.AreaErrPtg;
  18. import org.apache.poi.ss.formula.ptg.AreaPtg;
  19. import org.apache.poi.ss.formula.ptg.Ptg;
  20. import org.apache.poi.ss.util.CellRangeAddress;
  21. import org.apache.poi.util.Internal;
  22. import java.util.List;
  23. /**
  24. * Class for code common to {@link RowShifter} and {@link ColumnShifter}
  25. * Helper for shifting rows up or down and columns left and right
  26. *
  27. * @since POI 4.0.0
  28. */
  29. @Internal
  30. public abstract class BaseRowColShifter {
  31. /**
  32. * Update named ranges
  33. */
  34. public abstract void updateNamedRanges(FormulaShifter formulaShifter);
  35. /**
  36. * Update formulas.
  37. */
  38. public abstract void updateFormulas(FormulaShifter formulaShifter);
  39. /**
  40. * Shifts, grows, or shrinks the merged regions due to a row shift
  41. * ({@link RowShifter}) or column shift ({@link ColumnShifter}).
  42. * Merged regions that are completely overlaid by shifting will be deleted.
  43. *
  44. * @param start the first row or column to be shifted
  45. * @param end the last row or column to be shifted
  46. * @param n the number of rows or columns to shift
  47. * @return a list of affected merged regions, excluding contain deleted ones
  48. */
  49. public abstract List<CellRangeAddress> shiftMergedRegions(int start, int end, int n);
  50. /**
  51. * Update conditional formatting
  52. * @param formulaShifter The {@link FormulaShifter} to use
  53. */
  54. public abstract void updateConditionalFormatting(FormulaShifter formulaShifter);
  55. /**
  56. * Shift the Hyperlink anchors (not the hyperlink text, even if the hyperlink
  57. * is of type LINK_DOCUMENT and refers to a cell that was shifted). Hyperlinks
  58. * do not track the content they point to.
  59. *
  60. * @param formulaShifter the formula shifting policy
  61. */
  62. public abstract void updateHyperlinks(FormulaShifter formulaShifter);
  63. public static CellRangeAddress shiftRange(FormulaShifter formulaShifter, CellRangeAddress cra, int currentExternSheetIx) {
  64. // FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here
  65. AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false);
  66. Ptg[] ptgs = { aptg, };
  67. if (!formulaShifter.adjustFormula(ptgs, currentExternSheetIx)) {
  68. return cra;
  69. }
  70. Ptg ptg0 = ptgs[0];
  71. if (ptg0 instanceof AreaPtg) {
  72. AreaPtg bptg = (AreaPtg) ptg0;
  73. return new CellRangeAddress(bptg.getFirstRow(), bptg.getLastRow(), bptg.getFirstColumn(), bptg.getLastColumn());
  74. }
  75. if (ptg0 instanceof AreaErrPtg) {
  76. return null;
  77. }
  78. throw new IllegalStateException("Unexpected shifted ptg class (" + ptg0.getClass().getName() + ")");
  79. }
  80. }