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.

XSSFRow.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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.xssf.usermodel;
  16. import java.util.HashSet;
  17. import java.util.IdentityHashMap;
  18. import java.util.Iterator;
  19. import java.util.Objects;
  20. import java.util.Set;
  21. import java.util.TreeMap;
  22. import org.apache.poi.ss.SpreadsheetVersion;
  23. import org.apache.poi.ss.formula.FormulaShifter;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.CellCopyContext;
  26. import org.apache.poi.ss.usermodel.CellCopyPolicy;
  27. import org.apache.poi.ss.usermodel.CellStyle;
  28. import org.apache.poi.ss.usermodel.CellType;
  29. import org.apache.poi.ss.usermodel.FormulaError;
  30. import org.apache.poi.ss.usermodel.Row;
  31. import org.apache.poi.ss.usermodel.helpers.RowShifter;
  32. import org.apache.poi.ss.util.CellRangeAddress;
  33. import org.apache.poi.ss.util.CellUtil;
  34. import org.apache.poi.util.Beta;
  35. import org.apache.poi.util.Internal;
  36. import org.apache.poi.xssf.model.StylesTable;
  37. import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter;
  38. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
  39. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
  40. /**
  41. * High level representation of a row of a spreadsheet.
  42. */
  43. public class XSSFRow implements Row, Comparable<XSSFRow> {
  44. /**
  45. * the xml bean containing all cell definitions for this row
  46. */
  47. private final CTRow _row;
  48. /**
  49. * Cells of this row keyed by their column indexes.
  50. * The TreeMap ensures that the cells are ordered by columnIndex in the ascending order.
  51. */
  52. private final TreeMap<Integer, XSSFCell> _cells;
  53. /**
  54. * the parent sheet
  55. */
  56. private final XSSFSheet _sheet;
  57. /**
  58. * Construct a XSSFRow.
  59. *
  60. * @param row the xml bean containing all cell definitions for this row.
  61. * @param sheet the parent sheet.
  62. */
  63. protected XSSFRow(CTRow row, XSSFSheet sheet) {
  64. _row = row;
  65. _sheet = sheet;
  66. _cells = new TreeMap<>();
  67. for (CTCell c : row.getCArray()) {
  68. XSSFCell cell = new XSSFCell(this, c);
  69. // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  70. final Integer colI = Integer.valueOf(cell.getColumnIndex()); // NOSONAR
  71. _cells.put(colI, cell);
  72. sheet.onReadCell(cell);
  73. }
  74. if (! row.isSetR()) {
  75. // Certain file format writers skip the row number
  76. // Assume no gaps, and give this the next row number
  77. int nextRowNum = sheet.getLastRowNum()+2;
  78. if (nextRowNum == 2 && sheet.getPhysicalNumberOfRows() == 0) {
  79. nextRowNum = 1;
  80. }
  81. row.setR(nextRowNum);
  82. }
  83. }
  84. /**
  85. * Returns the XSSFSheet this row belongs to
  86. *
  87. * @return the XSSFSheet that owns this row
  88. */
  89. @Override
  90. public XSSFSheet getSheet() {
  91. return this._sheet;
  92. }
  93. /**
  94. * Cell iterator over the physically defined cells:
  95. * <pre>{@code
  96. * for (Iterator<Cell> it = row.cellIterator(); it.hasNext(); ) {
  97. * Cell cell = it.next();
  98. * ...
  99. * }
  100. * }</pre>
  101. *
  102. * @return an iterator over cells in this row.
  103. */
  104. @Override
  105. @SuppressWarnings("unchecked")
  106. public Iterator<Cell> cellIterator() {
  107. return (Iterator<Cell>)(Iterator<? extends Cell>)_cells.values().iterator();
  108. }
  109. /**
  110. * Alias for {@link #cellIterator()} to allow foreach loops:
  111. * <blockquote><pre>
  112. * for(Cell cell : row){
  113. * ...
  114. * }
  115. * </pre></blockquote>
  116. *
  117. * @return an iterator over cells in this row.
  118. */
  119. @Override
  120. public Iterator<Cell> iterator() {
  121. return cellIterator();
  122. }
  123. /**
  124. * Compares two {@code XSSFRow} objects. Two rows are equal if they belong to the same worksheet and
  125. * their row indexes are equal.
  126. *
  127. * @param other the {@code XSSFRow} to be compared.
  128. * @return <ul>
  129. * <li>
  130. * the value {@code 0} if the row number of this {@code XSSFRow} is
  131. * equal to the row number of the argument {@code XSSFRow}
  132. * </li>
  133. * <li>
  134. * a value less than {@code 0} if the row number of this this {@code XSSFRow} is
  135. * numerically less than the row number of the argument {@code XSSFRow}
  136. * </li>
  137. * <li>
  138. * a value greater than {@code 0} if the row number of this this {@code XSSFRow} is
  139. * numerically greater than the row number of the argument {@code XSSFRow}
  140. * </li>
  141. * </ul>
  142. * @throws IllegalArgumentException if the argument row belongs to a different worksheet
  143. */
  144. @Override
  145. public int compareTo(XSSFRow other) {
  146. if (this.getSheet() != other.getSheet()) {
  147. throw new IllegalArgumentException("The compared rows must belong to the same sheet");
  148. }
  149. int thisRow = this.getRowNum();
  150. int otherRow = other.getRowNum();
  151. return Integer.compare(thisRow, otherRow);
  152. }
  153. @Override
  154. public boolean equals(Object obj)
  155. {
  156. if (!(obj instanceof XSSFRow))
  157. {
  158. return false;
  159. }
  160. XSSFRow other = (XSSFRow) obj;
  161. return (this.getRowNum() == other.getRowNum()) &&
  162. (this.getSheet() == other.getSheet());
  163. }
  164. @Override
  165. public int hashCode() {
  166. return _row.hashCode();
  167. }
  168. /**
  169. * Use this to create new cells within the row and return it.
  170. * <p>
  171. * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
  172. * either through calling {@code setCellValue} or {@code setCellType}.
  173. * </p>
  174. * @param columnIndex - the column number this cell represents
  175. * @return Cell a high level representation of the created cell.
  176. * @throws IllegalArgumentException if columnIndex &lt; 0 or greater than 16384,
  177. * the maximum number of columns supported by the SpreadsheetML format (.xlsx)
  178. */
  179. @Override
  180. public XSSFCell createCell(int columnIndex) {
  181. return createCell(columnIndex, CellType.BLANK);
  182. }
  183. /**
  184. * Use this to create new cells within the row and return it.
  185. *
  186. * @param columnIndex - the column number this cell represents
  187. * @param type - the cell's data type
  188. * @return XSSFCell a high level representation of the created cell.
  189. * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex &lt; 0
  190. * or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx)
  191. */
  192. @Override
  193. public XSSFCell createCell(int columnIndex, CellType type) {
  194. // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  195. final Integer colI = Integer.valueOf(columnIndex); // NOSONAR
  196. CTCell ctCell;
  197. XSSFCell prev = _cells.get(colI);
  198. if(prev != null){
  199. ctCell = prev.getCTCell();
  200. ctCell.set(CTCell.Factory.newInstance());
  201. } else {
  202. ctCell = _row.addNewC();
  203. }
  204. XSSFCell xcell = new XSSFCell(this, ctCell);
  205. try {
  206. xcell.setCellNum(columnIndex);
  207. } catch (IllegalArgumentException e) {
  208. // we need to undo adding the CTCell in _row if something fails here, e.g.
  209. // cell-limits are exceeded
  210. _row.removeC(_row.getCList().size()-1);
  211. throw e;
  212. }
  213. if (type != CellType.BLANK && type != CellType.FORMULA) {
  214. setDefaultValue(xcell, type);
  215. }
  216. _cells.put(colI, xcell);
  217. return xcell;
  218. }
  219. private static void setDefaultValue(XSSFCell cell, CellType type) {
  220. switch (type) {
  221. case NUMERIC:
  222. cell.setCellValue(0);
  223. break;
  224. case STRING:
  225. cell.setCellValue("");
  226. break;
  227. case BOOLEAN:
  228. cell.setCellValue(false);
  229. break;
  230. case ERROR:
  231. cell.setCellErrorValue(FormulaError._NO_ERROR);
  232. break;
  233. default:
  234. throw new AssertionError("Unknown cell-type specified: " + type);
  235. }
  236. }
  237. /**
  238. * Returns the cell at the given (0 based) index,
  239. * with the {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} from the parent Workbook.
  240. *
  241. * @return the cell at the given (0 based) index
  242. */
  243. @Override
  244. public XSSFCell getCell(int cellnum) {
  245. return getCell(cellnum, _sheet.getWorkbook().getMissingCellPolicy());
  246. }
  247. /**
  248. * Returns the cell at the given (0 based) index, with the specified {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy}
  249. *
  250. * @return the cell at the given (0 based) index
  251. * @throws IllegalArgumentException if cellnum &lt; 0 or the specified MissingCellPolicy is invalid
  252. */
  253. @Override
  254. public XSSFCell getCell(int cellnum, MissingCellPolicy policy) {
  255. if(cellnum < 0) {
  256. throw new IllegalArgumentException("Cell index must be >= 0");
  257. }
  258. // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  259. final Integer colI = Integer.valueOf(cellnum); // NOSONAR
  260. XSSFCell cell = _cells.get(colI);
  261. switch (policy) {
  262. case RETURN_NULL_AND_BLANK:
  263. return cell;
  264. case RETURN_BLANK_AS_NULL:
  265. boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK);
  266. return (isBlank) ? null : cell;
  267. case CREATE_NULL_AS_BLANK:
  268. return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell;
  269. default:
  270. throw new IllegalArgumentException("Illegal policy " + policy);
  271. }
  272. }
  273. /**
  274. * Get the 0-based number of the first cell contained in this row.
  275. *
  276. * @return short representing the first logical cell in the row,
  277. * or -1 if the row does not contain any cells.
  278. */
  279. @Override
  280. public short getFirstCellNum() {
  281. return (short)(_cells.size() == 0 ? -1 : _cells.firstKey());
  282. }
  283. /**
  284. * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also
  285. * happens to be the 1-based column number of the last cell. This value can be used as a
  286. * standard upper bound when iterating over cells:
  287. * <pre>
  288. * short minColIx = row.getFirstCellNum();
  289. * short maxColIx = row.getLastCellNum();
  290. * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
  291. * XSSFCell cell = row.getCell(colIx);
  292. * if(cell == null) {
  293. * continue;
  294. * }
  295. * //... do something with cell
  296. * }
  297. * </pre>
  298. *
  299. * @return short representing the last logical cell in the row <b>PLUS ONE</b>,
  300. * or -1 if the row does not contain any cells.
  301. */
  302. @Override
  303. public short getLastCellNum() {
  304. return (short)(_cells.size() == 0 ? -1 : (_cells.lastKey() + 1));
  305. }
  306. /**
  307. * Get the row's height measured in twips (1/20th of a point). If the height is not set, the default worksheet value is returned,
  308. * See {@link org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()}
  309. *
  310. * @return row height measured in twips (1/20th of a point)
  311. */
  312. @Override
  313. public short getHeight() {
  314. return (short)(getHeightInPoints()*20);
  315. }
  316. /**
  317. * Returns row height measured in point size. If the height is not set, the default worksheet value is returned,
  318. * See {@link org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()}
  319. *
  320. * @return row height measured in point size
  321. * @see org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()
  322. */
  323. @Override
  324. public float getHeightInPoints() {
  325. if (this._row.isSetHt()) {
  326. return (float) this._row.getHt();
  327. }
  328. return _sheet.getDefaultRowHeightInPoints();
  329. }
  330. /**
  331. * Set the height in "twips" or 1/20th of a point.
  332. *
  333. * @param height the height in "twips" or 1/20th of a point. {@code -1} resets to the default height
  334. */
  335. @Override
  336. public void setHeight(short height) {
  337. if (height == -1) {
  338. if (_row.isSetHt()) {
  339. _row.unsetHt();
  340. }
  341. if (_row.isSetCustomHeight()) {
  342. _row.unsetCustomHeight();
  343. }
  344. } else {
  345. _row.setHt((double) height / 20);
  346. _row.setCustomHeight(true);
  347. }
  348. }
  349. /**
  350. * Set the row's height in points.
  351. *
  352. * @param height the height in points. {@code -1} resets to the default height
  353. */
  354. @Override
  355. public void setHeightInPoints(float height) {
  356. setHeight((short)(height == -1 ? -1 : (height*20)));
  357. }
  358. /**
  359. * Gets the number of defined cells (NOT number of cells in the actual row!).
  360. * That is to say if only columns 0,4,5 have values then there would be 3.
  361. *
  362. * @return int representing the number of defined cells in the row.
  363. */
  364. @Override
  365. public int getPhysicalNumberOfCells() {
  366. return _cells.size();
  367. }
  368. /**
  369. * Get row number this row represents
  370. *
  371. * @return the row number (0 based)
  372. */
  373. @Override
  374. public int getRowNum() {
  375. return Math.toIntExact(_row.getR() - 1);
  376. }
  377. /**
  378. * Set the row number of this row.
  379. *
  380. * @param rowIndex the row number (0-based)
  381. * @throws IllegalArgumentException if rowNum &lt; 0 or greater than 1048575
  382. */
  383. @Override
  384. public void setRowNum(int rowIndex) {
  385. int maxrow = SpreadsheetVersion.EXCEL2007.getLastRowIndex();
  386. if (rowIndex < 0 || rowIndex > maxrow) {
  387. throw new IllegalArgumentException("Invalid row number (" + rowIndex
  388. + ") outside allowable range (0.." + maxrow + ")");
  389. }
  390. _row.setR(rowIndex + 1L);
  391. }
  392. /**
  393. * Get whether or not to display this row with 0 height
  394. *
  395. * @return - height is zero or not.
  396. */
  397. @Override
  398. public boolean getZeroHeight() {
  399. return this._row.getHidden();
  400. }
  401. /**
  402. * Set whether or not to display this row with 0 height
  403. *
  404. * @param height height is zero or not.
  405. */
  406. @Override
  407. public void setZeroHeight(boolean height) {
  408. this._row.setHidden(height);
  409. }
  410. /**
  411. * Is this row formatted? Most aren't, but some rows
  412. * do have whole-row styles. For those that do, you
  413. * can get the formatting from {@link #getRowStyle()}
  414. */
  415. @Override
  416. public boolean isFormatted() {
  417. return _row.isSetS();
  418. }
  419. /**
  420. * Returns the whole-row cell style. Most rows won't
  421. * have one of these, so will return null. Call
  422. * {@link #isFormatted()} to check first.
  423. */
  424. @Override
  425. public XSSFCellStyle getRowStyle() {
  426. if(!isFormatted()) {
  427. return null;
  428. }
  429. StylesTable stylesSource = getSheet().getWorkbook().getStylesSource();
  430. if(stylesSource.getNumCellStyles() > 0) {
  431. return stylesSource.getStyleAt(Math.toIntExact(_row.getS()));
  432. } else {
  433. return null;
  434. }
  435. }
  436. /**
  437. * Applies a whole-row cell styling to the row.
  438. * If the value is null then the style information is removed,
  439. * causing the cell to used the default workbook style.
  440. */
  441. @Override
  442. public void setRowStyle(CellStyle style) {
  443. if(style == null) {
  444. if(_row.isSetS()) {
  445. _row.unsetS();
  446. _row.unsetCustomFormat();
  447. }
  448. } else {
  449. StylesTable styleSource = getSheet().getWorkbook().getStylesSource();
  450. XSSFCellStyle xStyle = (XSSFCellStyle)style;
  451. xStyle.verifyBelongsToStylesSource(styleSource);
  452. long idx = styleSource.putStyle(xStyle);
  453. _row.setS(idx);
  454. _row.setCustomFormat(true);
  455. }
  456. }
  457. /**
  458. * Remove the Cell from this row.
  459. *
  460. * @param cell the cell to remove
  461. */
  462. @Override
  463. public void removeCell(Cell cell) {
  464. if (cell.getRow() != this) {
  465. throw new IllegalArgumentException("Specified cell does not belong to this row");
  466. }
  467. //noinspection SuspiciousMethodCalls
  468. if(!_cells.containsValue(cell)) {
  469. throw new IllegalArgumentException("the row does not contain this cell");
  470. }
  471. XSSFCell xcell = (XSSFCell)cell;
  472. if(xcell.isPartOfArrayFormulaGroup()) {
  473. xcell.setCellFormula(null); // to remove the array formula
  474. }
  475. if(cell.getCellType() == CellType.FORMULA) {
  476. _sheet.getWorkbook().onDeleteFormula(xcell);
  477. }
  478. // Performance optimization for bug 57840: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  479. final Integer colI = Integer.valueOf(cell.getColumnIndex()); // NOSONAR
  480. XSSFCell removed = _cells.remove(colI);
  481. // also remove the corresponding CTCell from the _row.cArray,
  482. // it may not be at the same position right now
  483. // thus search for it
  484. int i = 0;
  485. for (CTCell ctCell : _row.getCArray()) {
  486. if(ctCell == removed.getCTCell()) {
  487. _row.removeC(i);
  488. }
  489. i++;
  490. }
  491. }
  492. /**
  493. * Returns the underlying CTRow xml bean containing all cell definitions in this row
  494. *
  495. * @return the underlying CTRow xml bean
  496. */
  497. @Internal
  498. public CTRow getCTRow(){
  499. return _row;
  500. }
  501. /**
  502. * Fired when the document is written to an output stream.
  503. *
  504. * @see org.apache.poi.xssf.usermodel.XSSFSheet#write(java.io.OutputStream) ()
  505. */
  506. protected void onDocumentWrite() {
  507. // _row.cArray and _cells.getCTCell might be out of sync after adding/removing cells,
  508. // thus we need to re-order it here to make the resulting file correct
  509. // do a quick check if there is work to do to not incur the overhead if not necessary anyway
  510. CTCell[] cArrayOrig = _row.getCArray();
  511. if(cArrayOrig.length == _cells.size()) {
  512. boolean allEqual = true;
  513. Iterator<XSSFCell> it = _cells.values().iterator();
  514. for (CTCell ctCell : cArrayOrig) {
  515. XSSFCell cell = it.next();
  516. // we want to compare on identity here on purpose
  517. // as we want to ensure that both lists contain the
  518. // same documents, not copies!
  519. if (ctCell != cell.getCTCell()) {
  520. allEqual = false;
  521. break;
  522. }
  523. }
  524. // we did not find any difference, so we can skip the work
  525. if(allEqual) {
  526. return;
  527. }
  528. }
  529. fixupCTCells(cArrayOrig);
  530. }
  531. private void fixupCTCells(CTCell[] cArrayOrig) {
  532. // copy all values to 2nd array and a map for lookup of index
  533. CTCell[] cArrayCopy = new CTCell[cArrayOrig.length];
  534. IdentityHashMap<CTCell, Integer> map = new IdentityHashMap<>(_cells.size());
  535. int i = 0;
  536. for (CTCell ctCell : cArrayOrig) {
  537. cArrayCopy[i] = (CTCell) ctCell.copy();
  538. map.put(ctCell, i);
  539. i++;
  540. }
  541. // populate _row.cArray correctly
  542. i = 0;
  543. for (XSSFCell cell : _cells.values()) {
  544. // no need to change anything if position is correct
  545. Integer correctPosition = map.get(cell.getCTCell());
  546. Objects.requireNonNull(correctPosition, "Should find CTCell in _row");
  547. if(correctPosition != i) {
  548. // we need to re-populate this CTCell
  549. _row.setCArray(i, cArrayCopy[correctPosition]);
  550. cell.setCTCell(_row.getCArray(i));
  551. }
  552. i++;
  553. }
  554. // remove any remaining illegal references in _rows.cArray
  555. while(cArrayOrig.length > _cells.size()) {
  556. _row.removeC(_cells.size());
  557. }
  558. }
  559. /**
  560. * @return formatted xml representation of this row
  561. */
  562. @Override
  563. public String toString(){
  564. return _row.toString();
  565. }
  566. /**
  567. * update cell references when shifting rows
  568. *
  569. * @param n the number of rows to move
  570. */
  571. protected void shift(int n) {
  572. int rownum = getRowNum() + n;
  573. String msg = "Row[rownum=" + getRowNum() + "] contains cell(s) included in a multi-cell array formula. " +
  574. "You cannot change part of an array.";
  575. setRowNum(rownum);
  576. for(Cell c : this){
  577. ((XSSFCell)c).updateCellReferencesForShifting(msg);
  578. }
  579. }
  580. /**
  581. * Copy the cells from srcRow to this row.
  582. * If this row is not a blank row, this will merge the two rows, overwriting
  583. * the cells in this row with the cells in srcRow.
  584. * If srcRow is null, overwrite cells in destination row with blank values, styles, etc per cell copy policy.
  585. *
  586. * Note that if you are copying from a non-XSSF row then you will need to disable style copying
  587. * in the {@link CellCopyPolicy} (XSSF styles are not compatible with HSSF styles, for instance).
  588. *
  589. * @param srcRow the rows to copy from
  590. * @param policy the policy to determine what gets copied
  591. */
  592. @Beta
  593. public void copyRowFrom(Row srcRow, CellCopyPolicy policy) {
  594. copyRowFrom(srcRow, policy, null);
  595. }
  596. /**
  597. * Copy the cells from srcRow to this row
  598. * If this row is not a blank row, this will merge the two rows, overwriting
  599. * the cells in this row with the cells in srcRow
  600. * If srcRow is null, overwrite cells in destination row with blank values, styles, etc per cell copy policy
  601. * srcRow may be from a different sheet in the same workbook
  602. *
  603. * Note that if you are copying from a non-XSSF row then you will need to disable style copying
  604. * in the {@link CellCopyPolicy} (XSSF styles are not compatible with HSSF styles, for instance).
  605. *
  606. * @param srcRow the rows to copy from
  607. * @param policy the policy to determine what gets copied
  608. * @param context the context - see {@link CellCopyContext}
  609. */
  610. @Beta
  611. public void copyRowFrom(Row srcRow, CellCopyPolicy policy, CellCopyContext context) {
  612. if (srcRow == null) {
  613. // srcRow is blank. Overwrite cells with blank values, blank styles, etc per cell copy policy
  614. for (Cell destCell : this) {
  615. CellUtil.copyCell(null, destCell, policy, context);
  616. }
  617. if (policy.isCopyMergedRegions()) {
  618. // Remove MergedRegions in dest row
  619. final int destRowNum = getRowNum();
  620. int index = 0;
  621. final Set<Integer> indices = new HashSet<>();
  622. for (CellRangeAddress destRegion : getSheet().getMergedRegions()) {
  623. if (destRowNum == destRegion.getFirstRow() && destRowNum == destRegion.getLastRow()) {
  624. indices.add(index);
  625. }
  626. index++;
  627. }
  628. getSheet().removeMergedRegions(indices);
  629. }
  630. if (policy.isCopyRowHeight()) {
  631. // clear row height
  632. setHeight((short)-1);
  633. }
  634. } else {
  635. for (final Cell c : srcRow) {
  636. final XSSFCell destCell = createCell(c.getColumnIndex());
  637. CellUtil.copyCell(c, destCell, policy, context);
  638. }
  639. final int sheetIndex = _sheet.getWorkbook().getSheetIndex(_sheet);
  640. final String sheetName = _sheet.getWorkbook().getSheetName(sheetIndex);
  641. final int srcRowNum = srcRow.getRowNum();
  642. final int destRowNum = getRowNum();
  643. final int rowDifference = destRowNum - srcRowNum;
  644. final FormulaShifter formulaShifter = FormulaShifter.createForRowCopy(sheetIndex, sheetName, srcRowNum, srcRowNum, rowDifference, SpreadsheetVersion.EXCEL2007);
  645. final XSSFRowShifter rowShifter = new XSSFRowShifter(_sheet);
  646. rowShifter.updateRowFormulas(this, formulaShifter);
  647. // Copy merged regions that are fully contained on the row
  648. // FIXME: is this something that rowShifter could be doing?
  649. if (policy.isCopyMergedRegions()) {
  650. for (CellRangeAddress srcRegion : srcRow.getSheet().getMergedRegions()) {
  651. if (srcRowNum == srcRegion.getFirstRow() && srcRowNum == srcRegion.getLastRow()) {
  652. CellRangeAddress destRegion = srcRegion.copy();
  653. destRegion.setFirstRow(destRowNum);
  654. destRegion.setLastRow(destRowNum);
  655. getSheet().addMergedRegion(destRegion);
  656. }
  657. }
  658. }
  659. if (policy.isCopyRowHeight()) {
  660. setHeight(srcRow.getHeight());
  661. }
  662. }
  663. }
  664. @Override
  665. public int getOutlineLevel() {
  666. return _row.getOutlineLevel();
  667. }
  668. /**
  669. * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the right.
  670. * @param firstShiftColumnIndex the column to start shifting
  671. * @param lastShiftColumnIndex the column to end shifting
  672. * @param step length of the shifting step
  673. */
  674. @Override
  675. public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) {
  676. RowShifter.validateShiftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step);
  677. for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting
  678. shiftCell(columnIndex, step);
  679. }
  680. for (int columnIndex = firstShiftColumnIndex; columnIndex <= firstShiftColumnIndex+step-1; columnIndex++)
  681. {
  682. _cells.remove(columnIndex);
  683. XSSFCell targetCell = getCell(columnIndex);
  684. if(targetCell != null) {
  685. targetCell.getCTCell().set(CTCell.Factory.newInstance());
  686. }
  687. }
  688. }
  689. /**
  690. * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the left.
  691. * @param firstShiftColumnIndex the column to start shifting
  692. * @param lastShiftColumnIndex the column to end shifting
  693. * @param step length of the shifting step
  694. */
  695. @Override
  696. public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) {
  697. RowShifter.validateShiftLeftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step);
  698. for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){
  699. shiftCell(columnIndex, -step);
  700. }
  701. for (int columnIndex = lastShiftColumnIndex-step+1; columnIndex <= lastShiftColumnIndex; columnIndex++){
  702. _cells.remove(columnIndex);
  703. XSSFCell targetCell = getCell(columnIndex);
  704. if(targetCell != null) {
  705. targetCell.getCTCell().set(CTCell.Factory.newInstance());
  706. }
  707. }
  708. }
  709. private void shiftCell(int columnIndex, int step/*pass negative value for left shift*/){
  710. if(columnIndex + step < 0) {
  711. throw new IllegalStateException("Column index less than zero : " + (Integer.valueOf(columnIndex + step)));
  712. }
  713. XSSFCell currentCell = getCell(columnIndex);
  714. if(currentCell != null){
  715. currentCell.setCellNum(columnIndex+step);
  716. _cells.put(columnIndex+step, currentCell);
  717. }
  718. else {
  719. _cells.remove(columnIndex+step);
  720. XSSFCell targetCell = getCell(columnIndex+step);
  721. if(targetCell != null) {
  722. targetCell.getCTCell().set(CTCell.Factory.newInstance());
  723. }
  724. }
  725. }
  726. }