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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
|
/* ====================================================================
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.xssf.streaming;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaParseException;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
/**
* Streaming version of XSSFRow implementing the "BigGridDemo" strategy.
*/
public class SXSSFCell implements Cell {
private static POILogger logger = POILogFactory.getLogger(SXSSFCell.class);
SXSSFRow _row;
Value _value;
CellStyle _style;
Property _firstProperty;
public SXSSFCell(SXSSFRow row,int cellType)
{
_row=row;
setType(cellType);
}
//start of interface implementation
/**
* Returns column index of this cell
*
* @return zero-based column index of a column in a sheet.
*/
public int getColumnIndex()
{
return _row.getCellIndex(this);
}
/**
* 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
*/
public int getRowIndex()
{
return _row.getRowNum();
}
/**
* Returns the sheet this cell belongs to
*
* @return the sheet this cell belongs to
*/
public SXSSFSheet getSheet()
{
return _row.getSheet();
}
/**
* Returns the Row this cell belongs to
*
* @return the Row that owns this cell
*/
public Row getRow()
{
return _row;
}
/**
* Set the cells type (numeric, formula or string)
*
* @throws IllegalArgumentException if the specified cell type is invalid
* @see #CELL_TYPE_NUMERIC
* @see #CELL_TYPE_STRING
* @see #CELL_TYPE_FORMULA
* @see #CELL_TYPE_BLANK
* @see #CELL_TYPE_BOOLEAN
* @see #CELL_TYPE_ERROR
*/
public void setCellType(int cellType)
{
ensureType(cellType);
}
/**
* Return the cell type.
*
* @return the cell type
* @see Cell#CELL_TYPE_BLANK
* @see Cell#CELL_TYPE_NUMERIC
* @see Cell#CELL_TYPE_STRING
* @see Cell#CELL_TYPE_FORMULA
* @see Cell#CELL_TYPE_BOOLEAN
* @see Cell#CELL_TYPE_ERROR
*/
public int getCellType()
{
return _value.getType();
}
/**
* Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
* {@link #CELL_TYPE_BOOLEAN}, {@link #CELL_TYPE_ERROR}) depending
* on the cached value of the formula
*/
public int getCachedFormulaResultType()
{
if (_value.getType() != CELL_TYPE_FORMULA) {
throw new IllegalStateException("Only formula cells have cached results");
}
return ((FormulaValue)_value).getFormulaType();
}
/**
* 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.
*/
public void setCellValue(double value)
{
if(Double.isInfinite(value)) {
// Excel does not support positive/negative infinities,
// rather, it gives a #DIV/0! error in these cases.
setCellErrorValue(FormulaError.DIV0.getCode());
} else if (Double.isNaN(value)){
setCellErrorValue(FormulaError.NUM.getCode());
} else {
ensureTypeOrFormulaType(CELL_TYPE_NUMERIC);
if(_value.getType()==CELL_TYPE_FORMULA)
((NumericFormulaValue)_value).setPreEvaluatedValue(value);
else
((NumericValue)_value).setValue(value);
}
}
/**
* Converts the supplied date to its equivalent Excel numeric value and sets
* that into the cell.
* <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 Cell#CELL_TYPE_NUMERIC}. POI
* does not attempt to replicate this behaviour. To make a numeric cell
* display as a date, use {@link #setCellStyle(CellStyle)} etc.
*
* @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.
*/
public void setCellValue(Date value) {
boolean date1904 = getSheet().getWorkbook().isDate1904();
setCellValue(DateUtil.getExcelDate(value, date1904));
}
/**
* Set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
* a date.
* <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.
*/
public void setCellValue(Calendar value) {
boolean date1904 = getSheet().getWorkbook().isDate1904();
setCellValue( DateUtil.getExcelDate(value, date1904 ));
}
/**
* 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.
*/
public void setCellValue(RichTextString value)
{
ensureRichTextStringType();
if(value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
((RichTextValue)_value).setValue(value);
if (((XSSFRichTextString)value).hasFormatting())
logger.log(POILogger.WARN, "SXSSF doesn't support Shared Strings, rich text formatting information has be lost");
}
/**
* 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.
*/
public void setCellValue(String value)
{
ensureTypeOrFormulaType(CELL_TYPE_STRING);
if(value != null && value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()){
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}
if(_value.getType()==CELL_TYPE_FORMULA)
((StringFormulaValue)_value).setPreEvaluatedValue(value);
else
((PlainStringValue)_value).setValue(value);
}
/**
* Sets formula for this cell.
* <p>
* Note, this method only sets the formula string and does not calculate the formula value.
* To set the precalculated value use {@link #setCellValue(double)} or {@link #setCellValue(String)}
* </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.
* @throws FormulaParseException if the formula has incorrect syntax or is otherwise invalid
*/
public void setCellFormula(String formula) throws FormulaParseException
{
if(formula == null) {
setType(Cell.CELL_TYPE_BLANK);
return;
}
ensureFormulaType(computeTypeFromFormula(formula));
((FormulaValue)_value).setValue(formula);
}
/**
* 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 CELL_TYPE_FORMULA
*/
public String getCellFormula()
{
if(_value.getType()!=CELL_TYPE_FORMULA)
throw typeMismatch(CELL_TYPE_FORMULA,_value.getType(),false);
return ((FormulaValue)_value).getValue();
}
/**
* 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 CELL_TYPE_STRING
* @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
* @see org.apache.poi.ss.usermodel.DataFormatter for turning this number into a string similar to that which Excel would render this number as.
*/
public double getNumericCellValue()
{
int cellType = getCellType();
switch(cellType)
{
case CELL_TYPE_BLANK:
return 0.0;
case CELL_TYPE_FORMULA:
{
FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_NUMERIC)
throw typeMismatch(CELL_TYPE_NUMERIC, CELL_TYPE_FORMULA, false);
return ((NumericFormulaValue)_value).getPreEvaluatedValue();
}
case CELL_TYPE_NUMERIC:
return ((NumericValue)_value).getValue();
default:
throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false);
}
}
/**
* 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 CELL_TYPE_STRING
* @exception NumberFormatException if the cell value isn't a parsable <code>double</code>.
* @see org.apache.poi.ss.usermodel.DataFormatter for formatting this date into a string similar to how excel does.
*/
public Date getDateCellValue()
{
int cellType = getCellType();
if (cellType == CELL_TYPE_BLANK)
{
return null;
}
double value = getNumericCellValue();
boolean date1904 = getSheet().getWorkbook().isDate1904();
return DateUtil.getJavaDate(value, date1904);
}
/**
* 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
*/
public RichTextString getRichStringCellValue()
{
int cellType = getCellType();
if(getCellType() != CELL_TYPE_STRING)
throw typeMismatch(CELL_TYPE_STRING, cellType, false);
StringValue sval = (StringValue)_value;
if(sval.isRichText())
return ((RichTextValue)_value).getValue();
else {
String plainText = getStringCellValue();
return getSheet().getWorkbook().getCreationHelper().createRichTextString(plainText);
}
}
/**
* 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
*/
public String getStringCellValue()
{
int cellType = getCellType();
switch(cellType)
{
case CELL_TYPE_BLANK:
return "";
case CELL_TYPE_FORMULA:
{
FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_STRING)
throw typeMismatch(CELL_TYPE_STRING, CELL_TYPE_FORMULA, false);
return ((StringFormulaValue)_value).getPreEvaluatedValue();
}
case CELL_TYPE_STRING:
{
if(((StringValue)_value).isRichText())
return ((RichTextValue)_value).getValue().getString();
else
return ((PlainStringValue)_value).getValue();
}
default:
throw typeMismatch(CELL_TYPE_STRING, cellType, false);
}
}
/**
* 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.
*/
public void setCellValue(boolean value)
{
ensureTypeOrFormulaType(CELL_TYPE_BOOLEAN);
if(_value.getType()==CELL_TYPE_FORMULA)
((BooleanFormulaValue)_value).setPreEvaluatedValue(value);
else
((BooleanValue)_value).setValue(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 org.apache.poi.ss.usermodel.FormulaError
*/
public void setCellErrorValue(byte value)
{
ensureType(CELL_TYPE_ERROR);
if(_value.getType()==CELL_TYPE_FORMULA)
((ErrorFormulaValue)_value).setPreEvaluatedValue(value);
else
((ErrorValue)_value).setValue(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 CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA
*/
public boolean getBooleanCellValue()
{
int cellType = getCellType();
switch(cellType)
{
case CELL_TYPE_BLANK:
return false;
case CELL_TYPE_FORMULA:
{
FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_BOOLEAN)
throw typeMismatch(CELL_TYPE_BOOLEAN, CELL_TYPE_FORMULA, false);
return ((BooleanFormulaValue)_value).getPreEvaluatedValue();
}
case CELL_TYPE_BOOLEAN:
{
return ((BooleanValue)_value).getValue();
}
default:
throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false);
}
}
/**
* 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 CELL_TYPE_ERROR
* @see org.apache.poi.ss.usermodel.FormulaError for error codes
*/
public byte getErrorCellValue()
{
int cellType = getCellType();
switch(cellType)
{
case CELL_TYPE_BLANK:
return 0;
case CELL_TYPE_FORMULA:
{
FormulaValue fv=(FormulaValue)_value;
if(fv.getFormulaType()!=CELL_TYPE_ERROR)
throw typeMismatch(CELL_TYPE_ERROR, CELL_TYPE_FORMULA, false);
return ((ErrorFormulaValue)_value).getPreEvaluatedValue();
}
case CELL_TYPE_ERROR:
{
return ((ErrorValue)_value).getValue();
}
default:
throw typeMismatch(CELL_TYPE_ERROR, cellType, false);
}
}
/**
* Set the style for the cell. The style should be an CellStyle created/retreived from
* the Workbook.
*
* @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()
*/
public void setCellStyle(CellStyle style)
{
_style=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 org.apache.poi.ss.usermodel.Workbook#getCellStyleAt(short)
*/
public CellStyle getCellStyle()
{
if(_style == null){
SXSSFWorkbook wb = (SXSSFWorkbook)getRow().getSheet().getWorkbook();
return wb.getCellStyleAt((short)0);
} else {
return _style;
}
}
/**
* Sets this cell as the active cell for the worksheet
*/
public void setAsActiveCell()
{
//TODO: What needs to be done here? Is there a "the active cell" at the sheet or even the workbook level?
//getRow().setAsActiveCell(this);
}
/**
* Assign a comment to this cell
*
* @param comment comment associated with this cell
*/
public void setCellComment(Comment comment)
{
setProperty(Property.COMMENT,comment);
}
/**
* Returns comment associated with this cell
*
* @return comment associated with this cell or <code>null</code> if not found
*/
public Comment getCellComment()
{
return (Comment)getPropertyValue(Property.COMMENT);
}
/**
* Removes the comment for this cell, if there is one.
*/
public void removeCellComment()
{
removeProperty(Property.COMMENT);
}
/**
* @return hyperlink associated with this cell or <code>null</code> if not found
*/
public Hyperlink getHyperlink()
{
return (Hyperlink)getPropertyValue(Property.HYPERLINK);
}
/**
* Assign a hyperlink to this cell. If the supplied hyperlink is null, the
* hyperlink for this cell will be removed.
*
* @param link hyperlink associated with this cell
*/
public void setHyperlink(Hyperlink link)
{
if (link == null) {
removeHyperlink();
return;
}
setProperty(Property.HYPERLINK,link);
XSSFHyperlink xssfobj = (XSSFHyperlink)link;
// Assign to us
CellReference ref = new CellReference(getRowIndex(), getColumnIndex());
xssfobj.getCTHyperlink().setRef( ref.formatAsString() );
// Add to the lists
getSheet()._sh.addHyperlink(xssfobj);
}
/**
* Removes the hyperlink for this cell, if there is one.
*/
public void removeHyperlink()
{
removeProperty(Property.HYPERLINK);
getSheet()._sh.removeHyperlink(getRowIndex(), getColumnIndex());
}
/**
* Only valid for array formula cells
*
* @return range of the array formula group that the cell belongs to.
*/
//TODO: What is this?
public CellRangeAddress getArrayFormulaRange()
{
return null;
}
/**
* @return <code>true</code> if this cell is part of group of cells having a common array formula.
*/
//TODO: What is this?
public boolean isPartOfArrayFormulaGroup()
{
return false;
}
//end of interface implementation
/**
* Returns a string representation of the cell
* <p>
* Formula cells return the formula string, rather than the formula result.
* Dates are displayed in dd-MMM-yyyy format
* Errors are displayed as #ERR<errIdx>
* </p>
*/
@Override
public String toString() {
switch (getCellType()) {
case CELL_TYPE_BLANK:
return "";
case CELL_TYPE_BOOLEAN:
return getBooleanCellValue() ? "TRUE" : "FALSE";
case CELL_TYPE_ERROR:
return ErrorEval.getText(getErrorCellValue());
case CELL_TYPE_FORMULA:
return getCellFormula();
case CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(this)) {
DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", LocaleUtil.getUserLocale());
return sdf.format(getDateCellValue());
}
return getNumericCellValue() + "";
case CELL_TYPE_STRING:
return getRichStringCellValue().toString();
default:
return "Unknown Cell Type: " + getCellType();
}
}
void removeProperty(int type)
{
Property current=_firstProperty;
Property previous=null;
while(current!=null&¤t.getType()!=type)
{
previous=current;
current=current._next;
}
if(current!=null)
{
if(previous!=null)
{
previous._next=current._next;
}
else
{
_firstProperty=current._next;
}
}
}
void setProperty(int type,Object value)
{
Property current=_firstProperty;
Property previous=null;
while(current!=null&¤t.getType()!=type)
{
previous=current;
current=current._next;
}
if(current!=null)
{
current.setValue(value);
}
else
{
switch(type)
{
case Property.COMMENT:
{
current=new CommentProperty(value);
break;
}
case Property.HYPERLINK:
{
current=new HyperlinkProperty(value);
break;
}
}
if(previous!=null)
{
previous._next=current;
}
else
{
_firstProperty=current;
}
}
}
Object getPropertyValue(int type)
{
return getPropertyValue(type,null);
}
Object getPropertyValue(int type,String defaultValue)
{
Property current=_firstProperty;
while(current!=null&¤t.getType()!=type) current=current._next;
return current==null?defaultValue:current.getValue();
}
void ensurePlainStringType()
{
if(_value.getType()!=CELL_TYPE_STRING
||((StringValue)_value).isRichText())
_value=new PlainStringValue();
}
void ensureRichTextStringType()
{
if(_value.getType()!=CELL_TYPE_STRING
||!((StringValue)_value).isRichText())
_value=new RichTextValue();
}
void ensureType(int type)
{
if(_value.getType()!=type)
setType(type);
}
void ensureFormulaType(int type)
{
if(_value.getType()!=CELL_TYPE_FORMULA
||((FormulaValue)_value).getFormulaType()!=type)
setFormulaType(type);
}
void ensureTypeOrFormulaType(int type)
{
if(_value.getType()==type)
{
if(type==CELL_TYPE_STRING&&((StringValue)_value).isRichText())
setType(CELL_TYPE_STRING);
return;
}
if(_value.getType()==CELL_TYPE_FORMULA)
{
if(((FormulaValue)_value).getFormulaType()==type)
return;
setFormulaType(type); // once a formula, always a formula
return;
}
setType(type);
}
void setType(int type)
{
switch(type)
{
case CELL_TYPE_NUMERIC:
{
_value=new NumericValue();
break;
}
case CELL_TYPE_STRING:
{
PlainStringValue sval = new PlainStringValue();
if(_value != null){
// if a cell is not blank then convert the old value to string
String str = convertCellValueToString();
sval.setValue(str);
}
_value = sval;
break;
}
case CELL_TYPE_FORMULA:
{
_value=new NumericFormulaValue();
break;
}
case CELL_TYPE_BLANK:
{
_value=new BlankValue();
break;
}
case CELL_TYPE_BOOLEAN:
{
BooleanValue bval = new BooleanValue();
if(_value != null){
// if a cell is not blank then convert the old value to string
boolean val = convertCellValueToBoolean();
bval.setValue(val);
}
_value = bval;
break;
}
case CELL_TYPE_ERROR:
{
_value=new ErrorValue();
break;
}
default:
{
throw new IllegalArgumentException("Illegal type " + type);
}
}
}
void setFormulaType(int type)
{
switch(type)
{
case CELL_TYPE_NUMERIC:
{
_value=new NumericFormulaValue();
break;
}
case CELL_TYPE_STRING:
{
_value=new StringFormulaValue();
break;
}
case CELL_TYPE_BOOLEAN:
{
_value=new BooleanFormulaValue();
break;
}
case CELL_TYPE_ERROR:
{
_value=new ErrorFormulaValue();
break;
}
default:
{
throw new IllegalArgumentException("Illegal type " + type);
}
}
}
//TODO: implement this correctly
int computeTypeFromFormula(String formula)
{
return CELL_TYPE_NUMERIC;
}
//COPIED FROM https://svn.apache.org/repos/asf/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java since the functions are declared private there
/**
* Used to help format error messages
*/
private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) {
String msg = "Cannot get a "
+ getCellTypeName(expectedTypeCode) + " value from a "
+ getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell";
return new IllegalStateException(msg);
}
/**
* Used to help format error messages
*/
private static String getCellTypeName(int cellTypeCode) {
switch (cellTypeCode) {
case CELL_TYPE_BLANK: return "blank";
case CELL_TYPE_STRING: return "text";
case CELL_TYPE_BOOLEAN: return "boolean";
case CELL_TYPE_ERROR: return "error";
case CELL_TYPE_NUMERIC: return "numeric";
case CELL_TYPE_FORMULA: return "formula";
}
return "#unknown cell type (" + cellTypeCode + ")#";
}
private boolean convertCellValueToBoolean() {
int cellType = getCellType();
if (cellType == CELL_TYPE_FORMULA) {
cellType = getCachedFormulaResultType();
}
switch (cellType) {
case CELL_TYPE_BOOLEAN:
return getBooleanCellValue();
case CELL_TYPE_STRING:
String text = getStringCellValue();
return Boolean.parseBoolean(text);
case CELL_TYPE_NUMERIC:
return getNumericCellValue() != 0;
case CELL_TYPE_ERROR:
case CELL_TYPE_BLANK:
return false;
}
throw new RuntimeException("Unexpected cell type (" + cellType + ")");
}
private String convertCellValueToString() {
int cellType = getCellType();
return convertCellValueToString(cellType);
}
private String convertCellValueToString(int cellType) {
switch (cellType) {
case CELL_TYPE_BLANK:
return "";
case CELL_TYPE_BOOLEAN:
return getBooleanCellValue() ? "TRUE" : "FALSE";
case CELL_TYPE_STRING:
return getStringCellValue();
case CELL_TYPE_NUMERIC:
return Double.toString( getNumericCellValue() );
case CELL_TYPE_ERROR:
byte errVal = getErrorCellValue();
return FormulaError.forInt(errVal).getString();
case CELL_TYPE_FORMULA:
if (_value != null) {
FormulaValue fv = (FormulaValue)_value;
if (fv.getFormulaType() != CELL_TYPE_FORMULA) {
return convertCellValueToString(fv.getFormulaType());
}
}
return "";
default:
throw new IllegalStateException("Unexpected cell type (" + cellType + ")");
}
}
//END OF COPIED CODE
static abstract class Property
{
final static int COMMENT=1;
final static int HYPERLINK=2;
Object _value;
Property _next;
public Property(Object value)
{
_value=value;
}
abstract int getType();
void setValue(Object value)
{
_value=value;
}
Object getValue()
{
return _value;
}
}
static class CommentProperty extends Property
{
public CommentProperty(Object value)
{
super(value);
}
@Override
public int getType()
{
return COMMENT;
}
}
static class HyperlinkProperty extends Property
{
public HyperlinkProperty(Object value)
{
super(value);
}
@Override
public int getType()
{
return HYPERLINK;
}
}
interface Value
{
int getType();
}
static class NumericValue implements Value
{
double _value;
public int getType()
{
return CELL_TYPE_NUMERIC;
}
void setValue(double value)
{
_value=value;
}
double getValue()
{
return _value;
}
}
static abstract class StringValue implements Value
{
public int getType()
{
return CELL_TYPE_STRING;
}
//We cannot introduce a new type CELL_TYPE_RICH_TEXT because the types are public so we have to make rich text as a type of string
abstract boolean isRichText(); // using the POI style which seems to avoid "instanceof".
}
static class PlainStringValue extends StringValue
{
String _value;
void setValue(String value)
{
_value=value;
}
String getValue()
{
return _value;
}
@Override
boolean isRichText()
{
return false;
}
}
static class RichTextValue extends StringValue
{
RichTextString _value;
@Override
public int getType()
{
return CELL_TYPE_STRING;
}
void setValue(RichTextString value)
{
_value=value;
}
RichTextString getValue()
{
return _value;
}
@Override
boolean isRichText()
{
return true;
}
}
static abstract class FormulaValue implements Value
{
String _value;
public int getType()
{
return CELL_TYPE_FORMULA;
}
void setValue(String value)
{
_value=value;
}
String getValue()
{
return _value;
}
abstract int getFormulaType();
}
static class NumericFormulaValue extends FormulaValue
{
double _preEvaluatedValue;
@Override
int getFormulaType()
{
return CELL_TYPE_NUMERIC;
}
void setPreEvaluatedValue(double value)
{
_preEvaluatedValue=value;
}
double getPreEvaluatedValue()
{
return _preEvaluatedValue;
}
}
static class StringFormulaValue extends FormulaValue
{
String _preEvaluatedValue;
@Override
int getFormulaType()
{
return CELL_TYPE_STRING;
}
void setPreEvaluatedValue(String value)
{
_preEvaluatedValue=value;
}
String getPreEvaluatedValue()
{
return _preEvaluatedValue;
}
}
static class BooleanFormulaValue extends FormulaValue
{
boolean _preEvaluatedValue;
@Override
int getFormulaType()
{
return CELL_TYPE_BOOLEAN;
}
void setPreEvaluatedValue(boolean value)
{
_preEvaluatedValue=value;
}
boolean getPreEvaluatedValue()
{
return _preEvaluatedValue;
}
}
static class ErrorFormulaValue extends FormulaValue
{
byte _preEvaluatedValue;
@Override
int getFormulaType()
{
return CELL_TYPE_ERROR;
}
void setPreEvaluatedValue(byte value)
{
_preEvaluatedValue=value;
}
byte getPreEvaluatedValue()
{
return _preEvaluatedValue;
}
}
static class BlankValue implements Value
{
public int getType()
{
return CELL_TYPE_BLANK;
}
}
static class BooleanValue implements Value
{
boolean _value;
public int getType()
{
return CELL_TYPE_BOOLEAN;
}
void setValue(boolean value)
{
_value=value;
}
boolean getValue()
{
return _value;
}
}
static class ErrorValue implements Value
{
byte _value;
public int getType()
{
return CELL_TYPE_ERROR;
}
void setValue(byte value)
{
_value=value;
}
byte getValue()
{
return _value;
}
}
}
|