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

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