Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ColumnShifter.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 java.util.ArrayList;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import org.apache.poi.ss.usermodel.Row;
  21. import org.apache.poi.ss.usermodel.Sheet;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.apache.poi.util.Beta;
  24. /**
  25. * Helper for shifting columns up or down
  26. *
  27. * @since POI 4.0.0
  28. */
  29. // non-Javadoc: This abstract class exists to consolidate duplicated code between XSSFColumnShifter and HSSFColumnShifter
  30. // (currently methods sprinkled throughout HSSFSheet)
  31. @Beta
  32. public abstract class ColumnShifter extends BaseRowColShifter {
  33. protected final Sheet sheet;
  34. public ColumnShifter(Sheet sh) {
  35. sheet = sh;
  36. }
  37. /**
  38. * Shifts, grows, or shrinks the merged regions due to a column shift.
  39. * Merged regions that are completely overlaid by shifting will be deleted.
  40. *
  41. * @param startColumn the column to start shifting
  42. * @param endColumn the column to end shifting
  43. * @param n the number of columns to shift
  44. * @return an array of affected merged regions, doesn't contain deleted ones
  45. * @since POI 4.0.0
  46. */
  47. // Keep this code in sync with {@link RowShifter#shiftMergedRegions}
  48. @Override
  49. public List<CellRangeAddress> shiftMergedRegions(int startColumn, int endColumn, int n) {
  50. List<CellRangeAddress> shiftedRegions = new ArrayList<>();
  51. Set<Integer> removedIndices = new HashSet<>();
  52. //move merged regions completely if they fall within the new region boundaries when they are shifted
  53. int size = sheet.getNumMergedRegions();
  54. for (int i = 0; i < size; i++) {
  55. CellRangeAddress merged = sheet.getMergedRegion(i);
  56. // remove merged region that are replaced by the shifting,
  57. // i.e. where the area includes something in the overwritten area
  58. if(removalNeeded(merged, startColumn, endColumn, n)) {
  59. removedIndices.add(i);
  60. continue;
  61. }
  62. boolean inStart = (merged.getFirstColumn() >= startColumn || merged.getLastColumn() >= startColumn);
  63. boolean inEnd = (merged.getFirstColumn() <= endColumn || merged.getLastColumn() <= endColumn);
  64. //don't check if it's not within the shifted area
  65. if (!inStart || !inEnd) {
  66. continue;
  67. }
  68. //only shift if the region outside the shifted columns is not merged too
  69. if (!merged.containsColumn(startColumn - 1) && !merged.containsColumn(endColumn + 1)) {
  70. merged.setFirstColumn(merged.getFirstColumn() + n);
  71. merged.setLastColumn(merged.getLastColumn() + n);
  72. //have to remove/add it back
  73. shiftedRegions.add(merged);
  74. removedIndices.add(i);
  75. }
  76. }
  77. if(!removedIndices.isEmpty()) {
  78. sheet.removeMergedRegions(removedIndices);
  79. }
  80. //read so it doesn't get shifted again
  81. for (CellRangeAddress region : shiftedRegions) {
  82. sheet.addMergedRegion(region);
  83. }
  84. return shiftedRegions;
  85. }
  86. // Keep in sync with {@link RowShifter#removalNeeded}
  87. private boolean removalNeeded(CellRangeAddress merged, int startColumn, int endColumn, int n) {
  88. final int movedColumns = endColumn - startColumn + 1;
  89. // build a range of the columns that are overwritten, i.e. the target-area, but without
  90. // columns that are moved along
  91. final CellRangeAddress overwrite;
  92. if(n > 0) {
  93. // area is moved down => overwritten area is [endColumn + n - movedColumns, endColumn + n]
  94. final int firstCol = Math.max(endColumn + 1, endColumn + n - movedColumns);
  95. final int lastCol = endColumn + n;
  96. overwrite = new CellRangeAddress(0, 0, firstCol, lastCol);
  97. } else {
  98. // area is moved up => overwritten area is [startColumn + n, startColumn + n + movedColumns]
  99. final int firstCol = startColumn + n;
  100. final int lastCol = Math.min(startColumn - 1, startColumn + n + movedColumns);
  101. overwrite = new CellRangeAddress(0, 0, firstCol, lastCol);
  102. }
  103. // if the merged-region and the overwritten area intersect, we need to remove it
  104. return merged.intersects(overwrite);
  105. }
  106. public void shiftColumns(int firstShiftColumnIndex, int lastShiftColumnIndex, int step){
  107. if(step > 0){
  108. for (Row row : sheet)
  109. if(row != null)
  110. row.shiftCellsRight(firstShiftColumnIndex, lastShiftColumnIndex, step);
  111. }
  112. else if(step < 0){
  113. for (Row row : sheet)
  114. if(row != null)
  115. row.shiftCellsLeft(firstShiftColumnIndex, lastShiftColumnIndex, -step);
  116. }
  117. //else step == 0 => nothing to shift
  118. }
  119. }