PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/poi-3.6/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 1473 lines | 678 code | 178 blank | 617 comment | 95 complexity | 92adbba2709703943dca0bf868a70712 MD5 | raw file
  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.usermodel;
  16. import java.awt.font.FontRenderContext;
  17. import java.awt.font.TextAttribute;
  18. import java.awt.font.TextLayout;
  19. import java.awt.geom.AffineTransform;
  20. import java.io.PrintWriter;
  21. import java.text.AttributedString;
  22. import java.text.DecimalFormat;
  23. import java.text.NumberFormat;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.TreeMap;
  28. import org.apache.poi.ddf.EscherRecord;
  29. import org.apache.poi.hssf.model.Sheet;
  30. import org.apache.poi.hssf.model.Workbook;
  31. import org.apache.poi.hssf.record.CellValueRecordInterface;
  32. import org.apache.poi.hssf.record.DVRecord;
  33. import org.apache.poi.hssf.record.EscherAggregate;
  34. import org.apache.poi.hssf.record.ExtendedFormatRecord;
  35. import org.apache.poi.hssf.record.NoteRecord;
  36. import org.apache.poi.hssf.record.Record;
  37. import org.apache.poi.hssf.record.RowRecord;
  38. import org.apache.poi.hssf.record.SCLRecord;
  39. import org.apache.poi.hssf.record.WSBoolRecord;
  40. import org.apache.poi.hssf.record.WindowTwoRecord;
  41. import org.apache.poi.hssf.record.aggregates.DataValidityTable;
  42. import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock;
  43. import org.apache.poi.hssf.record.formula.FormulaShifter;
  44. import org.apache.poi.hssf.util.PaneInformation;
  45. import org.apache.poi.hssf.util.Region;
  46. import org.apache.poi.ss.usermodel.Cell;
  47. import org.apache.poi.ss.usermodel.CellStyle;
  48. import org.apache.poi.ss.usermodel.Row;
  49. import org.apache.poi.ss.util.CellRangeAddress;
  50. import org.apache.poi.ss.SpreadsheetVersion;
  51. import org.apache.poi.util.POILogFactory;
  52. import org.apache.poi.util.POILogger;
  53. /**
  54. * High level representation of a worksheet.
  55. * @author Andrew C. Oliver (acoliver at apache dot org)
  56. * @author Glen Stampoultzis (glens at apache.org)
  57. * @author Libin Roman (romal at vistaportal.com)
  58. * @author Shawn Laubach (slaubach at apache dot org) (Just a little)
  59. * @author Jean-Pierre Paris (jean-pierre.paris at m4x dot org) (Just a little, too)
  60. * @author Yegor Kozlov (yegor at apache.org) (Autosizing columns)
  61. */
  62. public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
  63. private static final POILogger log = POILogFactory.getLogger(HSSFSheet.class);
  64. private static final int DEBUG = POILogger.DEBUG;
  65. /**
  66. * Used for compile-time optimization. This is the initial size for the collection of
  67. * rows. It is currently set to 20. If you generate larger sheets you may benefit
  68. * by setting this to a higher number and recompiling a custom edition of HSSFSheet.
  69. */
  70. public final static int INITIAL_CAPACITY = 20;
  71. /**
  72. * reference to the low level {@link Sheet} object
  73. */
  74. private final Sheet _sheet;
  75. /** stores rows by zero-based row number */
  76. private final TreeMap<Integer, HSSFRow> _rows;
  77. protected final Workbook _book;
  78. protected final HSSFWorkbook _workbook;
  79. private int _firstrow;
  80. private int _lastrow;
  81. /**
  82. * Creates new HSSFSheet - called by HSSFWorkbook to create a sheet from
  83. * scratch. You should not be calling this from application code (its protected anyhow).
  84. *
  85. * @param workbook - The HSSF Workbook object associated with the sheet.
  86. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()
  87. */
  88. protected HSSFSheet(HSSFWorkbook workbook) {
  89. _sheet = Sheet.createSheet();
  90. _rows = new TreeMap<Integer, HSSFRow>();
  91. this._workbook = workbook;
  92. this._book = workbook.getWorkbook();
  93. }
  94. /**
  95. * Creates an HSSFSheet representing the given Sheet object. Should only be
  96. * called by HSSFWorkbook when reading in an exisiting file.
  97. *
  98. * @param workbook - The HSSF Workbook object associated with the sheet.
  99. * @param sheet - lowlevel Sheet object this sheet will represent
  100. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()
  101. */
  102. protected HSSFSheet(HSSFWorkbook workbook, Sheet sheet) {
  103. this._sheet = sheet;
  104. _rows = new TreeMap<Integer, HSSFRow>();
  105. this._workbook = workbook;
  106. this._book = workbook.getWorkbook();
  107. setPropertiesFromSheet(sheet);
  108. }
  109. HSSFSheet cloneSheet(HSSFWorkbook workbook) {
  110. return new HSSFSheet(workbook, _sheet.cloneSheet());
  111. }
  112. /**
  113. * Return the parent workbook
  114. *
  115. * @return the parent workbook
  116. */
  117. public HSSFWorkbook getWorkbook(){
  118. return _workbook;
  119. }
  120. /**
  121. * used internally to set the properties given a Sheet object
  122. */
  123. private void setPropertiesFromSheet(Sheet sheet) {
  124. RowRecord row = sheet.getNextRow();
  125. boolean rowRecordsAlreadyPresent = row!=null;
  126. while (row != null) {
  127. createRowFromRecord(row);
  128. row = sheet.getNextRow();
  129. }
  130. CellValueRecordInterface[] cvals = sheet.getValueRecords();
  131. long timestart = System.currentTimeMillis();
  132. if (log.check( POILogger.DEBUG ))
  133. log.log(DEBUG, "Time at start of cell creating in HSSF sheet = ",
  134. Long.valueOf(timestart));
  135. HSSFRow lastrow = null;
  136. // Add every cell to its row
  137. for (int i = 0; i < cvals.length; i++) {
  138. CellValueRecordInterface cval = cvals[i];
  139. long cellstart = System.currentTimeMillis();
  140. HSSFRow hrow = lastrow;
  141. if (hrow == null || hrow.getRowNum() != cval.getRow()) {
  142. hrow = getRow( cval.getRow() );
  143. lastrow = hrow;
  144. if (hrow == null) {
  145. // Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords
  146. // Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too.
  147. if (rowRecordsAlreadyPresent) {
  148. // if at least one row record is present, all should be present.
  149. throw new RuntimeException("Unexpected missing row when some rows already present");
  150. }
  151. // create the row record on the fly now.
  152. RowRecord rowRec = new RowRecord(cval.getRow());
  153. sheet.addRow(rowRec);
  154. hrow = createRowFromRecord(rowRec);
  155. }
  156. }
  157. if (log.check( POILogger.DEBUG ))
  158. log.log( DEBUG, "record id = " + Integer.toHexString( ( (Record) cval ).getSid() ) );
  159. hrow.createCellFromRecord( cval );
  160. if (log.check( POILogger.DEBUG ))
  161. log.log( DEBUG, "record took ",
  162. Long.valueOf( System.currentTimeMillis() - cellstart ) );
  163. }
  164. if (log.check( POILogger.DEBUG ))
  165. log.log(DEBUG, "total sheet cell creation took ",
  166. Long.valueOf(System.currentTimeMillis() - timestart));
  167. }
  168. /**
  169. * Create a new row within the sheet and return the high level representation
  170. *
  171. * @param rownum row number
  172. * @return High level HSSFRow object representing a row in the sheet
  173. * @see org.apache.poi.hssf.usermodel.HSSFRow
  174. * @see #removeRow(org.apache.poi.ss.usermodel.Row)
  175. */
  176. public HSSFRow createRow(int rownum)
  177. {
  178. HSSFRow row = new HSSFRow(_workbook, this, rownum);
  179. addRow(row, true);
  180. return row;
  181. }
  182. /**
  183. * Used internally to create a high level Row object from a low level row object.
  184. * USed when reading an existing file
  185. * @param row low level record to represent as a high level Row and add to sheet
  186. * @return HSSFRow high level representation
  187. */
  188. private HSSFRow createRowFromRecord(RowRecord row)
  189. {
  190. HSSFRow hrow = new HSSFRow(_workbook, this, row);
  191. addRow(hrow, false);
  192. return hrow;
  193. }
  194. /**
  195. * Remove a row from this sheet. All cells contained in the row are removed as well
  196. *
  197. * @param row representing a row to remove.
  198. */
  199. public void removeRow(Row row) {
  200. HSSFRow hrow = (HSSFRow) row;
  201. if (row.getSheet() != this) {
  202. throw new IllegalArgumentException("Specified row does not belong to this sheet");
  203. }
  204. if (_rows.size() > 0) {
  205. Integer key = Integer.valueOf(row.getRowNum());
  206. HSSFRow removedRow = _rows.remove(key);
  207. if (removedRow != row) {
  208. //should not happen if the input argument is valid
  209. throw new IllegalArgumentException("Specified row does not belong to this sheet");
  210. }
  211. if (hrow.getRowNum() == getLastRowNum())
  212. {
  213. _lastrow = findLastRow(_lastrow);
  214. }
  215. if (hrow.getRowNum() == getFirstRowNum())
  216. {
  217. _firstrow = findFirstRow(_firstrow);
  218. }
  219. _sheet.removeRow(hrow.getRowRecord());
  220. }
  221. }
  222. /**
  223. * used internally to refresh the "last row" when the last row is removed.
  224. */
  225. private int findLastRow(int lastrow) {
  226. if (lastrow < 1) {
  227. return 0;
  228. }
  229. int rownum = lastrow - 1;
  230. HSSFRow r = getRow(rownum);
  231. while (r == null && rownum > 0) {
  232. r = getRow(--rownum);
  233. }
  234. if (r == null) {
  235. return 0;
  236. }
  237. return rownum;
  238. }
  239. /**
  240. * used internally to refresh the "first row" when the first row is removed.
  241. */
  242. private int findFirstRow(int firstrow)
  243. {
  244. int rownum = firstrow + 1;
  245. HSSFRow r = getRow(rownum);
  246. while (r == null && rownum <= getLastRowNum())
  247. {
  248. r = getRow(++rownum);
  249. }
  250. if (rownum > getLastRowNum())
  251. return 0;
  252. return rownum;
  253. }
  254. /**
  255. * add a row to the sheet
  256. *
  257. * @param addLow whether to add the row to the low level model - false if its already there
  258. */
  259. private void addRow(HSSFRow row, boolean addLow)
  260. {
  261. _rows.put(Integer.valueOf(row.getRowNum()), row);
  262. if (addLow)
  263. {
  264. _sheet.addRow(row.getRowRecord());
  265. }
  266. boolean firstRow = _rows.size() == 1;
  267. if (row.getRowNum() > getLastRowNum() || firstRow)
  268. {
  269. _lastrow = row.getRowNum();
  270. }
  271. if (row.getRowNum() < getFirstRowNum() || firstRow)
  272. {
  273. _firstrow = row.getRowNum();
  274. }
  275. }
  276. /**
  277. * Returns the logical row (not physical) 0-based. If you ask for a row that is not
  278. * defined you get a null. This is to say row 4 represents the fifth row on a sheet.
  279. * @param rowIndex row to get
  280. * @return HSSFRow representing the row number or null if its not defined on the sheet
  281. */
  282. public HSSFRow getRow(int rowIndex) {
  283. return _rows.get(Integer.valueOf(rowIndex));
  284. }
  285. /**
  286. * Returns the number of physically defined rows (NOT the number of rows in the sheet)
  287. */
  288. public int getPhysicalNumberOfRows() {
  289. return _rows.size();
  290. }
  291. /**
  292. * Gets the first row on the sheet
  293. * @return the number of the first logical row on the sheet, zero based
  294. */
  295. public int getFirstRowNum() {
  296. return _firstrow;
  297. }
  298. /**
  299. * Gets the number last row on the sheet.
  300. * Owing to idiosyncrasies in the excel file
  301. * format, if the result of calling this method
  302. * is zero, you can't tell if that means there
  303. * are zero rows on the sheet, or one at
  304. * position zero. For that case, additionally
  305. * call {@link #getPhysicalNumberOfRows()} to
  306. * tell if there is a row at position zero
  307. * or not.
  308. * @return the number of the last row contained in this sheet, zero based.
  309. */
  310. public int getLastRowNum() {
  311. return _lastrow;
  312. }
  313. /**
  314. * Creates a data validation object
  315. * @param dataValidation The Data validation object settings
  316. */
  317. public void addValidationData(HSSFDataValidation dataValidation) {
  318. if (dataValidation == null) {
  319. throw new IllegalArgumentException("objValidation must not be null");
  320. }
  321. DataValidityTable dvt = _sheet.getOrCreateDataValidityTable();
  322. DVRecord dvRecord = dataValidation.createDVRecord(this);
  323. dvt.addDataValidation(dvRecord);
  324. }
  325. /**
  326. * @deprecated (Sep 2008) use {@link #setColumnHidden(int, boolean)}
  327. */
  328. public void setColumnHidden(short columnIndex, boolean hidden) {
  329. setColumnHidden(columnIndex & 0xFFFF, hidden);
  330. }
  331. /**
  332. * @deprecated (Sep 2008) use {@link #isColumnHidden(int)}
  333. */
  334. public boolean isColumnHidden(short columnIndex) {
  335. return isColumnHidden(columnIndex & 0xFFFF);
  336. }
  337. /**
  338. * @deprecated (Sep 2008) use {@link #setColumnWidth(int, int)}
  339. */
  340. public void setColumnWidth(short columnIndex, short width) {
  341. setColumnWidth(columnIndex & 0xFFFF, width & 0xFFFF);
  342. }
  343. /**
  344. * @deprecated (Sep 2008) use {@link #getColumnWidth(int)}
  345. */
  346. public short getColumnWidth(short columnIndex) {
  347. return (short)getColumnWidth(columnIndex & 0xFFFF);
  348. }
  349. /**
  350. * @deprecated (Sep 2008) use {@link #setDefaultColumnWidth(int)}
  351. */
  352. public void setDefaultColumnWidth(short width) {
  353. setDefaultColumnWidth(width & 0xFFFF);
  354. }
  355. /**
  356. * Get the visibility state for a given column.
  357. * @param columnIndex - the column to get (0-based)
  358. * @param hidden - the visiblity state of the column
  359. */
  360. public void setColumnHidden(int columnIndex, boolean hidden) {
  361. _sheet.setColumnHidden(columnIndex, hidden);
  362. }
  363. /**
  364. * Get the hidden state for a given column.
  365. * @param columnIndex - the column to set (0-based)
  366. * @return hidden - <code>false</code> if the column is visible
  367. */
  368. public boolean isColumnHidden(int columnIndex) {
  369. return _sheet.isColumnHidden(columnIndex);
  370. }
  371. /**
  372. * Set the width (in units of 1/256th of a character width)
  373. * <p>
  374. * The maximum column width for an individual cell is 255 characters.
  375. * This value represents the number of characters that can be displayed
  376. * in a cell that is formatted with the standard font.
  377. * </p>
  378. *
  379. * @param columnIndex - the column to set (0-based)
  380. * @param width - the width in units of 1/256th of a character width
  381. * @throws IllegalArgumentException if width > 65280 (the maximum column width in Excel)
  382. */
  383. public void setColumnWidth(int columnIndex, int width) {
  384. _sheet.setColumnWidth(columnIndex, width);
  385. }
  386. /**
  387. * get the width (in units of 1/256th of a character width )
  388. * @param columnIndex - the column to set (0-based)
  389. * @return width - the width in units of 1/256th of a character width
  390. */
  391. public int getColumnWidth(int columnIndex) {
  392. return _sheet.getColumnWidth(columnIndex);
  393. }
  394. /**
  395. * get the default column width for the sheet (if the columns do not define their own width) in
  396. * characters
  397. * @return default column width
  398. */
  399. public int getDefaultColumnWidth() {
  400. return _sheet.getDefaultColumnWidth();
  401. }
  402. /**
  403. * set the default column width for the sheet (if the columns do not define their own width) in
  404. * characters
  405. * @param width default column width
  406. */
  407. public void setDefaultColumnWidth(int width) {
  408. _sheet.setDefaultColumnWidth(width);
  409. }
  410. /**
  411. * get the default row height for the sheet (if the rows do not define their own height) in
  412. * twips (1/20 of a point)
  413. * @return default row height
  414. */
  415. public short getDefaultRowHeight() {
  416. return _sheet.getDefaultRowHeight();
  417. }
  418. /**
  419. * get the default row height for the sheet (if the rows do not define their own height) in
  420. * points.
  421. * @return default row height in points
  422. */
  423. public float getDefaultRowHeightInPoints()
  424. {
  425. return ((float)_sheet.getDefaultRowHeight() / 20);
  426. }
  427. /**
  428. * set the default row height for the sheet (if the rows do not define their own height) in
  429. * twips (1/20 of a point)
  430. * @param height default row height
  431. */
  432. public void setDefaultRowHeight(short height)
  433. {
  434. _sheet.setDefaultRowHeight(height);
  435. }
  436. /**
  437. * set the default row height for the sheet (if the rows do not define their own height) in
  438. * points
  439. * @param height default row height
  440. */
  441. public void setDefaultRowHeightInPoints(float height)
  442. {
  443. _sheet.setDefaultRowHeight((short) (height * 20));
  444. }
  445. /**
  446. * Returns the HSSFCellStyle that applies to the given
  447. * (0 based) column, or null if no style has been
  448. * set for that column
  449. */
  450. public HSSFCellStyle getColumnStyle(int column) {
  451. short styleIndex = _sheet.getXFIndexForColAt((short)column);
  452. if(styleIndex == 0xf) {
  453. // None set
  454. return null;
  455. }
  456. ExtendedFormatRecord xf = _book.getExFormatAt(styleIndex);
  457. return new HSSFCellStyle(styleIndex, xf, _book);
  458. }
  459. /**
  460. * get whether gridlines are printed.
  461. * @return true if printed
  462. */
  463. public boolean isGridsPrinted()
  464. {
  465. return _sheet.isGridsPrinted();
  466. }
  467. /**
  468. * set whether gridlines printed.
  469. * @param value false if not printed.
  470. */
  471. public void setGridsPrinted(boolean value)
  472. {
  473. _sheet.setGridsPrinted(value);
  474. }
  475. /**
  476. * @deprecated (Aug-2008) use <tt>CellRangeAddress</tt> instead of <tt>Region</tt>
  477. */
  478. public int addMergedRegion(org.apache.poi.ss.util.Region region)
  479. {
  480. return _sheet.addMergedRegion( region.getRowFrom(),
  481. region.getColumnFrom(),
  482. //(short) region.getRowTo(),
  483. region.getRowTo(),
  484. region.getColumnTo());
  485. }
  486. /**
  487. * adds a merged region of cells (hence those cells form one)
  488. * @param region (rowfrom/colfrom-rowto/colto) to merge
  489. * @return index of this region
  490. */
  491. public int addMergedRegion(CellRangeAddress region)
  492. {
  493. region.validate(SpreadsheetVersion.EXCEL97);
  494. return _sheet.addMergedRegion( region.getFirstRow(),
  495. region.getFirstColumn(),
  496. region.getLastRow(),
  497. region.getLastColumn());
  498. }
  499. /**
  500. * Whether a record must be inserted or not at generation to indicate that
  501. * formula must be recalculated when workbook is opened.
  502. * @param value true if an uncalced record must be inserted or not at generation
  503. */
  504. public void setForceFormulaRecalculation(boolean value)
  505. {
  506. _sheet.setUncalced(value);
  507. }
  508. /**
  509. * Whether a record must be inserted or not at generation to indicate that
  510. * formula must be recalculated when workbook is opened.
  511. * @return true if an uncalced record must be inserted or not at generation
  512. */
  513. public boolean getForceFormulaRecalculation()
  514. {
  515. return _sheet.getUncalced();
  516. }
  517. /**
  518. * determines whether the output is vertically centered on the page.
  519. * @param value true to vertically center, false otherwise.
  520. */
  521. public void setVerticallyCenter(boolean value)
  522. {
  523. _sheet.getPageSettings().getVCenter().setVCenter(value);
  524. }
  525. /**
  526. * TODO: Boolean not needed, remove after next release
  527. * @deprecated (Mar-2008) use getVerticallyCenter() instead
  528. */
  529. public boolean getVerticallyCenter(boolean value) {
  530. return getVerticallyCenter();
  531. }
  532. /**
  533. * Determine whether printed output for this sheet will be vertically centered.
  534. */
  535. public boolean getVerticallyCenter()
  536. {
  537. return _sheet.getPageSettings().getVCenter().getVCenter();
  538. }
  539. /**
  540. * determines whether the output is horizontally centered on the page.
  541. * @param value true to horizontally center, false otherwise.
  542. */
  543. public void setHorizontallyCenter(boolean value)
  544. {
  545. _sheet.getPageSettings().getHCenter().setHCenter(value);
  546. }
  547. /**
  548. * Determine whether printed output for this sheet will be horizontally centered.
  549. */
  550. public boolean getHorizontallyCenter()
  551. {
  552. return _sheet.getPageSettings().getHCenter().getHCenter();
  553. }
  554. /**
  555. * Sets whether the worksheet is displayed from right to left instead of from left to right.
  556. *
  557. * @param value true for right to left, false otherwise.
  558. */
  559. public void setRightToLeft(boolean value)
  560. {
  561. _sheet.getWindowTwo().setArabic(value);
  562. }
  563. /**
  564. * Whether the text is displayed in right-to-left mode in the window
  565. *
  566. * @return whether the text is displayed in right-to-left mode in the window
  567. */
  568. public boolean isRightToLeft()
  569. {
  570. return _sheet.getWindowTwo().getArabic();
  571. }
  572. /**
  573. * removes a merged region of cells (hence letting them free)
  574. * @param index of the region to unmerge
  575. */
  576. public void removeMergedRegion(int index)
  577. {
  578. _sheet.removeMergedRegion(index);
  579. }
  580. /**
  581. * returns the number of merged regions
  582. * @return number of merged regions
  583. */
  584. public int getNumMergedRegions()
  585. {
  586. return _sheet.getNumMergedRegions();
  587. }
  588. /**
  589. * @deprecated (Aug-2008) use {@link HSSFSheet#getMergedRegion(int)}
  590. */
  591. public Region getMergedRegionAt(int index) {
  592. CellRangeAddress cra = getMergedRegion(index);
  593. return new Region(cra.getFirstRow(), (short)cra.getFirstColumn(),
  594. cra.getLastRow(), (short)cra.getLastColumn());
  595. }
  596. /**
  597. * @return the merged region at the specified index
  598. */
  599. public CellRangeAddress getMergedRegion(int index) {
  600. return _sheet.getMergedRegionAt(index);
  601. }
  602. /**
  603. * @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not
  604. * be the third row if say for instance the second row is undefined.
  605. * Call getRowNum() on each row if you care which one it is.
  606. */
  607. public Iterator<Row> rowIterator() {
  608. @SuppressWarnings("unchecked") // can this clumsy generic syntax be improved?
  609. Iterator<Row> result = (Iterator<Row>)(Iterator<? extends Row>)_rows.values().iterator();
  610. return result;
  611. }
  612. /**
  613. * Alias for {@link #rowIterator()} to allow
  614. * foreach loops
  615. */
  616. public Iterator<Row> iterator() {
  617. return rowIterator();
  618. }
  619. /**
  620. * used internally in the API to get the low level Sheet record represented by this
  621. * Object.
  622. * @return Sheet - low level representation of this HSSFSheet.
  623. */
  624. Sheet getSheet() {
  625. return _sheet;
  626. }
  627. /**
  628. * whether alternate expression evaluation is on
  629. * @param b alternative expression evaluation or not
  630. */
  631. public void setAlternativeExpression(boolean b) {
  632. WSBoolRecord record =
  633. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  634. record.setAlternateExpression(b);
  635. }
  636. /**
  637. * whether alternative formula entry is on
  638. * @param b alternative formulas or not
  639. */
  640. public void setAlternativeFormula(boolean b) {
  641. WSBoolRecord record =
  642. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  643. record.setAlternateFormula(b);
  644. }
  645. /**
  646. * show automatic page breaks or not
  647. * @param b whether to show auto page breaks
  648. */
  649. public void setAutobreaks(boolean b) {
  650. WSBoolRecord record =
  651. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  652. record.setAutobreaks(b);
  653. }
  654. /**
  655. * set whether sheet is a dialog sheet or not
  656. * @param b isDialog or not
  657. */
  658. public void setDialog(boolean b) {
  659. WSBoolRecord record =
  660. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  661. record.setDialog(b);
  662. }
  663. /**
  664. * set whether to display the guts or not
  665. *
  666. * @param b guts or no guts (or glory)
  667. */
  668. public void setDisplayGuts(boolean b) {
  669. WSBoolRecord record =
  670. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  671. record.setDisplayGuts(b);
  672. }
  673. /**
  674. * fit to page option is on
  675. * @param b fit or not
  676. */
  677. public void setFitToPage(boolean b) {
  678. WSBoolRecord record =
  679. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  680. record.setFitToPage(b);
  681. }
  682. /**
  683. * set if row summaries appear below detail in the outline
  684. * @param b below or not
  685. */
  686. public void setRowSumsBelow(boolean b) {
  687. WSBoolRecord record =
  688. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  689. record.setRowSumsBelow(b);
  690. //setAlternateExpression must be set in conjuction with setRowSumsBelow
  691. record.setAlternateExpression(b);
  692. }
  693. /**
  694. * set if col summaries appear right of the detail in the outline
  695. * @param b right or not
  696. */
  697. public void setRowSumsRight(boolean b) {
  698. WSBoolRecord record =
  699. (WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid);
  700. record.setRowSumsRight(b);
  701. }
  702. /**
  703. * whether alternate expression evaluation is on
  704. * @return alternative expression evaluation or not
  705. */
  706. public boolean getAlternateExpression() {
  707. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  708. .getAlternateExpression();
  709. }
  710. /**
  711. * whether alternative formula entry is on
  712. * @return alternative formulas or not
  713. */
  714. public boolean getAlternateFormula() {
  715. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  716. .getAlternateFormula();
  717. }
  718. /**
  719. * show automatic page breaks or not
  720. * @return whether to show auto page breaks
  721. */
  722. public boolean getAutobreaks() {
  723. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  724. .getAutobreaks();
  725. }
  726. /**
  727. * get whether sheet is a dialog sheet or not
  728. * @return isDialog or not
  729. */
  730. public boolean getDialog() {
  731. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  732. .getDialog();
  733. }
  734. /**
  735. * get whether to display the guts or not
  736. *
  737. * @return guts or no guts (or glory)
  738. */
  739. public boolean getDisplayGuts() {
  740. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  741. .getDisplayGuts();
  742. }
  743. /**
  744. * Gets the flag indicating whether the window should show 0 (zero) in cells containing zero value.
  745. * When false, cells with zero value appear blank instead of showing the number zero.
  746. * <p>
  747. * In Excel 2003 this option can be changed in the Options dialog on the View tab.
  748. * </p>
  749. * @return whether all zero values on the worksheet are displayed
  750. */
  751. public boolean isDisplayZeros(){
  752. return _sheet.getWindowTwo().getDisplayZeros();
  753. }
  754. /**
  755. * Set whether the window should show 0 (zero) in cells containing zero value.
  756. * When false, cells with zero value appear blank instead of showing the number zero.
  757. * <p>
  758. * In Excel 2003 this option can be set in the Options dialog on the View tab.
  759. * </p>
  760. * @param value whether to display or hide all zero values on the worksheet
  761. */
  762. public void setDisplayZeros(boolean value){
  763. _sheet.getWindowTwo().setDisplayZeros(value);
  764. }
  765. /**
  766. * fit to page option is on
  767. * @return fit or not
  768. */
  769. public boolean getFitToPage() {
  770. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  771. .getFitToPage();
  772. }
  773. /**
  774. * get if row summaries appear below detail in the outline
  775. * @return below or not
  776. */
  777. public boolean getRowSumsBelow() {
  778. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  779. .getRowSumsBelow();
  780. }
  781. /**
  782. * get if col summaries appear right of the detail in the outline
  783. * @return right or not
  784. */
  785. public boolean getRowSumsRight() {
  786. return ((WSBoolRecord) _sheet.findFirstRecordBySid(WSBoolRecord.sid))
  787. .getRowSumsRight();
  788. }
  789. /**
  790. * Returns whether gridlines are printed.
  791. * @return Gridlines are printed
  792. */
  793. public boolean isPrintGridlines() {
  794. return getSheet().getPrintGridlines().getPrintGridlines();
  795. }
  796. /**
  797. * Turns on or off the printing of gridlines.
  798. * @param newPrintGridlines boolean to turn on or off the printing of
  799. * gridlines
  800. */
  801. public void setPrintGridlines(boolean newPrintGridlines) {
  802. getSheet().getPrintGridlines().setPrintGridlines(newPrintGridlines);
  803. }
  804. /**
  805. * Gets the print setup object.
  806. * @return The user model for the print setup object.
  807. */
  808. public HSSFPrintSetup getPrintSetup() {
  809. return new HSSFPrintSetup(_sheet.getPageSettings().getPrintSetup());
  810. }
  811. public HSSFHeader getHeader() {
  812. return new HSSFHeader(_sheet.getPageSettings());
  813. }
  814. public HSSFFooter getFooter() {
  815. return new HSSFFooter(_sheet.getPageSettings());
  816. }
  817. /**
  818. * Note - this is not the same as whether the sheet is focused (isActive)
  819. * @return <code>true</code> if this sheet is currently selected
  820. */
  821. public boolean isSelected() {
  822. return getSheet().getWindowTwo().getSelected();
  823. }
  824. /**
  825. * Sets whether sheet is selected.
  826. * @param sel Whether to select the sheet or deselect the sheet.
  827. */
  828. public void setSelected(boolean sel) {
  829. getSheet().getWindowTwo().setSelected(sel);
  830. }
  831. /**
  832. * @return <code>true</code> if this sheet is currently focused
  833. */
  834. public boolean isActive() {
  835. return getSheet().getWindowTwo().isActive();
  836. }
  837. /**
  838. * Sets whether sheet is selected.
  839. * @param sel Whether to select the sheet or deselect the sheet.
  840. */
  841. public void setActive(boolean sel) {
  842. getSheet().getWindowTwo().setActive(sel);
  843. }
  844. /**
  845. * Gets the size of the margin in inches.
  846. * @param margin which margin to get
  847. * @return the size of the margin
  848. */
  849. public double getMargin(short margin) {
  850. return _sheet.getPageSettings().getMargin(margin);
  851. }
  852. /**
  853. * Sets the size of the margin in inches.
  854. * @param margin which margin to get
  855. * @param size the size of the margin
  856. */
  857. public void setMargin(short margin, double size) {
  858. _sheet.getPageSettings().setMargin(margin, size);
  859. }
  860. private WorksheetProtectionBlock getProtectionBlock() {
  861. return _sheet.getProtectionBlock();
  862. }
  863. /**
  864. * Answer whether protection is enabled or disabled
  865. * @return true => protection enabled; false => protection disabled
  866. */
  867. public boolean getProtect() {
  868. return getProtectionBlock().isSheetProtected();
  869. }
  870. /**
  871. * @return hashed password
  872. */
  873. public short getPassword() {
  874. return (short)getProtectionBlock().getPasswordHash();
  875. }
  876. /**
  877. * Answer whether object protection is enabled or disabled
  878. * @return true => protection enabled; false => protection disabled
  879. */
  880. public boolean getObjectProtect() {
  881. return getProtectionBlock().isObjectProtected();
  882. }
  883. /**
  884. * Answer whether scenario protection is enabled or disabled
  885. * @return true => protection enabled; false => protection disabled
  886. */
  887. public boolean getScenarioProtect() {
  888. return getProtectionBlock().isScenarioProtected();
  889. }
  890. /**
  891. * Sets the protection enabled as well as the password
  892. * @param password to set for protection. Pass <code>null</code> to remove protection
  893. */
  894. public void protectSheet(String password) {
  895. getProtectionBlock().protectSheet(password, true, true); //protect objs&scenarios(normal)
  896. }
  897. /**
  898. * Sets the zoom magnification for the sheet. The zoom is expressed as a
  899. * fraction. For example to express a zoom of 75% use 3 for the numerator
  900. * and 4 for the denominator.
  901. *
  902. * @param numerator The numerator for the zoom magnification.
  903. * @param denominator The denominator for the zoom magnification.
  904. */
  905. public void setZoom( int numerator, int denominator)
  906. {
  907. if (numerator < 1 || numerator > 65535)
  908. throw new IllegalArgumentException("Numerator must be greater than 1 and less than 65536");
  909. if (denominator < 1 || denominator > 65535)
  910. throw new IllegalArgumentException("Denominator must be greater than 1 and less than 65536");
  911. SCLRecord sclRecord = new SCLRecord();
  912. sclRecord.setNumerator((short)numerator);
  913. sclRecord.setDenominator((short)denominator);
  914. getSheet().setSCLRecord(sclRecord);
  915. }
  916. /**
  917. * The top row in the visible view when the sheet is
  918. * first viewed after opening it in a viewer
  919. * @return short indicating the rownum (0 based) of the top row
  920. */
  921. public short getTopRow() {
  922. return _sheet.getTopRow();
  923. }
  924. /**
  925. * The left col in the visible view when the sheet is
  926. * first viewed after opening it in a viewer
  927. * @return short indicating the rownum (0 based) of the top row
  928. */
  929. public short getLeftCol() {
  930. return _sheet.getLeftCol();
  931. }
  932. /**
  933. * Sets desktop window pane display area, when the
  934. * file is first opened in a viewer.
  935. * @param toprow the top row to show in desktop window pane
  936. * @param leftcol the left column to show in desktop window pane
  937. */
  938. public void showInPane(short toprow, short leftcol){
  939. _sheet.setTopRow(toprow);
  940. _sheet.setLeftCol(leftcol);
  941. }
  942. /**
  943. * Shifts the merged regions left or right depending on mode
  944. * <p>
  945. * TODO: MODE , this is only row specific
  946. * @param startRow
  947. * @param endRow
  948. * @param n
  949. * @param isRow
  950. */
  951. protected void shiftMerged(int startRow, int endRow, int n, boolean isRow) {
  952. List<CellRangeAddress> shiftedRegions = new ArrayList<CellRangeAddress>();
  953. //move merged regions completely if they fall within the new region boundaries when they are shifted
  954. for (int i = 0; i < getNumMergedRegions(); i++) {
  955. CellRangeAddress merged = getMergedRegion(i);
  956. boolean inStart= (merged.getFirstRow() >= startRow || merged.getLastRow() >= startRow);
  957. boolean inEnd = (merged.getFirstRow() <= endRow || merged.getLastRow() <= endRow);
  958. //don't check if it's not within the shifted area
  959. if (!inStart || !inEnd) {
  960. continue;
  961. }
  962. //only shift if the region outside the shifted rows is not merged too
  963. if (!containsCell(merged, startRow-1, 0) && !containsCell(merged, endRow+1, 0)){
  964. merged.setFirstRow(merged.getFirstRow()+n);
  965. merged.setLastRow(merged.getLastRow()+n);
  966. //have to remove/add it back
  967. shiftedRegions.add(merged);
  968. removeMergedRegion(i);
  969. i = i -1; // we have to back up now since we removed one
  970. }
  971. }
  972. //read so it doesn't get shifted again
  973. Iterator<CellRangeAddress> iterator = shiftedRegions.iterator();
  974. while (iterator.hasNext()) {
  975. CellRangeAddress region = iterator.next();
  976. this.addMergedRegion(region);
  977. }
  978. }
  979. private static boolean containsCell(CellRangeAddress cr, int rowIx, int colIx) {
  980. if (cr.getFirstRow() <= rowIx && cr.getLastRow() >= rowIx
  981. && cr.getFirstColumn() <= colIx && cr.getLastColumn() >= colIx)
  982. {
  983. return true;
  984. }
  985. return false;
  986. }
  987. /**
  988. * Shifts rows between startRow and endRow n number of rows.
  989. * If you use a negative number, it will shift rows up.
  990. * Code ensures that rows don't wrap around.
  991. *
  992. * Calls shiftRows(startRow, endRow, n, false, false);
  993. *
  994. * <p>
  995. * Additionally shifts merged regions that are completely defined in these
  996. * rows (ie. merged 2 cells on a row to be shifted).
  997. * @param startRow the row to start shifting
  998. * @param endRow the row to end shifting
  999. * @param n the number of rows to shift
  1000. */
  1001. public void shiftRows( int startRow, int endRow, int n ) {
  1002. shiftRows(startRow, endRow, n, false, false);
  1003. }
  1004. /**
  1005. * Shifts rows between startRow and endRow n number of rows.
  1006. * If you use a negative number, it will shift rows up.
  1007. * Code ensures that rows don't wrap around
  1008. *
  1009. * <p>
  1010. * Additionally shifts merged regions that are completely defined in these
  1011. * rows (ie. merged 2 cells on a row to be shifted).
  1012. * <p>
  1013. * TODO Might want to add bounds checking here
  1014. * @param startRow the row to start shifting
  1015. * @param endRow the row to end shifting
  1016. * @param n the number of rows to shift
  1017. * @param copyRowHeight whether to copy the row height during the shift
  1018. * @param resetOriginalRowHeight whether to set the original row's height to the default
  1019. */
  1020. public void shiftRows( int startRow, int endRow, int n, boolean copyRowHeight, boolean resetOriginalRowHeight) {
  1021. shiftRows(startRow, endRow, n, copyRowHeight, resetOriginalRowHeight, true);
  1022. }
  1023. /**
  1024. * Shifts rows between startRow and endRow n number of rows.
  1025. * If you use a negative number, it will shift rows up.
  1026. * Code ensures that rows don't wrap around
  1027. *
  1028. * <p>
  1029. * Additionally shifts merged regions that are completely defined in these
  1030. * rows (ie. merged 2 cells on a row to be shifted).
  1031. * <p>
  1032. * TODO Might want to add bounds checking here
  1033. * @param startRow the row to start shifting
  1034. * @param endRow the row to end shifting
  1035. * @param n the number of rows to shift
  1036. * @param copyRowHeight whether to copy the row height during the shift
  1037. * @param resetOriginalRowHeight whether to set the original row's height to the default
  1038. * @param moveComments whether to move comments at the same time as the cells they are attached to
  1039. */
  1040. public void shiftRows(int startRow, int endRow, int n,
  1041. boolean copyRowHeight, boolean resetOriginalRowHeight, boolean moveComments) {
  1042. int s, inc;
  1043. if (n < 0) {
  1044. s = startRow;
  1045. inc = 1;
  1046. } else {
  1047. s = endRow;
  1048. inc = -1;
  1049. }
  1050. NoteRecord[] noteRecs;
  1051. if (moveComments) {
  1052. noteRecs = _sheet.getNoteRecords();
  1053. } else {
  1054. noteRecs = NoteRecord.EMPTY_ARRAY;
  1055. }
  1056. shiftMerged(startRow, endRow, n, true);
  1057. _sheet.getPageSettings().shiftRowBreaks(startRow, endRow, n);
  1058. for ( int rowNum = s; rowNum >= startRow && rowNum <= endRow && rowNum >= 0 && rowNum < 65536; rowNum += inc ) {
  1059. HSSFRow row = getRow( rowNum );
  1060. HSSFRow row2Replace = getRow( rowNum + n );
  1061. if ( row2Replace == null )
  1062. row2Replace = createRow( rowNum + n );
  1063. // Remove all the old cells from the row we'll
  1064. // be writing too, before we start overwriting
  1065. // any cells. This avoids issues with cells
  1066. // changing type, and records not being correctly
  1067. // overwritten
  1068. row2Replace.removeAllCells();
  1069. // If this row doesn't exist, nothing needs to
  1070. // be done for the now empty destination row
  1071. if (row == null) continue; // Nothing to do for this row
  1072. // Fix up row heights if required
  1073. if (copyRowHeight) {
  1074. row2Replace.setHeight(row.getHeight());
  1075. }
  1076. if (resetOriginalRowHeight) {
  1077. row.setHeight((short)0xff);
  1078. }
  1079. // Copy each cell from the source row to
  1080. // the destination row
  1081. for(Iterator<Cell> cells = row.cellIterator(); cells.hasNext(); ) {
  1082. HSSFCell cell = (HSSFCell)cells.next();
  1083. row.removeCell( cell );
  1084. CellValueRecordInterface cellRecord = cell.getCellValueRecord();
  1085. cellRecord.setRow( rowNum + n );
  1086. row2Replace.createCellFromRecord( cellRecord );
  1087. _sheet.addValueRecord( rowNum + n, cellRecord );
  1088. HSSFHyperlink link = cell.getHyperlink();
  1089. if(link != null){
  1090. link.setFirstRow(link.getFirstRow() + n);
  1091. link.setLastRow(link.getLastRow() + n);
  1092. }
  1093. }
  1094. // Now zap all the cells in the source row
  1095. row.removeAllCells();
  1096. // Move comments from the source row to the
  1097. // destination row. Note that comments can
  1098. // exist for cells which are null
  1099. if(moveComments) {
  1100. // This code would get simpler if NoteRecords could be organised by HSSFRow.
  1101. for(int i=noteRecs.length-1; i>=0; i--) {
  1102. NoteRecord nr = noteRecs[i];
  1103. if (nr.getRow() != rowNum) {
  1104. continue;
  1105. }
  1106. HSSFComment comment = getCellComment(rowNum, nr.getColumn());
  1107. if (comment != null) {
  1108. comment.setRow(rowNum + n);
  1109. }
  1110. }
  1111. }
  1112. }
  1113. if ( endRow == _lastrow || endRow + n > _lastrow ) _lastrow = Math.min( endRow + n, SpreadsheetVersion.EXCEL97.getLastRowIndex() );
  1114. if ( startRow == _firstrow || startRow + n < _firstrow ) _firstrow = Math.max( startRow + n, 0 );
  1115. // Update any formulas on this sheet that point to
  1116. // rows which have been moved
  1117. int sheetIndex = _workbook.getSheetIndex(this);
  1118. short externSheetIndex = _book.checkExternSheet(sheetIndex);
  1119. FormulaShifter shifter = FormulaShifter.createForRowShift(externSheetIndex, startRow, endRow, n);
  1120. _sheet.updateFormulasAfterCellShift(shifter, externSheetIndex);
  1121. int nSheets = _workbook.getNumberOfSheets();
  1122. for(int i=0; i<nSheets; i++) {
  1123. Sheet otherSheet = _workbook.getSheetAt(i).getSheet();
  1124. if (otherSheet == this._sheet) {
  1125. continue;
  1126. }
  1127. short otherExtSheetIx = _book.checkExternSheet(i);
  1128. otherSheet.updateFormulasAfterCellShift(shifter, otherExtSheetIx);
  1129. }
  1130. _workbook.getWorkbook().updateNamesAfterCellShift(shifter);
  1131. }
  1132. protected void insertChartRecords(List<Record> records) {
  1133. int window2Loc = _sheet.findFirstRecordLocBySid(WindowTwoRecord.sid);
  1134. _sheet.getRecords().addAll(window2Loc, records);
  1135. }
  1136. /**
  1137. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  1138. * @param colSplit Horizonatal position of split.
  1139. * @param rowSplit Vertical position of split.
  1140. * @param topRow Top row visible in bottom pane
  1141. * @param leftmostColumn Left column visible in right pane.
  1142. */
  1143. public void createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow) {
  1144. validateColumn(colSplit);
  1145. validateRow(rowSplit);
  1146. if (leftmostColumn < colSplit) throw new IllegalArgumentException("leftmostColumn parameter must not be less than colSplit parameter");
  1147. if (topRow < rowSplit) throw new IllegalArgumentException("topRow parameter must not be less than leftmostColumn parameter");
  1148. getSheet().createFreezePane( colSplit, rowSplit, topRow, leftmostColumn );
  1149. }
  1150. /**
  1151. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  1152. * @param colSplit Horizonatal position of split.
  1153. * @param rowSplit Vertical position of split.
  1154. */
  1155. public void createFreezePane(int colSplit, int rowSplit) {
  1156. createFreezePane(colSplit, rowSplit, colSplit, rowSplit);
  1157. }
  1158. /**
  1159. * Creates a split pane. Any existing freezepane or split pane is overwritten.
  1160. * @param xSplitPos Horizonatal position of split (in 1/20th of a point).
  1161. * @param ySplitPos Vertical position of split (in 1/20th of a point).
  1162. * @param topRow Top row visible in bottom pane
  1163. * @param leftmostColumn Left column visible in right pane.
  1164. * @param activePane Active pane. One of: PANE_LOWER_RIGHT,
  1165. * PANE_UPPER_RIGHT, PANE_LOWER_LEFT, PANE_UPPER_LEFT
  1166. * @see #PANE_LOWER_LEFT
  1167. * @see #PANE_LOWER_RIGHT
  1168. * @see #PANE_UPPER_LEFT
  1169. * @see #PANE_UPPER_RIGHT
  1170. */
  1171. public void createSplitPane(int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, int activePane) {
  1172. getSheet().createSplitPane( xSplitPos, ySplitPos, topRow, leftmostColumn, activePane );
  1173. }
  1174. /**
  1175. * Returns the information regarding the currently configured pane (split or freeze).
  1176. * @return null if no pane configured, or the pane information.
  1177. */
  1178. public PaneInformation getPaneInformation() {
  1179. return getSheet().getPaneInformation();
  1180. }
  1181. /**
  1182. * Sets whether the gridlines are shown in a viewer.
  1183. * @param show whether to show gridlines or not
  1184. */
  1185. public void setDisplayGridlines(boolean show) {
  1186. _sheet.setDisplayGridlines(show);
  1187. }
  1188. /**
  1189. * Returns if gridlines are displayed.
  1190. * @return whether gridlines are displayed
  1191. */
  1192. public boolean isDisplayGridlines() {
  1193. return _sheet.isDisplayGridlines();
  1194. }
  1195. /**
  1196. * Sets whether the formulas are shown in a viewer.
  1197. * @param show whether to show formulas or not
  1198. */
  1199. public void setDisplayFormulas(boolean show) {
  1200. _sheet.setDisplayFormulas(show);
  1201. }
  1202. /**
  1203. * Returns if formulas are displayed.
  1204. * @return whether formulas are displayed
  1205. */
  1206. public boolean isDisplayFormulas() {
  1207. return _sheet.isDisplayFormulas();
  1208. }
  1209. /**
  1210. * Sets whether the RowColHeadings are shown in a viewer.
  1211. * @param show whether to show RowColHeadings or not
  1212. */
  1213. public void setDisplayRowColHeadings(boolean show) {
  1214. _sheet.setDisplayRowColHeadings(show);
  1215. }
  1216. /**
  1217. * Returns if RowColHeadings are displayed.
  1218. * @return whether RowColHeadings are displayed
  1219. */
  1220. public boolean isDisplayRowColHeadings() {
  1221. return _sheet.isDisplayRowColHeadings();
  1222. }
  1223. /**
  1224. * Sets a page break at the indicated row
  1225. * @param row FIXME: Document this!
  1226. */
  1227. public void setRowBreak(int row) {
  1228. validateRow(row);
  1229. _sheet.getPageSettings().setRowBreak(row, (short)0, (short)255);
  1230. }
  1231. /**
  1232. * @return <code>true</code> if there is a page break at the indicated row
  1233. */
  1234. public boolean isRowBroken(int row) {
  1235. return _sheet.getPageSettings().isRowBroken(row);
  1236. }
  1237. /**
  1238. * Removes the page break at the indicated row
  1239. */
  1240. public void removeRowBreak(int row) {
  1241. _sheet.getPageSettings().removeRowBreak(row);
  1242. }
  1243. /**
  1244. * @return row indexes of all the horizontal page breaks, never <code>null</code>
  1245. */
  1246. public int[] getRowBreaks() {
  1247. //we can probably cache this information, but this should be a sparsely used function
  1248. return _sheet.getPageSettings().getRowBreaks();
  1249. }
  1250. /**
  1251. * @return column indexes of all the vertical page breaks, never <code>null</code>
  1252. */
  1253. public int[] getColumnBreaks() {
  1254. //we can probably cache this information, but this should be a sparsely used function
  1255. return _sheet.getPageSettings().getColumnBreaks();
  1256. }
  1257. /**
  1258. * Sets a page break at the indicated column
  1259. * @param column
  1260. */
  1261. public void setColumnBreak(int column) {
  1262. validateColumn((short)column);
  1263. _sheet.getPageSettings().setColumnBreak((short)column, (short)0, (short) SpreadsheetVersion.EXCEL97.getLastRowIndex());
  1264. }
  1265. /**
  1266. * Determines if there is a page break at the indicated column
  1267. * @param column FIXME: Document this!
  1268. * @return FIXME: Document this!
  1269. */
  1270. public boolean isColumnBroken(int column) {
  1271. return _sheet.getPageSettings().isColumnBroken(column);
  1272. }
  1273. /**
  1274. * Removes a page break at the indicated column
  1275. * @param column
  1276. */
  1277. public void removeColumnBreak(int column) {
  1278. _sheet.getPageSettings().removeColumnBreak(column);
  1279. }
  1280. /**
  1281. * Runs a bounds check for row numbers
  1282. * @param row
  1283. */
  1284. protected void validateRow(int row) {
  1285. int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex();
  1286. if (row > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow);
  1287. if (row < 0) throw new IllegalArgumentException("Minumum row number is 0");
  1288. }
  1289. /**
  1290. * Runs a bounds check for column numbers
  1291. * @param column
  1292. */
  1293. protected void validateColumn(int column) {