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.

HSSFCell.java 44KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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.text.DateFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.util.*;
  19. import org.apache.poi.hssf.model.HSSFFormulaParser;
  20. import org.apache.poi.hssf.model.InternalSheet;
  21. import org.apache.poi.hssf.model.InternalWorkbook;
  22. import org.apache.poi.hssf.record.BlankRecord;
  23. import org.apache.poi.hssf.record.BoolErrRecord;
  24. import org.apache.poi.hssf.record.CellValueRecordInterface;
  25. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  26. import org.apache.poi.hssf.record.DrawingRecord;
  27. import org.apache.poi.hssf.record.ExtendedFormatRecord;
  28. import org.apache.poi.hssf.record.FormulaRecord;
  29. import org.apache.poi.hssf.record.HyperlinkRecord;
  30. import org.apache.poi.hssf.record.LabelSSTRecord;
  31. import org.apache.poi.hssf.record.NoteRecord;
  32. import org.apache.poi.hssf.record.NumberRecord;
  33. import org.apache.poi.hssf.record.ObjRecord;
  34. import org.apache.poi.hssf.record.Record;
  35. import org.apache.poi.hssf.record.RecordBase;
  36. import org.apache.poi.hssf.record.SubRecord;
  37. import org.apache.poi.hssf.record.TextObjectRecord;
  38. import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
  39. import org.apache.poi.hssf.record.common.UnicodeString;
  40. import org.apache.poi.ss.formula.ptg.ExpPtg;
  41. import org.apache.poi.ss.formula.ptg.Ptg;
  42. import org.apache.poi.ss.formula.eval.ErrorEval;
  43. import org.apache.poi.ss.usermodel.*;
  44. import org.apache.poi.ss.util.CellRangeAddress;
  45. import org.apache.poi.ss.util.CellReference;
  46. import org.apache.poi.ss.util.NumberToTextConverter;
  47. import org.apache.poi.ss.formula.FormulaType;
  48. import org.apache.poi.ss.SpreadsheetVersion;
  49. import org.apache.poi.util.POILogger;
  50. import org.apache.poi.util.POILogFactory;
  51. /**
  52. * High level representation of a cell in a row of a spreadsheet.
  53. * Cells can be numeric, formula-based or string-based (text). The cell type
  54. * specifies this. String cells cannot conatin numbers and numeric cells cannot
  55. * contain strings (at least according to our model). Client apps should do the
  56. * conversions themselves. Formula cells have the formula string, as well as
  57. * the formula result, which can be numeric or string.
  58. * <p>
  59. * Cells should have their number (0 based) before being added to a row. Only
  60. * cells that have values should be added.
  61. * <p>
  62. *
  63. * @author Andrew C. Oliver (acoliver at apache dot org)
  64. * @author Dan Sherman (dsherman at isisph.com)
  65. * @author Brian Sanders (kestrel at burdell dot org) Active Cell support
  66. * @author Yegor Kozlov cell comments support
  67. */
  68. public class HSSFCell implements Cell {
  69. private static POILogger log = POILogFactory.getLogger(HSSFCell.class);
  70. private static final String FILE_FORMAT_NAME = "BIFF8";
  71. /**
  72. * The maximum number of columns in BIFF8
  73. */
  74. public static final int LAST_COLUMN_NUMBER = SpreadsheetVersion.EXCEL97.getLastColumnIndex(); // 2^8 - 1
  75. private static final String LAST_COLUMN_NAME = SpreadsheetVersion.EXCEL97.getLastColumnName();
  76. public final static short ENCODING_UNCHANGED = -1;
  77. public final static short ENCODING_COMPRESSED_UNICODE = 0;
  78. public final static short ENCODING_UTF_16 = 1;
  79. private final HSSFWorkbook _book;
  80. private final HSSFSheet _sheet;
  81. private int _cellType;
  82. private HSSFRichTextString _stringValue;
  83. private CellValueRecordInterface _record;
  84. private HSSFComment _comment;
  85. /**
  86. * Creates new Cell - Should only be called by HSSFRow. This creates a cell
  87. * from scratch.
  88. * <p>
  89. * When the cell is initially created it is set to CELL_TYPE_BLANK. Cell types
  90. * can be changed/overwritten by calling setCellValue with the appropriate
  91. * type as a parameter although conversions from one type to another may be
  92. * prohibited.
  93. *
  94. * @param book - Workbook record of the workbook containing this cell
  95. * @param sheet - Sheet record of the sheet containing this cell
  96. * @param row - the row of this cell
  97. * @param col - the column for this cell
  98. *
  99. * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short)
  100. */
  101. protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col)
  102. {
  103. checkBounds(col);
  104. _stringValue = null;
  105. _book = book;
  106. _sheet = sheet;
  107. // Relying on the fact that by default the cellType is set to 0 which
  108. // is different to CELL_TYPE_BLANK hence the following method call correctly
  109. // creates a new blank cell.
  110. short xfindex = sheet.getSheet().getXFIndexForColAt(col);
  111. setCellType(CELL_TYPE_BLANK, false, row, col,xfindex);
  112. }
  113. /**
  114. * Returns the HSSFSheet this cell belongs to
  115. *
  116. * @return the HSSFSheet that owns this cell
  117. */
  118. public HSSFSheet getSheet() {
  119. return _sheet;
  120. }
  121. /**
  122. * Returns the HSSFRow this cell belongs to
  123. *
  124. * @return the HSSFRow that owns this cell
  125. */
  126. public HSSFRow getRow() {
  127. int rowIndex = getRowIndex();
  128. return _sheet.getRow(rowIndex);
  129. }
  130. /**
  131. * Creates new Cell - Should only be called by HSSFRow. This creates a cell
  132. * from scratch.
  133. *
  134. * @param book - Workbook record of the workbook containing this cell
  135. * @param sheet - Sheet record of the sheet containing this cell
  136. * @param row - the row of this cell
  137. * @param col - the column for this cell
  138. * @param type - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK,
  139. * CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
  140. * Type of cell
  141. * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short,int)
  142. */
  143. protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
  144. int type)
  145. {
  146. checkBounds(col);
  147. _cellType = -1; // Force 'setCellType' to create a first Record
  148. _stringValue = null;
  149. _book = book;
  150. _sheet = sheet;
  151. short xfindex = sheet.getSheet().getXFIndexForColAt(col);
  152. setCellType(type,false,row,col,xfindex);
  153. }
  154. /**
  155. * Creates an HSSFCell from a CellValueRecordInterface. HSSFSheet uses this when
  156. * reading in cells from an existing sheet.
  157. *
  158. * @param book - Workbook record of the workbook containing this cell
  159. * @param sheet - Sheet record of the sheet containing this cell
  160. * @param cval - the Cell Value Record we wish to represent
  161. */
  162. protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, CellValueRecordInterface cval) {
  163. _record = cval;
  164. _cellType = determineType(cval);
  165. _stringValue = null;
  166. _book = book;
  167. _sheet = sheet;
  168. switch (_cellType)
  169. {
  170. case CELL_TYPE_STRING :
  171. _stringValue = new HSSFRichTextString(book.getWorkbook(), (LabelSSTRecord ) cval);
  172. break;
  173. case CELL_TYPE_BLANK :
  174. break;
  175. case CELL_TYPE_FORMULA :
  176. _stringValue=new HSSFRichTextString(((FormulaRecordAggregate) cval).getStringValue());
  177. break;
  178. }
  179. }
  180. /**
  181. * used internally -- given a cell value record, figure out its type
  182. */
  183. private static int determineType(CellValueRecordInterface cval) {
  184. if (cval instanceof FormulaRecordAggregate) {
  185. return HSSFCell.CELL_TYPE_FORMULA;
  186. }
  187. // all others are plain BIFF records
  188. Record record = ( Record ) cval;
  189. switch (record.getSid()) {
  190. case NumberRecord.sid : return HSSFCell.CELL_TYPE_NUMERIC;
  191. case BlankRecord.sid : return HSSFCell.CELL_TYPE_BLANK;
  192. case LabelSSTRecord.sid : return HSSFCell.CELL_TYPE_STRING;
  193. case BoolErrRecord.sid :
  194. BoolErrRecord boolErrRecord = ( BoolErrRecord ) record;
  195. return boolErrRecord.isBoolean()
  196. ? HSSFCell.CELL_TYPE_BOOLEAN
  197. : HSSFCell.CELL_TYPE_ERROR;
  198. }
  199. throw new RuntimeException("Bad cell value rec (" + cval.getClass().getName() + ")");
  200. }
  201. /**
  202. * Returns the Workbook that this Cell is bound to
  203. */
  204. protected InternalWorkbook getBoundWorkbook() {
  205. return _book.getWorkbook();
  206. }
  207. /**
  208. * @return the (zero based) index of the row containing this cell
  209. */
  210. public int getRowIndex() {
  211. return _record.getRow();
  212. }
  213. /**
  214. * Set the cell's number within the row (0 based).
  215. * @param num short the cell number
  216. * @deprecated (Jan 2008) Doesn't update the row's idea of what cell this is, use {@link HSSFRow#moveCell(HSSFCell, short)} instead
  217. */
  218. public void setCellNum(short num)
  219. {
  220. _record.setColumn(num);
  221. }
  222. /**
  223. * Updates the cell record's idea of what
  224. * column it belongs in (0 based)
  225. * @param num the new cell number
  226. */
  227. protected void updateCellNum(short num)
  228. {
  229. _record.setColumn(num);
  230. }
  231. /**
  232. * @deprecated (Oct 2008) use {@link #getColumnIndex()}
  233. */
  234. public short getCellNum() {
  235. return (short) getColumnIndex();
  236. }
  237. public int getColumnIndex() {
  238. return _record.getColumn() & 0xFFFF;
  239. }
  240. /**
  241. * set the cells type (numeric, formula or string)
  242. * @see #CELL_TYPE_NUMERIC
  243. * @see #CELL_TYPE_STRING
  244. * @see #CELL_TYPE_FORMULA
  245. * @see #CELL_TYPE_BLANK
  246. * @see #CELL_TYPE_BOOLEAN
  247. * @see #CELL_TYPE_ERROR
  248. */
  249. public void setCellType(int cellType) {
  250. notifyFormulaChanging();
  251. if(isPartOfArrayFormulaGroup()){
  252. notifyArrayFormulaChanging();
  253. }
  254. int row=_record.getRow();
  255. short col=_record.getColumn();
  256. short styleIndex=_record.getXFIndex();
  257. setCellType(cellType, true, row, col, styleIndex);
  258. }
  259. /**
  260. * sets the cell type. The setValue flag indicates whether to bother about
  261. * trying to preserve the current value in the new record if one is created.
  262. * <p>
  263. * The @see #setCellValue method will call this method with false in setValue
  264. * since it will overwrite the cell value later
  265. *
  266. */
  267. private void setCellType(int cellType, boolean setValue, int row,short col, short styleIndex)
  268. {
  269. if (cellType > CELL_TYPE_ERROR)
  270. {
  271. throw new RuntimeException("I have no idea what type that is!");
  272. }
  273. switch (cellType)
  274. {
  275. case CELL_TYPE_FORMULA :
  276. FormulaRecordAggregate frec;
  277. if (cellType != _cellType) {
  278. frec = _sheet.getSheet().getRowsAggregate().createFormula(row, col);
  279. } else {
  280. frec = (FormulaRecordAggregate) _record;
  281. frec.setRow(row);
  282. frec.setColumn(col);
  283. }
  284. if (setValue)
  285. {
  286. frec.getFormulaRecord().setValue(getNumericCellValue());
  287. }
  288. frec.setXFIndex(styleIndex);
  289. _record = frec;
  290. break;
  291. case CELL_TYPE_NUMERIC :
  292. NumberRecord nrec = null;
  293. if (cellType != _cellType)
  294. {
  295. nrec = new NumberRecord();
  296. }
  297. else
  298. {
  299. nrec = ( NumberRecord ) _record;
  300. }
  301. nrec.setColumn(col);
  302. if (setValue)
  303. {
  304. nrec.setValue(getNumericCellValue());
  305. }
  306. nrec.setXFIndex(styleIndex);
  307. nrec.setRow(row);
  308. _record = nrec;
  309. break;
  310. case CELL_TYPE_STRING :
  311. LabelSSTRecord lrec;
  312. if (cellType == _cellType) {
  313. lrec = (LabelSSTRecord) _record;
  314. } else {
  315. lrec = new LabelSSTRecord();
  316. lrec.setColumn(col);
  317. lrec.setRow(row);
  318. lrec.setXFIndex(styleIndex);
  319. }
  320. if (setValue) {
  321. String str = convertCellValueToString();
  322. int sstIndex = _book.getWorkbook().addSSTString(new UnicodeString(str));
  323. lrec.setSSTIndex(sstIndex);
  324. UnicodeString us = _book.getWorkbook().getSSTString(sstIndex);
  325. _stringValue = new HSSFRichTextString();
  326. _stringValue.setUnicodeString(us);
  327. }
  328. _record = lrec;
  329. break;
  330. case CELL_TYPE_BLANK :
  331. BlankRecord brec = null;
  332. if (cellType != _cellType)
  333. {
  334. brec = new BlankRecord();
  335. }
  336. else
  337. {
  338. brec = ( BlankRecord ) _record;
  339. }
  340. brec.setColumn(col);
  341. // During construction the cellStyle may be null for a Blank cell.
  342. brec.setXFIndex(styleIndex);
  343. brec.setRow(row);
  344. _record = brec;
  345. break;
  346. case CELL_TYPE_BOOLEAN :
  347. BoolErrRecord boolRec = null;
  348. if (cellType != _cellType)
  349. {
  350. boolRec = new BoolErrRecord();
  351. }
  352. else
  353. {
  354. boolRec = ( BoolErrRecord ) _record;
  355. }
  356. boolRec.setColumn(col);
  357. if (setValue)
  358. {
  359. boolRec.setValue(convertCellValueToBoolean());
  360. }
  361. boolRec.setXFIndex(styleIndex);
  362. boolRec.setRow(row);
  363. _record = boolRec;
  364. break;
  365. case CELL_TYPE_ERROR :
  366. BoolErrRecord errRec = null;
  367. if (cellType != _cellType)
  368. {
  369. errRec = new BoolErrRecord();
  370. }
  371. else
  372. {
  373. errRec = ( BoolErrRecord ) _record;
  374. }
  375. errRec.setColumn(col);
  376. if (setValue)
  377. {
  378. errRec.setValue((byte)HSSFErrorConstants.ERROR_VALUE);
  379. }
  380. errRec.setXFIndex(styleIndex);
  381. errRec.setRow(row);
  382. _record = errRec;
  383. break;
  384. }
  385. if (cellType != _cellType &&
  386. _cellType!=-1 ) // Special Value to indicate an uninitialized Cell
  387. {
  388. _sheet.getSheet().replaceValueRecord(_record);
  389. }
  390. _cellType = cellType;
  391. }
  392. /**
  393. * get the cells type (numeric, formula or string)
  394. * @see #CELL_TYPE_STRING
  395. * @see #CELL_TYPE_NUMERIC
  396. * @see #CELL_TYPE_FORMULA
  397. * @see #CELL_TYPE_BOOLEAN
  398. * @see #CELL_TYPE_ERROR
  399. */
  400. public int getCellType()
  401. {
  402. return _cellType;
  403. }
  404. /**
  405. * set a numeric value for the cell
  406. *
  407. * @param value the numeric value to set this cell to. For formulas we'll set the
  408. * precalculated value, for numerics we'll set its value. For other types we
  409. * will change the cell to a numeric cell and set its value.
  410. */
  411. public void setCellValue(double value) {
  412. if(Double.isInfinite(value)) {
  413. // Excel does not support positive/negative infinities,
  414. // rather, it gives a #DIV/0! error in these cases.
  415. setCellErrorValue(FormulaError.DIV0.getCode());
  416. } else if (Double.isNaN(value)){
  417. // Excel does not support Not-a-Number (NaN),
  418. // instead it immediately generates a #NUM! error.
  419. setCellErrorValue(FormulaError.NUM.getCode());
  420. } else {
  421. int row=_record.getRow();
  422. short col=_record.getColumn();
  423. short styleIndex=_record.getXFIndex();
  424. switch (_cellType) {
  425. default:
  426. setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex);
  427. case CELL_TYPE_NUMERIC:
  428. (( NumberRecord ) _record).setValue(value);
  429. break;
  430. case CELL_TYPE_FORMULA:
  431. ((FormulaRecordAggregate)_record).setCachedDoubleResult(value);
  432. break;
  433. }
  434. }
  435. }
  436. /**
  437. * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
  438. * a date.
  439. *
  440. * @param value the date value to set this cell to. For formulas we'll set the
  441. * precalculated value, for numerics we'll set its value. For other types we
  442. * will change the cell to a numeric cell and set its value.
  443. */
  444. public void setCellValue(Date value)
  445. {
  446. setCellValue(HSSFDateUtil.getExcelDate(value, _book.getWorkbook().isUsing1904DateWindowing()));
  447. }
  448. /**
  449. * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
  450. * a date.
  451. *
  452. * This will set the cell value based on the Calendar's timezone. As Excel
  453. * does not support timezones this means that both 20:00+03:00 and
  454. * 20:00-03:00 will be reported as the same value (20:00) even that there
  455. * are 6 hours difference between the two times. This difference can be
  456. * preserved by using <code>setCellValue(value.getTime())</code> which will
  457. * automatically shift the times to the default timezone.
  458. *
  459. * @param value the date value to set this cell to. For formulas we'll set the
  460. * precalculated value, for numerics we'll set its value. For othertypes we
  461. * will change the cell to a numeric cell and set its value.
  462. */
  463. public void setCellValue(Calendar value)
  464. {
  465. setCellValue( HSSFDateUtil.getExcelDate(value, _book.getWorkbook().isUsing1904DateWindowing()) );
  466. }
  467. /**
  468. * set a string value for the cell.
  469. *
  470. * @param value value to set the cell to. For formulas we'll set the formula
  471. * cached string result, for String cells we'll set its value. For other types we will
  472. * change the cell to a string cell and set its value.
  473. * If value is null then we will change the cell to a Blank cell.
  474. */
  475. public void setCellValue(String value) {
  476. HSSFRichTextString str = value == null ? null : new HSSFRichTextString(value);
  477. setCellValue(str);
  478. }
  479. /**
  480. * Set a string value for the cell.
  481. *
  482. * @param value value to set the cell to. For formulas we'll set the formula
  483. * string, for String cells we'll set its value. For other types we will
  484. * change the cell to a string cell and set its value.
  485. * If value is <code>null</code> then we will change the cell to a Blank cell.
  486. */
  487. public void setCellValue(RichTextString value)
  488. {
  489. HSSFRichTextString hvalue = (HSSFRichTextString) value;
  490. int row=_record.getRow();
  491. short col=_record.getColumn();
  492. short styleIndex=_record.getXFIndex();
  493. if (hvalue == null)
  494. {
  495. notifyFormulaChanging();
  496. setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
  497. return;
  498. }
  499. if(hvalue.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){
  500. throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
  501. }
  502. if (_cellType == CELL_TYPE_FORMULA) {
  503. // Set the 'pre-evaluated result' for the formula
  504. // note - formulas do not preserve text formatting.
  505. FormulaRecordAggregate fr = (FormulaRecordAggregate) _record;
  506. fr.setCachedStringResult(hvalue.getString());
  507. // Update our local cache to the un-formatted version
  508. _stringValue = new HSSFRichTextString(value.getString());
  509. // All done
  510. return;
  511. }
  512. // If we get here, we're not dealing with a formula,
  513. // so handle things as a normal rich text cell
  514. if (_cellType != CELL_TYPE_STRING) {
  515. setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);
  516. }
  517. int index = 0;
  518. UnicodeString str = hvalue.getUnicodeString();
  519. index = _book.getWorkbook().addSSTString(str);
  520. (( LabelSSTRecord ) _record).setSSTIndex(index);
  521. _stringValue = hvalue;
  522. _stringValue.setWorkbookReferences(_book.getWorkbook(), (( LabelSSTRecord ) _record));
  523. _stringValue.setUnicodeString(_book.getWorkbook().getSSTString(index));
  524. }
  525. public void setCellFormula(String formula) {
  526. if(isPartOfArrayFormulaGroup()){
  527. notifyArrayFormulaChanging();
  528. }
  529. int row=_record.getRow();
  530. short col=_record.getColumn();
  531. short styleIndex=_record.getXFIndex();
  532. if (formula==null) {
  533. notifyFormulaChanging();
  534. setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
  535. return;
  536. }
  537. int sheetIndex = _book.getSheetIndex(_sheet);
  538. Ptg[] ptgs = HSSFFormulaParser.parse(formula, _book, FormulaType.CELL, sheetIndex);
  539. setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);
  540. FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
  541. FormulaRecord frec = agg.getFormulaRecord();
  542. frec.setOptions((short) 2);
  543. frec.setValue(0);
  544. //only set to default if there is no extended format index already set
  545. if (agg.getXFIndex() == (short)0) {
  546. agg.setXFIndex((short) 0x0f);
  547. }
  548. agg.setParsedExpression(ptgs);
  549. }
  550. /**
  551. * Should be called any time that a formula could potentially be deleted.
  552. * Does nothing if this cell currently does not hold a formula
  553. */
  554. private void notifyFormulaChanging() {
  555. if (_record instanceof FormulaRecordAggregate) {
  556. ((FormulaRecordAggregate)_record).notifyFormulaChanging();
  557. }
  558. }
  559. public String getCellFormula() {
  560. if (!(_record instanceof FormulaRecordAggregate)) {
  561. throw typeMismatch(CELL_TYPE_FORMULA, _cellType, true);
  562. }
  563. return HSSFFormulaParser.toFormulaString(_book, ((FormulaRecordAggregate)_record).getFormulaTokens());
  564. }
  565. /**
  566. * Used to help format error messages
  567. */
  568. private static String getCellTypeName(int cellTypeCode) {
  569. switch (cellTypeCode) {
  570. case CELL_TYPE_BLANK: return "blank";
  571. case CELL_TYPE_STRING: return "text";
  572. case CELL_TYPE_BOOLEAN: return "boolean";
  573. case CELL_TYPE_ERROR: return "error";
  574. case CELL_TYPE_NUMERIC: return "numeric";
  575. case CELL_TYPE_FORMULA: return "formula";
  576. }
  577. return "#unknown cell type (" + cellTypeCode + ")#";
  578. }
  579. private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) {
  580. String msg = "Cannot get a "
  581. + getCellTypeName(expectedTypeCode) + " value from a "
  582. + getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell";
  583. return new IllegalStateException(msg);
  584. }
  585. private static void checkFormulaCachedValueType(int expectedTypeCode, FormulaRecord fr) {
  586. int cachedValueType = fr.getCachedResultType();
  587. if (cachedValueType != expectedTypeCode) {
  588. throw typeMismatch(expectedTypeCode, cachedValueType, true);
  589. }
  590. }
  591. /**
  592. * Get the value of the cell as a number.
  593. * For strings we throw an exception.
  594. * For blank cells we return a 0.
  595. * See {@link HSSFDataFormatter} for turning this
  596. * number into a string similar to that which
  597. * Excel would render this number as.
  598. */
  599. public double getNumericCellValue() {
  600. switch(_cellType) {
  601. case CELL_TYPE_BLANK:
  602. return 0.0;
  603. case CELL_TYPE_NUMERIC:
  604. return ((NumberRecord)_record).getValue();
  605. default:
  606. throw typeMismatch(CELL_TYPE_NUMERIC, _cellType, false);
  607. case CELL_TYPE_FORMULA:
  608. break;
  609. }
  610. FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
  611. checkFormulaCachedValueType(CELL_TYPE_NUMERIC, fr);
  612. return fr.getValue();
  613. }
  614. /**
  615. * Get the value of the cell as a date.
  616. * For strings we throw an exception.
  617. * For blank cells we return a null.
  618. * See {@link HSSFDataFormatter} for formatting
  619. * this date into a string similar to how excel does.
  620. */
  621. public Date getDateCellValue() {
  622. if (_cellType == CELL_TYPE_BLANK) {
  623. return null;
  624. }
  625. double value = getNumericCellValue();
  626. if (_book.getWorkbook().isUsing1904DateWindowing()) {
  627. return HSSFDateUtil.getJavaDate(value, true);
  628. }
  629. return HSSFDateUtil.getJavaDate(value, false);
  630. }
  631. /**
  632. * get the value of the cell as a string - for numeric cells we throw an exception.
  633. * For blank cells we return an empty string.
  634. * For formulaCells that are not string Formulas, we throw an exception
  635. */
  636. public String getStringCellValue()
  637. {
  638. HSSFRichTextString str = getRichStringCellValue();
  639. return str.getString();
  640. }
  641. /**
  642. * get the value of the cell as a string - for numeric cells we throw an exception.
  643. * For blank cells we return an empty string.
  644. * For formulaCells that are not string Formulas, we throw an exception
  645. */
  646. public HSSFRichTextString getRichStringCellValue() {
  647. switch(_cellType) {
  648. case CELL_TYPE_BLANK:
  649. return new HSSFRichTextString("");
  650. case CELL_TYPE_STRING:
  651. return _stringValue;
  652. default:
  653. throw typeMismatch(CELL_TYPE_STRING, _cellType, false);
  654. case CELL_TYPE_FORMULA:
  655. break;
  656. }
  657. FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
  658. checkFormulaCachedValueType(CELL_TYPE_STRING, fra.getFormulaRecord());
  659. String strVal = fra.getStringValue();
  660. return new HSSFRichTextString(strVal == null ? "" : strVal);
  661. }
  662. /**
  663. * set a boolean value for the cell
  664. *
  665. * @param value the boolean value to set this cell to. For formulas we'll set the
  666. * precalculated value, for booleans we'll set its value. For other types we
  667. * will change the cell to a boolean cell and set its value.
  668. */
  669. public void setCellValue(boolean value) {
  670. int row=_record.getRow();
  671. short col=_record.getColumn();
  672. short styleIndex=_record.getXFIndex();
  673. switch (_cellType) {
  674. default:
  675. setCellType(CELL_TYPE_BOOLEAN, false, row, col, styleIndex);
  676. case CELL_TYPE_BOOLEAN:
  677. (( BoolErrRecord ) _record).setValue(value);
  678. break;
  679. case CELL_TYPE_FORMULA:
  680. ((FormulaRecordAggregate)_record).setCachedBooleanResult(value);
  681. break;
  682. }
  683. }
  684. /**
  685. * set a error value for the cell
  686. *
  687. * @param errorCode the error value to set this cell to. For formulas we'll set the
  688. * precalculated value , for errors we'll set
  689. * its value. For other types we will change the cell to an error
  690. * cell and set its value.
  691. */
  692. public void setCellErrorValue(byte errorCode) {
  693. int row=_record.getRow();
  694. short col=_record.getColumn();
  695. short styleIndex=_record.getXFIndex();
  696. switch (_cellType) {
  697. default:
  698. setCellType(CELL_TYPE_ERROR, false, row, col, styleIndex);
  699. case CELL_TYPE_ERROR:
  700. (( BoolErrRecord ) _record).setValue(errorCode);
  701. break;
  702. case CELL_TYPE_FORMULA:
  703. ((FormulaRecordAggregate)_record).setCachedErrorResult(errorCode);
  704. break;
  705. }
  706. }
  707. /**
  708. * Chooses a new boolean value for the cell when its type is changing.<p/>
  709. *
  710. * Usually the caller is calling setCellType() with the intention of calling
  711. * setCellValue(boolean) straight afterwards. This method only exists to give
  712. * the cell a somewhat reasonable value until the setCellValue() call (if at all).
  713. * TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this
  714. */
  715. private boolean convertCellValueToBoolean() {
  716. switch (_cellType) {
  717. case CELL_TYPE_BOOLEAN:
  718. return (( BoolErrRecord ) _record).getBooleanValue();
  719. case CELL_TYPE_STRING:
  720. int sstIndex = ((LabelSSTRecord)_record).getSSTIndex();
  721. String text = _book.getWorkbook().getSSTString(sstIndex).getString();
  722. return Boolean.valueOf(text).booleanValue();
  723. case CELL_TYPE_NUMERIC:
  724. return ((NumberRecord)_record).getValue() != 0;
  725. case CELL_TYPE_FORMULA:
  726. // use cached formula result if it's the right type:
  727. FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
  728. checkFormulaCachedValueType(CELL_TYPE_BOOLEAN, fr);
  729. return fr.getCachedBooleanValue();
  730. // Other cases convert to false
  731. // These choices are not well justified.
  732. case CELL_TYPE_ERROR:
  733. case CELL_TYPE_BLANK:
  734. return false;
  735. }
  736. throw new RuntimeException("Unexpected cell type (" + _cellType + ")");
  737. }
  738. private String convertCellValueToString() {
  739. switch (_cellType) {
  740. case CELL_TYPE_BLANK:
  741. return "";
  742. case CELL_TYPE_BOOLEAN:
  743. return ((BoolErrRecord) _record).getBooleanValue() ? "TRUE" : "FALSE";
  744. case CELL_TYPE_STRING:
  745. int sstIndex = ((LabelSSTRecord)_record).getSSTIndex();
  746. return _book.getWorkbook().getSSTString(sstIndex).getString();
  747. case CELL_TYPE_NUMERIC:
  748. return NumberToTextConverter.toText(((NumberRecord)_record).getValue());
  749. case CELL_TYPE_ERROR:
  750. return HSSFErrorConstants.getText(((BoolErrRecord) _record).getErrorValue());
  751. case CELL_TYPE_FORMULA:
  752. // should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator
  753. // just use cached formula result instead
  754. break;
  755. default:
  756. throw new IllegalStateException("Unexpected cell type (" + _cellType + ")");
  757. }
  758. FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
  759. FormulaRecord fr = fra.getFormulaRecord();
  760. switch (fr.getCachedResultType()) {
  761. case CELL_TYPE_BOOLEAN:
  762. return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
  763. case CELL_TYPE_STRING:
  764. return fra.getStringValue();
  765. case CELL_TYPE_NUMERIC:
  766. return NumberToTextConverter.toText(fr.getValue());
  767. case CELL_TYPE_ERROR:
  768. return HSSFErrorConstants.getText(fr.getCachedErrorValue());
  769. }
  770. throw new IllegalStateException("Unexpected formula result type (" + _cellType + ")");
  771. }
  772. /**
  773. * get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception.
  774. * For blank cells we return a false.
  775. */
  776. public boolean getBooleanCellValue() {
  777. switch(_cellType) {
  778. case CELL_TYPE_BLANK:
  779. return false;
  780. case CELL_TYPE_BOOLEAN:
  781. return (( BoolErrRecord ) _record).getBooleanValue();
  782. default:
  783. throw typeMismatch(CELL_TYPE_BOOLEAN, _cellType, false);
  784. case CELL_TYPE_FORMULA:
  785. break;
  786. }
  787. FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
  788. checkFormulaCachedValueType(CELL_TYPE_BOOLEAN, fr);
  789. return fr.getCachedBooleanValue();
  790. }
  791. /**
  792. * get the value of the cell as an error code. For strings, numbers, and booleans, we throw an exception.
  793. * For blank cells we return a 0.
  794. */
  795. public byte getErrorCellValue() {
  796. switch(_cellType) {
  797. case CELL_TYPE_ERROR:
  798. return (( BoolErrRecord ) _record).getErrorValue();
  799. default:
  800. throw typeMismatch(CELL_TYPE_ERROR, _cellType, false);
  801. case CELL_TYPE_FORMULA:
  802. break;
  803. }
  804. FormulaRecord fr = ((FormulaRecordAggregate)_record).getFormulaRecord();
  805. checkFormulaCachedValueType(CELL_TYPE_ERROR, fr);
  806. return (byte) fr.getCachedErrorValue();
  807. }
  808. /**
  809. * set the style for the cell. The style should be an HSSFCellStyle created/retreived from
  810. * the HSSFWorkbook.
  811. *
  812. * @param style reference contained in the workbook
  813. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
  814. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
  815. */
  816. public void setCellStyle(CellStyle style) {
  817. setCellStyle( (HSSFCellStyle)style );
  818. }
  819. public void setCellStyle(HSSFCellStyle style) {
  820. // Verify it really does belong to our workbook
  821. style.verifyBelongsToWorkbook(_book);
  822. short styleIndex;
  823. if(style.getUserStyleName() != null) {
  824. styleIndex = applyUserCellStyle(style);
  825. } else {
  826. styleIndex = style.getIndex();
  827. }
  828. // Change our cell record to use this style
  829. _record.setXFIndex(styleIndex);
  830. }
  831. /**
  832. * get the style for the cell. This is a reference to a cell style contained in the workbook
  833. * object.
  834. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
  835. */
  836. public HSSFCellStyle getCellStyle()
  837. {
  838. short styleIndex=_record.getXFIndex();
  839. ExtendedFormatRecord xf = _book.getWorkbook().getExFormatAt(styleIndex);
  840. return new HSSFCellStyle(styleIndex, xf, _book);
  841. }
  842. /**
  843. * Should only be used by HSSFSheet and friends. Returns the low level CellValueRecordInterface record
  844. *
  845. * @return CellValueRecordInterface representing the cell via the low level api.
  846. */
  847. protected CellValueRecordInterface getCellValueRecord()
  848. {
  849. return _record;
  850. }
  851. /**
  852. * @throws RuntimeException if the bounds are exceeded.
  853. */
  854. private static void checkBounds(int cellIndex) {
  855. if (cellIndex < 0 || cellIndex > LAST_COLUMN_NUMBER) {
  856. throw new IllegalArgumentException("Invalid column index (" + cellIndex
  857. + "). Allowable column range for " + FILE_FORMAT_NAME + " is (0.."
  858. + LAST_COLUMN_NUMBER + ") or ('A'..'" + LAST_COLUMN_NAME + "')");
  859. }
  860. }
  861. /**
  862. * Sets this cell as the active cell for the worksheet
  863. */
  864. public void setAsActiveCell()
  865. {
  866. int row=_record.getRow();
  867. short col=_record.getColumn();
  868. _sheet.getSheet().setActiveCellRow(row);
  869. _sheet.getSheet().setActiveCellCol(col);
  870. }
  871. /**
  872. * Returns a string representation of the cell
  873. *
  874. * This method returns a simple representation,
  875. * anthing more complex should be in user code, with
  876. * knowledge of the semantics of the sheet being processed.
  877. *
  878. * Formula cells return the formula string,
  879. * rather than the formula result.
  880. * Dates are displayed in dd-MMM-yyyy format
  881. * Errors are displayed as #ERR&lt;errIdx&gt;
  882. */
  883. public String toString() {
  884. switch (getCellType()) {
  885. case CELL_TYPE_BLANK:
  886. return "";
  887. case CELL_TYPE_BOOLEAN:
  888. return getBooleanCellValue()?"TRUE":"FALSE";
  889. case CELL_TYPE_ERROR:
  890. return ErrorEval.getText((( BoolErrRecord ) _record).getErrorValue());
  891. case CELL_TYPE_FORMULA:
  892. return getCellFormula();
  893. case CELL_TYPE_NUMERIC:
  894. //TODO apply the dataformat for this cell
  895. if (HSSFDateUtil.isCellDateFormatted(this)) {
  896. DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
  897. return sdf.format(getDateCellValue());
  898. }
  899. return String.valueOf(getNumericCellValue());
  900. case CELL_TYPE_STRING:
  901. return getStringCellValue();
  902. default:
  903. return "Unknown Cell Type: " + getCellType();
  904. }
  905. }
  906. /**
  907. * Assign a comment to this cell. If the supplied
  908. * comment is null, the comment for this cell
  909. * will be removed.
  910. *
  911. * @param comment comment associated with this cell
  912. */
  913. public void setCellComment(Comment comment){
  914. if(comment == null) {
  915. removeCellComment();
  916. return;
  917. }
  918. comment.setRow(_record.getRow());
  919. comment.setColumn(_record.getColumn());
  920. _comment = (HSSFComment)comment;
  921. }
  922. /**
  923. * Returns comment associated with this cell
  924. *
  925. * @return comment associated with this cell
  926. */
  927. public HSSFComment getCellComment(){
  928. if (_comment == null) {
  929. _comment = _sheet.findCellComment(_record.getRow(), _record.getColumn());
  930. }
  931. return _comment;
  932. }
  933. /**
  934. * Removes the comment for this cell, if
  935. * there is one.
  936. * WARNING - some versions of excel will loose
  937. * all comments after performing this action!
  938. */
  939. public void removeCellComment() {
  940. HSSFComment comment = _sheet.findCellComment(_record.getRow(), _record.getColumn());
  941. _comment = null;
  942. if (null == comment){
  943. return;
  944. }
  945. _sheet.getDrawingPatriarch().removeShape(comment);
  946. }
  947. /**
  948. * @return hyperlink associated with this cell or <code>null</code> if not found
  949. */
  950. public HSSFHyperlink getHyperlink(){
  951. for (Iterator<RecordBase> it = _sheet.getSheet().getRecords().iterator(); it.hasNext(); ) {
  952. RecordBase rec = it.next();
  953. if (rec instanceof HyperlinkRecord){
  954. HyperlinkRecord link = (HyperlinkRecord)rec;
  955. if(link.getFirstColumn() == _record.getColumn() && link.getFirstRow() == _record.getRow()){
  956. return new HSSFHyperlink(link);
  957. }
  958. }
  959. }
  960. return null;
  961. }
  962. /**
  963. * Assign a hyperlink to this cell
  964. *
  965. * @param hyperlink hyperlink associated with this cell
  966. */
  967. public void setHyperlink(Hyperlink hyperlink){
  968. HSSFHyperlink link = (HSSFHyperlink)hyperlink;
  969. link.setFirstRow(_record.getRow());
  970. link.setLastRow(_record.getRow());
  971. link.setFirstColumn(_record.getColumn());
  972. link.setLastColumn(_record.getColumn());
  973. switch(link.getType()){
  974. case HSSFHyperlink.LINK_EMAIL:
  975. case HSSFHyperlink.LINK_URL:
  976. link.setLabel("url");
  977. break;
  978. case HSSFHyperlink.LINK_FILE:
  979. link.setLabel("file");
  980. break;
  981. case HSSFHyperlink.LINK_DOCUMENT:
  982. link.setLabel("place");
  983. break;
  984. }
  985. List<RecordBase> records = _sheet.getSheet().getRecords();
  986. int eofLoc = records.size() - 1;
  987. records.add( eofLoc, link.record );
  988. }
  989. /**
  990. * Only valid for formula cells
  991. * @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
  992. * {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending
  993. * on the cached value of the formula
  994. */
  995. public int getCachedFormulaResultType() {
  996. if (_cellType != CELL_TYPE_FORMULA) {
  997. throw new IllegalStateException("Only formula cells have cached results");
  998. }
  999. return ((FormulaRecordAggregate)_record).getFormulaRecord().getCachedResultType();
  1000. }
  1001. void setCellArrayFormula(CellRangeAddress range) {
  1002. int row = _record.getRow();
  1003. short col = _record.getColumn();
  1004. short styleIndex = _record.getXFIndex();
  1005. setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);
  1006. // Billet for formula in rec
  1007. Ptg[] ptgsForCell = {new ExpPtg(range.getFirstRow(), range.getFirstColumn())};
  1008. FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
  1009. agg.setParsedExpression(ptgsForCell);
  1010. }
  1011. public CellRangeAddress getArrayFormulaRange() {
  1012. if (_cellType != CELL_TYPE_FORMULA) {
  1013. String ref = new CellReference(this).formatAsString();
  1014. throw new IllegalStateException("Cell " + ref
  1015. + " is not part of an array formula.");
  1016. }
  1017. return ((FormulaRecordAggregate)_record).getArrayFormulaRange();
  1018. }
  1019. public boolean isPartOfArrayFormulaGroup() {
  1020. if (_cellType != CELL_TYPE_FORMULA) {
  1021. return false;
  1022. }
  1023. return ((FormulaRecordAggregate)_record).isPartOfArrayFormula();
  1024. }
  1025. /**
  1026. * The purpose of this method is to validate the cell state prior to modification
  1027. *
  1028. * @see #notifyArrayFormulaChanging()
  1029. */
  1030. void notifyArrayFormulaChanging(String msg){
  1031. CellRangeAddress cra = getArrayFormulaRange();
  1032. if(cra.getNumberOfCells() > 1) {
  1033. throw new IllegalStateException(msg);
  1034. }
  1035. //un-register the single-cell array formula from the parent XSSFSheet
  1036. getRow().getSheet().removeArrayFormula(this);
  1037. }
  1038. /**
  1039. * Called when this cell is modified.
  1040. * <p>
  1041. * The purpose of this method is to validate the cell state prior to modification.
  1042. * </p>
  1043. *
  1044. * @see #setCellType(int)
  1045. * @see #setCellFormula(String)
  1046. * @see HSSFRow#removeCell(org.apache.poi.ss.usermodel.Cell)
  1047. * @see org.apache.poi.hssf.usermodel.HSSFSheet#removeRow(org.apache.poi.ss.usermodel.Row)
  1048. * @see org.apache.poi.hssf.usermodel.HSSFSheet#shiftRows(int, int, int)
  1049. * @see org.apache.poi.hssf.usermodel.HSSFSheet#addMergedRegion(org.apache.poi.ss.util.CellRangeAddress)
  1050. * @throws IllegalStateException if modification is not allowed
  1051. */
  1052. void notifyArrayFormulaChanging(){
  1053. CellReference ref = new CellReference(this);
  1054. String msg = "Cell "+ref.formatAsString()+" is part of a multi-cell array formula. " +
  1055. "You cannot change part of an array.";
  1056. notifyArrayFormulaChanging(msg);
  1057. }
  1058. /**
  1059. * Applying a user-defined style (UDS) is special. Excel does not directly reference user-defined styles, but
  1060. * instead create a 'proxy' ExtendedFormatRecord referencing the UDS as parent.
  1061. *
  1062. * The proceudre to apply a UDS is as follows:
  1063. *
  1064. * 1. search for a ExtendedFormatRecord with parentIndex == style.getIndex()
  1065. * and xfType == ExtendedFormatRecord.XF_CELL.
  1066. * 2. if not found then create a new ExtendedFormatRecord and copy all attributes from the user-defined style
  1067. * and set the parentIndex to be style.getIndex()
  1068. * 3. return the index of the ExtendedFormatRecord, this will be assigned to the parent cell record
  1069. *
  1070. * @param style the user style to apply
  1071. *
  1072. * @return the index of a ExtendedFormatRecord record that will be referenced by the cell
  1073. */
  1074. private short applyUserCellStyle(HSSFCellStyle style){
  1075. if(style.getUserStyleName() == null) {
  1076. throw new IllegalArgumentException("Expected user-defined style");
  1077. }
  1078. InternalWorkbook iwb = _book.getWorkbook();
  1079. short userXf = -1;
  1080. int numfmt = iwb.getNumExFormats();
  1081. for(short i = 0; i < numfmt; i++){
  1082. ExtendedFormatRecord xf = iwb.getExFormatAt(i);
  1083. if(xf.getXFType() == ExtendedFormatRecord.XF_CELL && xf.getParentIndex() == style.getIndex() ){
  1084. userXf = i;
  1085. break;
  1086. }
  1087. }
  1088. short styleIndex;
  1089. if (userXf == -1){
  1090. ExtendedFormatRecord xfr = iwb.createCellXF();
  1091. xfr.cloneStyleFrom(iwb.getExFormatAt(style.getIndex()));
  1092. xfr.setIndentionOptions((short)0);
  1093. xfr.setXFType(ExtendedFormatRecord.XF_CELL);
  1094. xfr.setParentIndex(style.getIndex());
  1095. styleIndex = (short)numfmt;
  1096. } else {
  1097. styleIndex = userXf;
  1098. }
  1099. return styleIndex;
  1100. }
  1101. }