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.

Cell.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import java.time.LocalDate;
  17. import java.time.LocalDateTime;
  18. import java.util.Calendar;
  19. import java.util.Date;
  20. import java.util.Map;
  21. import org.apache.poi.ss.formula.FormulaParseException;
  22. import org.apache.poi.ss.util.CellAddress;
  23. import org.apache.poi.ss.util.CellRangeAddress;
  24. import org.apache.poi.util.Removal;
  25. /**
  26. * High level representation of a cell in a row of a spreadsheet.
  27. * <p>
  28. * Cells can be numeric, formula-based or string-based (text). The cell type
  29. * specifies this. String cells cannot conatin numbers and numeric cells cannot
  30. * contain strings (at least according to our model). Client apps should do the
  31. * conversions themselves. Formula cells have the formula string, as well as
  32. * the formula result, which can be numeric or string.
  33. * </p>
  34. * <p>
  35. * Cells should have their number (0 based) before being added to a row.
  36. * </p>
  37. */
  38. public interface Cell {
  39. /**
  40. * Returns column index of this cell
  41. *
  42. * @return zero-based column index of a column in a sheet.
  43. */
  44. int getColumnIndex();
  45. /**
  46. * Returns row index of a row in the sheet that contains this cell
  47. *
  48. * @return zero-based row index of a row in the sheet that contains this cell
  49. */
  50. int getRowIndex();
  51. /**
  52. * Returns the sheet this cell belongs to
  53. *
  54. * @return the sheet this cell belongs to
  55. */
  56. Sheet getSheet();
  57. /**
  58. * Returns the Row this cell belongs to
  59. *
  60. * @return the Row that owns this cell
  61. */
  62. Row getRow();
  63. /**
  64. * Set the cells type (blank, numeric, boolean, error or string).
  65. * <p>If the cell currently contains a value, the value will
  66. * be converted to match the new type, if possible. Formatting
  67. * is generally lost in the process however.</p>
  68. * <p>Conversion rules:</p>
  69. * <p>to NUMERIC: numeric value is left as is. True converts to 1.0, false converts to 0. otherwise, the
  70. * value is set to 0. Formula is removed.</p>
  71. * <p>If what you want to do is get a String value for your
  72. * numeric cell, <i>stop!</i> This is not the way to do it.
  73. * Instead, for fetching the string value of a numeric or boolean
  74. * or date cell, use {@link DataFormatter} instead.</p>
  75. * <p>If cell is a member of an array formula group containing more than 1 cell, an {@link IllegalStateException}
  76. * is thrown. If the array formula group contains only this cell, it is removed.</p>
  77. * <p>Passing {@link CellType#FORMULA} is illegal and will result in an {@link IllegalArgumentException}.</p>
  78. *
  79. * @deprecated This method is deprecated and will be removed in POI 5.0.
  80. * Use explicit {@link #setCellFormula(String)}, <code>setCellValue(...)</code> or {@link #setBlank()}
  81. * to get the desired result.
  82. * @throws IllegalArgumentException if the specified cell type is invalid (null, _NONE or FORMULA)
  83. * @throws IllegalStateException if the current value cannot be converted to the new type or
  84. * if the cell is a part of an array formula group containing other cells
  85. */
  86. @Deprecated
  87. @Removal(version = "5.0")
  88. void setCellType(CellType cellType);
  89. /**
  90. * Removes formula and value from the cell, and sets its type to {@link CellType#BLANK}.
  91. * Preserves comments and hyperlinks.
  92. * While {@link #setCellType(CellType)} exists, is an alias for {@code setCellType(CellType.BLANK)}.
  93. */
  94. void setBlank();
  95. /**
  96. * Return the cell type.
  97. *
  98. * @return the cell type
  99. */
  100. CellType getCellType();
  101. /**
  102. * Only valid for formula cells
  103. *
  104. * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
  105. * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
  106. * on the cached value of the formula
  107. */
  108. CellType getCachedFormulaResultType();
  109. /**
  110. * Set a numeric value for the cell.
  111. *
  112. * @param value the numeric value to set this cell to. For formulas, we'll set the
  113. * precalculated value, for numerics we'll set its value. For other types, we
  114. * will change the cell to a numeric cell and set its value.
  115. */
  116. void setCellValue(double value);
  117. /**
  118. * <p>Converts the supplied date to its equivalent Excel numeric value and sets
  119. * that into the cell.</p>
  120. *
  121. * <p><b>Note</b> - There is actually no 'DATE' cell type in Excel. In many
  122. * cases (when entering date values), Excel automatically adjusts the
  123. * <i>cell style</i> to some date format, creating the illusion that the cell
  124. * data type is now something besides {@link CellType#NUMERIC}. POI
  125. * does not attempt to replicate this behaviour. To make a numeric cell
  126. * display as a date, use {@link #setCellStyle(CellStyle)} etc.</p>
  127. *
  128. * @param value the numeric value to set this cell to. For formulas, we'll set the
  129. * precalculated value, for numerics we'll set its value. For other types, we
  130. * will change the cell to a numerics cell and set its value.
  131. */
  132. void setCellValue(Date value);
  133. /**
  134. * <p>Converts the supplied date to its equivalent Excel numeric value and sets
  135. * that into the cell.</p>
  136. *
  137. * <p><b>Note</b> - There is actually no 'DATE' cell type in Excel. In many
  138. * cases (when entering date values), Excel automatically adjusts the
  139. * <i>cell style</i> to some date format, creating the illusion that the cell
  140. * data type is now something besides {@link CellType#NUMERIC}. POI
  141. * does not attempt to replicate this behaviour. To make a numeric cell
  142. * display as a date, use {@link #setCellStyle(CellStyle)} etc.</p>
  143. *
  144. * @param value the numeric value to set this cell to. For formulas, we'll set the
  145. * precalculated value, for numerics we'll set its value. For other types, we
  146. * will change the cell to a numerics cell and set its value.
  147. */
  148. void setCellValue(LocalDateTime value);
  149. /**
  150. * <p>Converts the supplied date to its equivalent Excel numeric value and sets
  151. * that into the cell.</p>
  152. *
  153. * <p><b>Note</b> - There is actually no 'DATE' cell type in Excel. In many
  154. * cases (when entering date values), Excel automatically adjusts the
  155. * <i>cell style</i> to some date format, creating the illusion that the cell
  156. * data type is now something besides {@link CellType#NUMERIC}. POI
  157. * does not attempt to replicate this behaviour. To make a numeric cell
  158. * display as a date, use {@link #setCellStyle(CellStyle)} etc.</p>
  159. *
  160. * @param value the numeric value to set this cell to. For formulas, we'll set the
  161. * precalculated value, for numerics we'll set its value. For other types, we
  162. * will change the cell to a numerics cell and set its value.
  163. */
  164. default void setCellValue(LocalDate value) {
  165. setCellValue(value == null ? null : value.atStartOfDay());
  166. }
  167. /**
  168. * <p>Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
  169. * a date.</p>
  170. * <p>
  171. * This will set the cell value based on the Calendar's timezone. As Excel
  172. * does not support timezones this means that both 20:00+03:00 and
  173. * 20:00-03:00 will be reported as the same value (20:00) even that there
  174. * are 6 hours difference between the two times. This difference can be
  175. * preserved by using <code>setCellValue(value.getTime())</code> which will
  176. * automatically shift the times to the default timezone.
  177. * </p>
  178. *
  179. * @param value the date value to set this cell to. For formulas, we'll set the
  180. * precalculated value, for numerics we'll set its value. For other types, we
  181. * will change the cell to a numeric cell and set its value.
  182. */
  183. void setCellValue(Calendar value);
  184. /**
  185. * Set a rich string value for the cell.
  186. *
  187. * @param value value to set the cell to. For formulas, we'll set the formula
  188. * string, for String cells we'll set its value. For other types, we will
  189. * change the cell to a string cell and set its value.
  190. * If value is null then we will change the cell to a Blank cell.
  191. */
  192. void setCellValue(RichTextString value);
  193. /**
  194. * Set a string value for the cell.
  195. *
  196. * @param value value to set the cell to. For formulas, we'll set the formula
  197. * string, for String cells we'll set its value. For other types, we will
  198. * change the cell to a string cell and set its value.
  199. * If value is null then we will change the cell to a Blank cell.
  200. */
  201. void setCellValue(String value);
  202. /**
  203. * Sets formula for this cell.
  204. * <p>If {@code formula} is not null, sets or updates the formula. If {@code formula} is null, removes the formula.
  205. * Or use {@link #removeFormula()} to remove the formula.</p>
  206. *
  207. * <p>Note, this method only sets the formula string and does not calculate the formula value.
  208. * To set the precalculated value use {@link #setCellValue}.</p>
  209. *
  210. * <p>If the cell was blank, sets value to 0. Otherwise, preserves the value as precalculated.</p>
  211. *
  212. * @param formula the formula to set, e.g. <code>"SUM(C4:E4)"</code>.
  213. * If the argument is <code>null</code> then the current formula is removed.
  214. *
  215. * @see Cell#removeFormula
  216. * @throws IllegalStateException if this cell is a part of an array formula group containing other cells
  217. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  218. */
  219. void setCellFormula(String formula) throws FormulaParseException, IllegalStateException;
  220. /**
  221. * Removes formula, if any.
  222. *
  223. * If cell was blank, leaves it as is.
  224. * If it is a part of an array formula group, blanks the cell.
  225. * If has a regular formula, removes the formula preserving the "cached" value.
  226. * @throws IllegalStateException if cell is a part of an array formula group containing other cells
  227. */
  228. void removeFormula() throws IllegalStateException;
  229. /**
  230. * Return a formula for the cell, for example, <code>SUM(C4:E4)</code>
  231. *
  232. * @return a formula for the cell
  233. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not {@link CellType#FORMULA}
  234. */
  235. String getCellFormula();
  236. /**
  237. * Get the value of the cell as a number.
  238. * <p>
  239. * For strings we throw an exception. For blank cells we return a 0.
  240. * For formulas or error cells we return the precalculated value;
  241. * </p>
  242. * @return the value of the cell as a number
  243. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is {@link CellType#STRING}
  244. * @throws NumberFormatException if the cell value isn't a parsable <code>double</code>.
  245. * @see DataFormatter for turning this number into a string similar to that which Excel would render this number as.
  246. */
  247. double getNumericCellValue();
  248. /**
  249. * Get the value of the cell as a date.
  250. * <p>
  251. * For strings we throw an exception. For blank cells we return a null.
  252. * </p>
  253. * @return the value of the cell as a date
  254. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is {@link CellType#STRING}
  255. * @throws NumberFormatException if the cell value isn't a parsable <code>double</code>.
  256. * @see DataFormatter for formatting this date into a string similar to how excel does.
  257. */
  258. Date getDateCellValue();
  259. /**
  260. * Get the value of the cell as a LocalDateTime.
  261. * <p>
  262. * For strings we throw an exception. For blank cells we return a null.
  263. * </p>
  264. * @return the value of the cell as a LocalDateTime
  265. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is {@link CellType#STRING}
  266. * @throws NumberFormatException if the cell value isn't a parsable <code>double</code>.
  267. * @see DataFormatter for formatting this date into a string similar to how excel does.
  268. */
  269. LocalDateTime getLocalDateTimeCellValue();
  270. /**
  271. * Get the value of the cell as a XSSFRichTextString
  272. * <p>
  273. * For numeric cells we throw an exception. For blank cells we return an empty string.
  274. * For formula cells we return the pre-calculated value if a string, otherwise an exception.
  275. * </p>
  276. * @return the value of the cell as a XSSFRichTextString
  277. */
  278. RichTextString getRichStringCellValue();
  279. /**
  280. * Get the value of the cell as a string
  281. * <p>
  282. * For numeric cells we throw an exception. For blank cells we return an empty string.
  283. * For formulaCells that are not string Formulas, we throw an exception.
  284. * </p>
  285. * @return the value of the cell as a string
  286. */
  287. String getStringCellValue();
  288. /**
  289. * Set a boolean value for the cell
  290. *
  291. * @param value the boolean value to set this cell to. For formulas, we'll set the
  292. * precalculated value, for booleans we'll set its value. For other types, we
  293. * will change the cell to a boolean cell and set its value.
  294. */
  295. void setCellValue(boolean value);
  296. /**
  297. * Set a error value for the cell
  298. *
  299. * @param value the error value to set this cell to. For formulas, we'll set the
  300. * precalculated value , for errors we'll set
  301. * its value. For other types, we will change the cell to an error
  302. * cell and set its value.
  303. * @see FormulaError
  304. */
  305. void setCellErrorValue(byte value);
  306. /**
  307. * Get the value of the cell as a boolean.
  308. * <p>
  309. * For strings, numbers, and errors, we throw an exception. For blank cells we return a false.
  310. * </p>
  311. * @return the value of the cell as a boolean
  312. * @throws IllegalStateException if the cell type returned by {@link #getCellType()}
  313. * is not {@link CellType#BOOLEAN}, {@link CellType#BLANK} or {@link CellType#FORMULA}
  314. */
  315. boolean getBooleanCellValue();
  316. /**
  317. * Get the value of the cell as an error code.
  318. * <p>
  319. * For strings, numbers, and booleans, we throw an exception.
  320. * For blank cells we return a 0.
  321. * </p>
  322. *
  323. * @return the value of the cell as an error code
  324. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't {@link CellType#ERROR}
  325. * @see FormulaError for error codes
  326. */
  327. byte getErrorCellValue();
  328. /**
  329. * <p>Set the style for the cell. The style should be an CellStyle created/retrieved from
  330. * the Workbook.</p>
  331. *
  332. * <p>To change the style of a cell without affecting other cells that use the same style,
  333. * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}</p>
  334. *
  335. * @param style reference contained in the workbook.
  336. * If the value is null then the style information is removed causing the cell to used the default workbook style.
  337. * @see org.apache.poi.ss.usermodel.Workbook#createCellStyle()
  338. */
  339. void setCellStyle(CellStyle style);
  340. /**
  341. * Return the cell's style.
  342. *
  343. * @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as
  344. * <code>workbook.getCellStyleAt(0)</code>
  345. * @see Workbook#getCellStyleAt(int)
  346. */
  347. CellStyle getCellStyle();
  348. /**
  349. * Sets this cell as the active cell for the worksheet
  350. */
  351. void setAsActiveCell();
  352. /**
  353. * Gets the address of this cell
  354. *
  355. * @return <code>A1</code> style address of this cell
  356. * @since 3.14beta1
  357. */
  358. CellAddress getAddress();
  359. /**
  360. * Assign a comment to this cell
  361. *
  362. * @param comment comment associated with this cell
  363. */
  364. void setCellComment(Comment comment);
  365. /**
  366. * Returns comment associated with this cell
  367. *
  368. * @return comment associated with this cell or <code>null</code> if not found
  369. */
  370. Comment getCellComment();
  371. /**
  372. * Removes the comment for this cell, if there is one.
  373. */
  374. void removeCellComment();
  375. /**
  376. * @return hyperlink associated with this cell or <code>null</code> if not found
  377. */
  378. Hyperlink getHyperlink();
  379. /**
  380. * Assign a hyperlink to this cell
  381. *
  382. * @param link hyperlink associated with this cell
  383. */
  384. void setHyperlink(Hyperlink link);
  385. /**
  386. * Removes the hyperlink for this cell, if there is one.
  387. */
  388. void removeHyperlink();
  389. /**
  390. * Only valid for array formula cells
  391. *
  392. * @return range of the array formula group that the cell belongs to.
  393. */
  394. CellRangeAddress getArrayFormulaRange();
  395. /**
  396. * @return <code>true</code> if this cell is part of group of cells having a common array formula.
  397. */
  398. boolean isPartOfArrayFormulaGroup();
  399. }