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.

SXSSFCell.java 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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.streaming;
  16. import java.text.DateFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.time.LocalDateTime;
  19. import java.util.Calendar;
  20. import java.util.Date;
  21. import java.util.Map;
  22. import org.apache.poi.ss.SpreadsheetVersion;
  23. import org.apache.poi.ss.formula.FormulaParseException;
  24. import org.apache.poi.ss.formula.eval.ErrorEval;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.CellBase;
  27. import org.apache.poi.ss.usermodel.CellStyle;
  28. import org.apache.poi.ss.usermodel.CellType;
  29. import org.apache.poi.ss.usermodel.Comment;
  30. import org.apache.poi.ss.usermodel.DateUtil;
  31. import org.apache.poi.ss.usermodel.FormulaError;
  32. import org.apache.poi.ss.usermodel.Hyperlink;
  33. import org.apache.poi.ss.usermodel.RichTextString;
  34. import org.apache.poi.ss.usermodel.Row;
  35. import org.apache.poi.ss.util.CellRangeAddress;
  36. import org.apache.poi.ss.util.CellReference;
  37. import org.apache.poi.util.*;
  38. import org.apache.poi.xssf.usermodel.XSSFHyperlink;
  39. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  40. /**
  41. * Streaming version of XSSFCell implementing the "BigGridDemo" strategy.
  42. */
  43. public class SXSSFCell extends CellBase {
  44. private final SXSSFRow _row;
  45. private Value _value;
  46. private CellStyle _style;
  47. private Property _firstProperty;
  48. public SXSSFCell(SXSSFRow row, CellType cellType)
  49. {
  50. _row=row;
  51. _value = new BlankValue();
  52. setType(cellType);
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. @Override
  58. protected SpreadsheetVersion getSpreadsheetVersion() {
  59. return SpreadsheetVersion.EXCEL2007;
  60. }
  61. /**
  62. * Returns column index of this cell
  63. *
  64. * @return zero-based column index of a column in a sheet.
  65. */
  66. @Override
  67. public int getColumnIndex()
  68. {
  69. return _row.getCellIndex(this);
  70. }
  71. /**
  72. * Returns row index of a row in the sheet that contains this cell
  73. *
  74. * @return zero-based row index of a row in the sheet that contains this cell
  75. */
  76. @Override
  77. public int getRowIndex()
  78. {
  79. return _row.getRowNum();
  80. }
  81. /**
  82. * Returns the sheet this cell belongs to
  83. *
  84. * @return the sheet this cell belongs to
  85. */
  86. @Override
  87. public SXSSFSheet getSheet()
  88. {
  89. return _row.getSheet();
  90. }
  91. /**
  92. * Returns the Row this cell belongs to
  93. *
  94. * @return the Row that owns this cell
  95. */
  96. @Override
  97. public Row getRow()
  98. {
  99. return _row;
  100. }
  101. @Override
  102. protected void setCellTypeImpl(CellType cellType) {
  103. ensureType(cellType);
  104. }
  105. private boolean isFormulaCell() {
  106. return _value instanceof FormulaValue;
  107. }
  108. /**
  109. * Return the cell type.
  110. *
  111. * @return the cell type
  112. */
  113. @Override
  114. public CellType getCellType() {
  115. if (isFormulaCell()) {
  116. return CellType.FORMULA;
  117. }
  118. return _value.getType();
  119. }
  120. /**
  121. * Only valid for formula cells
  122. * @return one of ({@link CellType#NUMERIC}, {@link CellType#STRING},
  123. * {@link CellType#BOOLEAN}, {@link CellType#ERROR}) depending
  124. * on the cached value of the formula
  125. */
  126. @Override
  127. public CellType getCachedFormulaResultType() {
  128. if (!isFormulaCell()) {
  129. throw new IllegalStateException("Only formula cells have cached results");
  130. }
  131. return ((FormulaValue)_value).getFormulaType();
  132. }
  133. /**
  134. * {@inheritDoc}
  135. */
  136. @Override
  137. public void setCellValueImpl(double value) {
  138. ensureTypeOrFormulaType(CellType.NUMERIC);
  139. if(_value.getType() == CellType.FORMULA) {
  140. ((NumericFormulaValue) _value).setPreEvaluatedValue(value);
  141. } else {
  142. ((NumericValue)_value).setValue(value);
  143. }
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. @Override
  149. protected void setCellValueImpl(Date value) {
  150. boolean date1904 = getSheet().getWorkbook().isDate1904();
  151. setCellValue(DateUtil.getExcelDate(value, date1904));
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. @Override
  157. protected void setCellValueImpl(LocalDateTime value) {
  158. boolean date1904 = getSheet().getWorkbook().isDate1904();
  159. setCellValue(DateUtil.getExcelDate(value, date1904));
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. @Override
  165. protected void setCellValueImpl(Calendar value) {
  166. boolean date1904 = getSheet().getWorkbook().isDate1904();
  167. setCellValue( DateUtil.getExcelDate(value, date1904 ));
  168. }
  169. /**
  170. * {@inheritDoc}
  171. */
  172. @Override
  173. protected void setCellValueImpl(RichTextString value) {
  174. ensureRichTextStringType();
  175. if(_value instanceof RichTextStringFormulaValue) {
  176. ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(value);
  177. } else {
  178. ((RichTextValue) _value).setValue(value);
  179. }
  180. }
  181. /**
  182. * {@inheritDoc}
  183. */
  184. @Override
  185. protected void setCellValueImpl(String value) {
  186. ensureTypeOrFormulaType(CellType.STRING);
  187. if(_value.getType() == CellType.FORMULA) {
  188. ((StringFormulaValue) _value).setPreEvaluatedValue(value);
  189. } else {
  190. ((PlainStringValue) _value).setValue(value);
  191. }
  192. }
  193. /**
  194. * Sets formula for this cell.
  195. * <p>
  196. * Note, this method only sets the formula string and does not calculate the formula value.
  197. * To set the precalculated value use {@link #setCellValue(double)} or {@link #setCellValue(String)}
  198. * </p>
  199. *
  200. * @param formula the formula to set, e.g. <code>"SUM(C4:E4)"</code>.
  201. * If the argument is <code>null</code> then the current formula is removed.
  202. * @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
  203. */
  204. @Override
  205. public void setCellFormulaImpl(String formula) throws FormulaParseException {
  206. assert formula != null;
  207. if (getCellType() == CellType.FORMULA) {
  208. ((FormulaValue)_value).setValue(formula);
  209. } else {
  210. switch (getCellType()) {
  211. case BLANK:
  212. case NUMERIC:
  213. _value = new NumericFormulaValue(formula, getNumericCellValue());
  214. break;
  215. case STRING:
  216. if (_value instanceof PlainStringValue) {
  217. _value = new StringFormulaValue(formula, getStringCellValue());
  218. } else {
  219. assert(_value instanceof RichTextValue);
  220. _value = new RichTextStringFormulaValue(formula, ((RichTextValue) _value).getValue());
  221. }
  222. break;
  223. case BOOLEAN:
  224. _value = new BooleanFormulaValue(formula, getBooleanCellValue());
  225. break;
  226. case ERROR:
  227. _value = new ErrorFormulaValue(formula, getErrorCellValue());
  228. break;
  229. default:
  230. throw new IllegalStateException("Cannot set a formula for a cell of type " + getCellType());
  231. }
  232. }
  233. }
  234. @Override
  235. protected void removeFormulaImpl() {
  236. assert getCellType() == CellType.FORMULA;
  237. switch (getCachedFormulaResultType()) {
  238. case NUMERIC:
  239. double numericValue = ((NumericFormulaValue)_value).getPreEvaluatedValue();
  240. _value = new NumericValue();
  241. ((NumericValue) _value).setValue(numericValue);
  242. break;
  243. case STRING:
  244. String stringValue = ((StringFormulaValue)_value).getPreEvaluatedValue();
  245. _value = new PlainStringValue();
  246. ((PlainStringValue) _value).setValue(stringValue);
  247. break;
  248. case BOOLEAN:
  249. boolean booleanValue = ((BooleanFormulaValue)_value).getPreEvaluatedValue();
  250. _value = new BooleanValue();
  251. ((BooleanValue) _value).setValue(booleanValue);
  252. break;
  253. case ERROR:
  254. byte errorValue = ((ErrorFormulaValue)_value).getPreEvaluatedValue();
  255. _value = new ErrorValue();
  256. ((ErrorValue) _value).setValue(errorValue);
  257. break;
  258. default:
  259. throw new AssertionError();
  260. }
  261. }
  262. /**
  263. * Return a formula for the cell, for example, <code>SUM(C4:E4)</code>
  264. *
  265. * @return a formula for the cell
  266. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is not CellType.FORMULA
  267. */
  268. @Override
  269. public String getCellFormula()
  270. {
  271. if(_value.getType()!=CellType.FORMULA)
  272. throw typeMismatch(CellType.FORMULA,_value.getType(),false);
  273. return ((FormulaValue)_value).getValue();
  274. }
  275. /**
  276. * Get the value of the cell as a number.
  277. * <p>
  278. * For strings we throw an exception. For blank cells we return a 0.
  279. * For formulas or error cells we return the precalculated value;
  280. * </p>
  281. * @return the value of the cell as a number
  282. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CellType.STRING
  283. * @throws NumberFormatException if the cell value isn't a parsable <code>double</code>.
  284. * @see org.apache.poi.ss.usermodel.DataFormatter for turning this number into a string similar to that which Excel would render this number as.
  285. */
  286. @Override
  287. public double getNumericCellValue()
  288. {
  289. CellType cellType = getCellType();
  290. switch(cellType)
  291. {
  292. case BLANK:
  293. return 0.0;
  294. case FORMULA:
  295. {
  296. FormulaValue fv=(FormulaValue)_value;
  297. if(fv.getFormulaType()!=CellType.NUMERIC)
  298. throw typeMismatch(CellType.NUMERIC, CellType.FORMULA, false);
  299. return ((NumericFormulaValue)_value).getPreEvaluatedValue();
  300. }
  301. case NUMERIC:
  302. return ((NumericValue)_value).getValue();
  303. default:
  304. throw typeMismatch(CellType.NUMERIC, cellType, false);
  305. }
  306. }
  307. /**
  308. * Get the value of the cell as a date.
  309. * <p>
  310. * For strings we throw an exception. For blank cells we return a null.
  311. * </p>
  312. * @return the value of the cell as a date
  313. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CellType.STRING
  314. * @throws NumberFormatException if the cell value isn't a parsable <code>double</code>.
  315. * @see org.apache.poi.ss.usermodel.DataFormatter for formatting this date into a string similar to how excel does.
  316. */
  317. @Override
  318. public Date getDateCellValue()
  319. {
  320. CellType cellType = getCellType();
  321. if (cellType == CellType.BLANK)
  322. {
  323. return null;
  324. }
  325. double value = getNumericCellValue();
  326. boolean date1904 = getSheet().getWorkbook().isDate1904();
  327. return DateUtil.getJavaDate(value, date1904);
  328. }
  329. /**
  330. * Get the value of the cell as a LocalDateTime.
  331. * <p>
  332. * For strings we throw an exception. For blank cells we return a null.
  333. * </p>
  334. * @return the value of the cell as a date
  335. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} is CellType.STRING
  336. * @throws NumberFormatException if the cell value isn't a parsable <code>double</code>.
  337. * @see org.apache.poi.ss.usermodel.DataFormatter for formatting this date into a string similar to how excel does.
  338. */
  339. @Override
  340. public LocalDateTime getLocalDateTimeCellValue() {
  341. if (getCellType() == CellType.BLANK) {
  342. return null;
  343. }
  344. double value = getNumericCellValue();
  345. boolean date1904 = getSheet().getWorkbook().isDate1904();
  346. return DateUtil.getLocalDateTime(value, date1904);
  347. }
  348. /**
  349. * Get the value of the cell as a XSSFRichTextString
  350. * <p>
  351. * For numeric cells we throw an exception. For blank cells we return an empty string.
  352. * For formula cells we return the pre-calculated value if a string, otherwise an exception.
  353. * </p>
  354. * @return the value of the cell as a XSSFRichTextString
  355. */
  356. @Override
  357. public RichTextString getRichStringCellValue()
  358. {
  359. CellType cellType = getCellType();
  360. if(getCellType() != CellType.STRING)
  361. throw typeMismatch(CellType.STRING, cellType, false);
  362. StringValue sval = (StringValue)_value;
  363. if(sval.isRichText())
  364. return ((RichTextValue)_value).getValue();
  365. else {
  366. String plainText = getStringCellValue();
  367. return getSheet().getWorkbook().getCreationHelper().createRichTextString(plainText);
  368. }
  369. }
  370. /**
  371. * Get the value of the cell as a string
  372. * <p>
  373. * For numeric cells we throw an exception. For blank cells we return an empty string.
  374. * For formulaCells that are not string Formulas, we throw an exception.
  375. * </p>
  376. * @return the value of the cell as a string
  377. */
  378. @Override
  379. public String getStringCellValue()
  380. {
  381. CellType cellType = getCellType();
  382. switch(cellType)
  383. {
  384. case BLANK:
  385. return "";
  386. case FORMULA:
  387. {
  388. FormulaValue fv=(FormulaValue)_value;
  389. if(fv.getFormulaType()!=CellType.STRING)
  390. throw typeMismatch(CellType.STRING, CellType.FORMULA, false);
  391. if(_value instanceof RichTextStringFormulaValue) {
  392. return ((RichTextStringFormulaValue) _value).getPreEvaluatedValue().getString();
  393. } else {
  394. return ((StringFormulaValue) _value).getPreEvaluatedValue();
  395. }
  396. }
  397. case STRING:
  398. {
  399. if(((StringValue)_value).isRichText())
  400. return ((RichTextValue)_value).getValue().getString();
  401. else
  402. return ((PlainStringValue)_value).getValue();
  403. }
  404. default:
  405. throw typeMismatch(CellType.STRING, cellType, false);
  406. }
  407. }
  408. /**
  409. * Set a boolean value for the cell
  410. *
  411. * @param value the boolean value to set this cell to. For formulas, we'll set the
  412. * precalculated value, for booleans we'll set its value. For other types, we
  413. * will change the cell to a boolean cell and set its value.
  414. */
  415. @Override
  416. public void setCellValue(boolean value)
  417. {
  418. ensureTypeOrFormulaType(CellType.BOOLEAN);
  419. if(_value.getType()==CellType.FORMULA)
  420. ((BooleanFormulaValue)_value).setPreEvaluatedValue(value);
  421. else
  422. ((BooleanValue)_value).setValue(value);
  423. }
  424. /**
  425. * Set a error value for the cell
  426. *
  427. * @param value the error value to set this cell to. For formulas, we'll set the
  428. * precalculated value , for errors we'll set
  429. * its value. For other types, we will change the cell to an error
  430. * cell and set its value.
  431. * @see org.apache.poi.ss.usermodel.FormulaError
  432. */
  433. @Override
  434. public void setCellErrorValue(byte value) {
  435. // for formulas, we want to keep the type and only have an ERROR as formula value
  436. if(_value.getType()==CellType.FORMULA) {
  437. _value = new ErrorFormulaValue(getCellFormula(), value);
  438. } else {
  439. _value = new ErrorValue(value);
  440. }
  441. }
  442. /**
  443. * Get the value of the cell as a boolean.
  444. * <p>
  445. * For strings, numbers, and errors, we throw an exception. For blank cells we return a false.
  446. * </p>
  447. * @return the value of the cell as a boolean
  448. * @throws IllegalStateException if the cell type returned by {@link #getCellType()}
  449. * is not CellType.BOOLEAN, CellType.BLANK or CellType.FORMULA
  450. */
  451. @Override
  452. public boolean getBooleanCellValue()
  453. {
  454. CellType cellType = getCellType();
  455. switch(cellType)
  456. {
  457. case BLANK:
  458. return false;
  459. case FORMULA:
  460. {
  461. FormulaValue fv=(FormulaValue)_value;
  462. if(fv.getFormulaType()!=CellType.BOOLEAN)
  463. throw typeMismatch(CellType.BOOLEAN, CellType.FORMULA, false);
  464. return ((BooleanFormulaValue)_value).getPreEvaluatedValue();
  465. }
  466. case BOOLEAN:
  467. {
  468. return ((BooleanValue)_value).getValue();
  469. }
  470. default:
  471. throw typeMismatch(CellType.BOOLEAN, cellType, false);
  472. }
  473. }
  474. /**
  475. * Get the value of the cell as an error code.
  476. * <p>
  477. * For strings, numbers, and booleans, we throw an exception.
  478. * For blank cells we return a 0.
  479. * </p>
  480. *
  481. * @return the value of the cell as an error code
  482. * @throws IllegalStateException if the cell type returned by {@link #getCellType()} isn't CellType.ERROR
  483. * @see org.apache.poi.ss.usermodel.FormulaError for error codes
  484. */
  485. @Override
  486. public byte getErrorCellValue()
  487. {
  488. CellType cellType = getCellType();
  489. switch(cellType)
  490. {
  491. case BLANK:
  492. return 0;
  493. case FORMULA:
  494. {
  495. FormulaValue fv=(FormulaValue)_value;
  496. if(fv.getFormulaType()!=CellType.ERROR)
  497. throw typeMismatch(CellType.ERROR, CellType.FORMULA, false);
  498. return ((ErrorFormulaValue)_value).getPreEvaluatedValue();
  499. }
  500. case ERROR:
  501. {
  502. return ((ErrorValue)_value).getValue();
  503. }
  504. default:
  505. throw typeMismatch(CellType.ERROR, cellType, false);
  506. }
  507. }
  508. /**
  509. * <p>Set the style for the cell. The style should be an CellStyle created/retrieved from
  510. * the Workbook.</p>
  511. *
  512. * <p>To change the style of a cell without affecting other cells that use the same style,
  513. * use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperties(Cell, Map)}</p>
  514. *
  515. * @param style reference contained in the workbook.
  516. * If the value is null then the style information is removed causing the cell to used the default workbook style.
  517. * @see org.apache.poi.ss.usermodel.Workbook#createCellStyle
  518. */
  519. @Override
  520. public void setCellStyle(CellStyle style)
  521. {
  522. _style=style;
  523. }
  524. /**
  525. * Return the cell's style.
  526. *
  527. * @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as
  528. * <code>workbook.getCellStyleAt(0)</code>
  529. * @see org.apache.poi.ss.usermodel.Workbook#getCellStyleAt(int)
  530. */
  531. @Override
  532. public CellStyle getCellStyle()
  533. {
  534. if(_style == null){
  535. SXSSFWorkbook wb = (SXSSFWorkbook)getRow().getSheet().getWorkbook();
  536. return wb.getCellStyleAt(0);
  537. } else {
  538. return _style;
  539. }
  540. }
  541. /**
  542. * {@inheritDoc}
  543. */
  544. @Override
  545. public void setAsActiveCell()
  546. {
  547. getSheet().setActiveCell(getAddress());
  548. }
  549. /**
  550. * Assign a comment to this cell
  551. *
  552. * @param comment comment associated with this cell
  553. */
  554. @Override
  555. public void setCellComment(Comment comment)
  556. {
  557. setProperty(Property.COMMENT,comment);
  558. }
  559. /**
  560. * Returns comment associated with this cell
  561. *
  562. * @return comment associated with this cell or <code>null</code> if not found
  563. */
  564. @Override
  565. public Comment getCellComment()
  566. {
  567. return (Comment)getPropertyValue(Property.COMMENT);
  568. }
  569. /**
  570. * Removes the comment for this cell, if there is one.
  571. */
  572. @Override
  573. public void removeCellComment()
  574. {
  575. removeProperty(Property.COMMENT);
  576. }
  577. /**
  578. * @return hyperlink associated with this cell or <code>null</code> if not found
  579. */
  580. @Override
  581. public Hyperlink getHyperlink()
  582. {
  583. return (Hyperlink)getPropertyValue(Property.HYPERLINK);
  584. }
  585. /**
  586. * Assign a hyperlink to this cell. If the supplied hyperlink is null, the
  587. * hyperlink for this cell will be removed.
  588. *
  589. * @param link hyperlink associated with this cell
  590. */
  591. @Override
  592. public void setHyperlink(Hyperlink link)
  593. {
  594. if (link == null) {
  595. removeHyperlink();
  596. return;
  597. }
  598. setProperty(Property.HYPERLINK,link);
  599. XSSFHyperlink xssfobj = (XSSFHyperlink)link;
  600. // Assign to us
  601. CellReference ref = new CellReference(getRowIndex(), getColumnIndex());
  602. xssfobj.setCellReference( ref );
  603. // Add to the lists
  604. getSheet()._sh.addHyperlink(xssfobj);
  605. }
  606. /**
  607. * Removes the hyperlink for this cell, if there is one.
  608. */
  609. @Override
  610. public void removeHyperlink()
  611. {
  612. removeProperty(Property.HYPERLINK);
  613. getSheet()._sh.removeHyperlink(getRowIndex(), getColumnIndex());
  614. }
  615. /**
  616. * Only valid for array formula cells
  617. *
  618. * @return range of the array formula group that the cell belongs to.
  619. */
  620. // TODO: What is this?
  621. @NotImplemented
  622. public CellRangeAddress getArrayFormulaRange()
  623. {
  624. return null;
  625. }
  626. /**
  627. * @return <code>true</code> if this cell is part of group of cells having a common array formula.
  628. */
  629. //TODO: What is this?
  630. @NotImplemented
  631. public boolean isPartOfArrayFormulaGroup()
  632. {
  633. return false;
  634. }
  635. //end of interface implementation
  636. /**
  637. * Returns a string representation of the cell
  638. * <p>
  639. * Formula cells return the formula string, rather than the formula result.
  640. * Dates are displayed in dd-MMM-yyyy format
  641. * Errors are displayed as #ERR&lt;errIdx&gt;
  642. * </p>
  643. */
  644. @Override
  645. public String toString() {
  646. switch (getCellType()) {
  647. case BLANK:
  648. return "";
  649. case BOOLEAN:
  650. return getBooleanCellValue() ? "TRUE" : "FALSE";
  651. case ERROR:
  652. return ErrorEval.getText(getErrorCellValue());
  653. case FORMULA:
  654. return getCellFormula();
  655. case NUMERIC:
  656. if (DateUtil.isCellDateFormatted(this)) {
  657. DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
  658. sdf.setTimeZone(LocaleUtil.getUserTimeZone());
  659. return sdf.format(getDateCellValue());
  660. }
  661. return getNumericCellValue() + "";
  662. case STRING:
  663. return getRichStringCellValue().toString();
  664. default:
  665. return "Unknown Cell Type: " + getCellType();
  666. }
  667. }
  668. /*package*/ void removeProperty(int type)
  669. {
  670. Property current=_firstProperty;
  671. Property previous=null;
  672. while(current!=null&&current.getType()!=type)
  673. {
  674. previous=current;
  675. current=current._next;
  676. }
  677. if(current!=null)
  678. {
  679. if(previous!=null)
  680. {
  681. previous._next=current._next;
  682. }
  683. else
  684. {
  685. _firstProperty=current._next;
  686. }
  687. }
  688. }
  689. /*package*/ void setProperty(int type,Object value)
  690. {
  691. Property current=_firstProperty;
  692. Property previous=null;
  693. while(current!=null&&current.getType()!=type)
  694. {
  695. previous=current;
  696. current=current._next;
  697. }
  698. if(current!=null)
  699. {
  700. current.setValue(value);
  701. }
  702. else
  703. {
  704. switch(type)
  705. {
  706. case Property.COMMENT:
  707. {
  708. current=new CommentProperty(value);
  709. break;
  710. }
  711. case Property.HYPERLINK:
  712. {
  713. current=new HyperlinkProperty(value);
  714. break;
  715. }
  716. default:
  717. {
  718. throw new IllegalArgumentException("Invalid type: " + type);
  719. }
  720. }
  721. if(previous!=null)
  722. {
  723. previous._next=current;
  724. }
  725. else
  726. {
  727. _firstProperty=current;
  728. }
  729. }
  730. }
  731. /*package*/ Object getPropertyValue(int type)
  732. {
  733. return getPropertyValue(type,null);
  734. }
  735. /*package*/ Object getPropertyValue(int type,String defaultValue)
  736. {
  737. Property current=_firstProperty;
  738. while(current!=null&&current.getType()!=type) current=current._next;
  739. return current==null?defaultValue:current.getValue();
  740. }
  741. /*package*/ void ensurePlainStringType()
  742. {
  743. if(_value.getType()!=CellType.STRING
  744. ||((StringValue)_value).isRichText())
  745. _value = new PlainStringValue();
  746. }
  747. /*package*/ void ensureRichTextStringType()
  748. {
  749. // don't change cell type for formulas
  750. if(_value.getType() == CellType.FORMULA) {
  751. String formula = ((FormulaValue)_value).getValue();
  752. _value = new RichTextStringFormulaValue(formula, new XSSFRichTextString(""));
  753. } else if(_value.getType()!=CellType.STRING ||
  754. !((StringValue)_value).isRichText()) {
  755. _value = new RichTextValue();
  756. }
  757. }
  758. /*package*/ void ensureType(CellType type)
  759. {
  760. if(_value.getType()!=type)
  761. setType(type);
  762. }
  763. /*
  764. * Sets the cell type to type if it is different
  765. */
  766. /*package*/ void ensureTypeOrFormulaType(CellType type)
  767. {
  768. if(_value.getType()==type)
  769. {
  770. if(type==CellType.STRING&&((StringValue)_value).isRichText())
  771. setType(CellType.STRING);
  772. return;
  773. }
  774. if(_value.getType()==CellType.FORMULA)
  775. {
  776. if(((FormulaValue)_value).getFormulaType()==type)
  777. return;
  778. switch (type) {
  779. case BOOLEAN:
  780. _value = new BooleanFormulaValue(getCellFormula(), false);
  781. break;
  782. case NUMERIC:
  783. _value = new NumericFormulaValue(getCellFormula(), 0);
  784. break;
  785. case STRING:
  786. _value = new StringFormulaValue(getCellFormula(), "");
  787. break;
  788. case ERROR:
  789. _value = new ErrorFormulaValue(getCellFormula(), FormulaError._NO_ERROR.getCode());
  790. break;
  791. default:
  792. throw new AssertionError();
  793. }
  794. return;
  795. }
  796. setType(type);
  797. }
  798. /**
  799. * changes the cell type to the specified type, and resets the value to the default value for that type
  800. * If cell type is the same as specified type, this will reset the value to the default value for that type
  801. *
  802. * @param type the cell type to set
  803. * @throws IllegalArgumentException if type is not a recognized type
  804. */
  805. /*package*/ void setType(CellType type)
  806. {
  807. switch(type)
  808. {
  809. case NUMERIC:
  810. {
  811. _value = new NumericValue();
  812. break;
  813. }
  814. case STRING:
  815. {
  816. PlainStringValue sval = new PlainStringValue();
  817. if(_value != null){
  818. // if a cell is not blank then convert the old value to string
  819. String str = convertCellValueToString();
  820. sval.setValue(str);
  821. }
  822. _value = sval;
  823. break;
  824. }
  825. case FORMULA:
  826. {
  827. if (getCellType() == CellType.BLANK) {
  828. _value = new NumericFormulaValue("", 0);
  829. }
  830. break;
  831. }
  832. case BLANK:
  833. {
  834. _value = new BlankValue();
  835. break;
  836. }
  837. case BOOLEAN:
  838. {
  839. BooleanValue bval = new BooleanValue();
  840. if(_value != null){
  841. // if a cell is not blank then convert the old value to string
  842. boolean val = convertCellValueToBoolean();
  843. bval.setValue(val);
  844. }
  845. _value = bval;
  846. break;
  847. }
  848. case ERROR:
  849. {
  850. _value = new ErrorValue();
  851. break;
  852. }
  853. default:
  854. {
  855. throw new IllegalArgumentException("Illegal type " + type);
  856. }
  857. }
  858. }
  859. //COPIED FROM https://svn.apache.org/repos/asf/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java since the functions are declared private there
  860. /**
  861. * Used to help format error messages
  862. */
  863. private static IllegalStateException typeMismatch(CellType expectedTypeCode, CellType actualTypeCode, boolean isFormulaCell) {
  864. String msg = "Cannot get a " + expectedTypeCode + " value from a " + actualTypeCode
  865. + " " + (isFormulaCell ? "formula " : "") + "cell";
  866. return new IllegalStateException(msg);
  867. }
  868. private boolean convertCellValueToBoolean() {
  869. CellType cellType = getCellType();
  870. if (cellType == CellType.FORMULA) {
  871. cellType = getCachedFormulaResultType();
  872. }
  873. switch (cellType) {
  874. case BOOLEAN:
  875. return getBooleanCellValue();
  876. case STRING:
  877. String text = getStringCellValue();
  878. return Boolean.parseBoolean(text);
  879. case NUMERIC:
  880. return getNumericCellValue() != 0;
  881. case ERROR:
  882. case BLANK:
  883. return false;
  884. default:
  885. throw new IllegalStateException("Unexpected cell type (" + cellType + ")");
  886. }
  887. }
  888. private String convertCellValueToString() {
  889. CellType cellType = getCellType();
  890. return convertCellValueToString(cellType);
  891. }
  892. private String convertCellValueToString(CellType cellType) {
  893. switch (cellType) {
  894. case BLANK:
  895. return "";
  896. case BOOLEAN:
  897. return getBooleanCellValue() ? "TRUE" : "FALSE";
  898. case STRING:
  899. return getStringCellValue();
  900. case NUMERIC:
  901. return Double.toString( getNumericCellValue() );
  902. case ERROR:
  903. byte errVal = getErrorCellValue();
  904. return FormulaError.forInt(errVal).getString();
  905. case FORMULA:
  906. if (_value != null) {
  907. FormulaValue fv = (FormulaValue)_value;
  908. if (fv.getFormulaType() != CellType.FORMULA) {
  909. return convertCellValueToString(fv.getFormulaType());
  910. }
  911. }
  912. return "";
  913. default:
  914. throw new IllegalStateException("Unexpected cell type (" + cellType + ")");
  915. }
  916. }
  917. //END OF COPIED CODE
  918. static abstract class Property
  919. {
  920. static final int COMMENT=1;
  921. static final int HYPERLINK=2;
  922. Object _value;
  923. Property _next;
  924. public Property(Object value)
  925. {
  926. _value = value;
  927. }
  928. abstract int getType();
  929. void setValue(Object value)
  930. {
  931. _value = value;
  932. }
  933. Object getValue()
  934. {
  935. return _value;
  936. }
  937. }
  938. static class CommentProperty extends Property
  939. {
  940. public CommentProperty(Object value)
  941. {
  942. super(value);
  943. }
  944. @Override
  945. public int getType()
  946. {
  947. return COMMENT;
  948. }
  949. }
  950. static class HyperlinkProperty extends Property
  951. {
  952. public HyperlinkProperty(Object value)
  953. {
  954. super(value);
  955. }
  956. @Override
  957. public int getType()
  958. {
  959. return HYPERLINK;
  960. }
  961. }
  962. interface Value
  963. {
  964. CellType getType();
  965. }
  966. static class NumericValue implements Value {
  967. double _value;
  968. public NumericValue() {
  969. _value = 0;
  970. }
  971. public NumericValue(double _value) {
  972. this._value = _value;
  973. }
  974. public CellType getType()
  975. {
  976. return CellType.NUMERIC;
  977. }
  978. void setValue(double value)
  979. {
  980. _value = value;
  981. }
  982. double getValue()
  983. {
  984. return _value;
  985. }
  986. }
  987. static abstract class StringValue implements Value
  988. {
  989. public CellType getType()
  990. {
  991. return CellType.STRING;
  992. }
  993. //We cannot introduce a new type CellType.RICH_TEXT because the types are public so we have to make rich text as a type of string
  994. abstract boolean isRichText(); // using the POI style which seems to avoid "instanceof".
  995. }
  996. static class PlainStringValue extends StringValue
  997. {
  998. String _value;
  999. void setValue(String value)
  1000. {
  1001. _value = value;
  1002. }
  1003. String getValue()
  1004. {
  1005. return _value;
  1006. }
  1007. @Override
  1008. boolean isRichText()
  1009. {
  1010. return false;
  1011. }
  1012. }
  1013. static class RichTextValue extends StringValue
  1014. {
  1015. RichTextString _value;
  1016. @Override
  1017. public CellType getType()
  1018. {
  1019. return CellType.STRING;
  1020. }
  1021. void setValue(RichTextString value)
  1022. {
  1023. _value = value;
  1024. }
  1025. RichTextString getValue()
  1026. {
  1027. return _value;
  1028. }
  1029. @Override
  1030. boolean isRichText()
  1031. {
  1032. return true;
  1033. }
  1034. }
  1035. static abstract class FormulaValue implements Value
  1036. {
  1037. String _value;
  1038. public FormulaValue(String _value) {
  1039. this._value = _value;
  1040. }
  1041. public CellType getType()
  1042. {
  1043. return CellType.FORMULA;
  1044. }
  1045. void setValue(String value)
  1046. {
  1047. _value = value;
  1048. }
  1049. String getValue()
  1050. {
  1051. return _value;
  1052. }
  1053. abstract CellType getFormulaType();
  1054. }
  1055. static class NumericFormulaValue extends FormulaValue {
  1056. double _preEvaluatedValue;
  1057. public NumericFormulaValue(String formula, double _preEvaluatedValue) {
  1058. super(formula);
  1059. this._preEvaluatedValue = _preEvaluatedValue;
  1060. }
  1061. @Override
  1062. CellType getFormulaType()
  1063. {
  1064. return CellType.NUMERIC;
  1065. }
  1066. void setPreEvaluatedValue(double value)
  1067. {
  1068. _preEvaluatedValue=value;
  1069. }
  1070. double getPreEvaluatedValue()
  1071. {
  1072. return _preEvaluatedValue;
  1073. }
  1074. }
  1075. static class StringFormulaValue extends FormulaValue {
  1076. String _preEvaluatedValue;
  1077. public StringFormulaValue(String formula, String value) {
  1078. super(formula);
  1079. _preEvaluatedValue = value;
  1080. }
  1081. @Override
  1082. CellType getFormulaType()
  1083. {
  1084. return CellType.STRING;
  1085. }
  1086. void setPreEvaluatedValue(String value)
  1087. {
  1088. _preEvaluatedValue=value;
  1089. }
  1090. String getPreEvaluatedValue()
  1091. {
  1092. return _preEvaluatedValue;
  1093. }
  1094. }
  1095. static class RichTextStringFormulaValue extends FormulaValue {
  1096. RichTextString _preEvaluatedValue;
  1097. public RichTextStringFormulaValue(String formula, RichTextString value) {
  1098. super(formula);
  1099. _preEvaluatedValue = value;
  1100. }
  1101. @Override
  1102. CellType getFormulaType()
  1103. {
  1104. return CellType.STRING;
  1105. }
  1106. void setPreEvaluatedValue(RichTextString value)
  1107. {
  1108. _preEvaluatedValue=value;
  1109. }
  1110. RichTextString getPreEvaluatedValue()
  1111. {
  1112. return _preEvaluatedValue;
  1113. }
  1114. }
  1115. static class BooleanFormulaValue extends FormulaValue {
  1116. boolean _preEvaluatedValue;
  1117. public BooleanFormulaValue(String formula, boolean value) {
  1118. super(formula);
  1119. _preEvaluatedValue = value;
  1120. }
  1121. @Override
  1122. CellType getFormulaType()
  1123. {
  1124. return CellType.BOOLEAN;
  1125. }
  1126. void setPreEvaluatedValue(boolean value)
  1127. {
  1128. _preEvaluatedValue=value;
  1129. }
  1130. boolean getPreEvaluatedValue()
  1131. {
  1132. return _preEvaluatedValue;
  1133. }
  1134. }
  1135. static class ErrorFormulaValue extends FormulaValue {
  1136. byte _preEvaluatedValue;
  1137. public ErrorFormulaValue(String formula, byte value) {
  1138. super(formula);
  1139. _preEvaluatedValue = value;
  1140. }
  1141. @Override
  1142. CellType getFormulaType()
  1143. {
  1144. return CellType.ERROR;
  1145. }
  1146. void setPreEvaluatedValue(byte value)
  1147. {
  1148. _preEvaluatedValue=value;
  1149. }
  1150. byte getPreEvaluatedValue()
  1151. {
  1152. return _preEvaluatedValue;
  1153. }
  1154. }
  1155. static class BlankValue implements Value {
  1156. public CellType getType()
  1157. {
  1158. return CellType.BLANK;
  1159. }
  1160. }
  1161. static class BooleanValue implements Value {
  1162. boolean _value;
  1163. public BooleanValue() {
  1164. _value = false;
  1165. }
  1166. public BooleanValue(boolean _value) {
  1167. this._value = _value;
  1168. }
  1169. public CellType getType()
  1170. {
  1171. return CellType.BOOLEAN;
  1172. }
  1173. void setValue(boolean value)
  1174. {
  1175. _value = value;
  1176. }
  1177. boolean getValue()
  1178. {
  1179. return _value;
  1180. }
  1181. }
  1182. static class ErrorValue implements Value
  1183. {
  1184. byte _value;
  1185. public ErrorValue() {
  1186. _value = FormulaError._NO_ERROR.getCode();
  1187. }
  1188. public ErrorValue(byte _value) {
  1189. this._value = _value;
  1190. }
  1191. public CellType getType()
  1192. {
  1193. return CellType.ERROR;
  1194. }
  1195. void setValue(byte value)
  1196. {
  1197. _value = value;
  1198. }
  1199. byte getValue()
  1200. {
  1201. return _value;
  1202. }
  1203. }
  1204. }