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