-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cell.pm
1082 lines (671 loc) · 21.1 KB
/
Cell.pm
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
package Spreadsheet::XLSX::Reader::LibXML::Cell;
use version; our $VERSION = qv('v0.36.28');
$| = 1;
use 5.010;
use Moose;
use MooseX::StrictConstructor;
use MooseX::HasDefaults::RO;
use Types::Standard qw(
Str InstanceOf HashRef
Enum HasMethods ArrayRef
Int Maybe CodeRef
is_Object
);
use lib '../../../../../lib';
###LogSD use Log::Shiras::Telephone;
###LogSD use Log::Shiras::UnhideDebug;
###LogSD with 'Log::Shiras::LogSpace';
use Spreadsheet::XLSX::Reader::LibXML::Types qw(
CellID
);
#########1 Public Attributes 3#########4#########5#########6#########7#########8#########9
has error_inst =>(
isa => InstanceOf[ 'Spreadsheet::XLSX::Reader::LibXML::Error' ],
clearer => '_clear_error_inst',
reader => '_get_error_inst',
required => 1,
handles =>[ qw(
error set_error clear_error set_warnings if_warn
) ],
);
has cell_unformatted =>(
isa => Maybe[Str],
reader => '_unformatted',
predicate => 'has_unformatted',
#~ default => '',
);
has rich_text =>(
isa => ArrayRef,
reader => 'get_rich_text',
predicate => 'has_rich_text',
);
has cell_font =>(
isa => HashRef,
reader => 'get_font',
predicate => 'has_font',
);
has cell_border =>(
isa => HashRef,
reader => 'get_border',
predicate => 'has_border',
);
has cell_style =>(
isa => HashRef,
reader => 'get_style',
predicate => 'has_style',
);
has cell_fill =>(
isa => HashRef,
reader => 'get_fill',
predicate => 'has_fill',
);
has cell_type =>(
isa => Enum[qw( Text Numeric Date Custom )],
reader => 'type',
writer => '_set_cell_type',
predicate => 'has_type',
);
has cell_encoding =>(
isa => Str,
reader => 'encoding',
predicate => 'has_encoding',
);
has cell_merge =>(
isa => Str,
reader => 'merge_range',
predicate => 'is_merged',
);
has cell_formula =>(
isa => Str,
reader => 'formula',
predicate => 'has_formula',
);
has cell_row =>(
isa => Int,
reader => 'row',
predicate => 'has_row',
);
has cell_col =>(
isa => Int,
reader => 'col',
predicate => 'has_col',
);
has r =>(
isa => CellID,
reader => 'cell_id',
predicate => 'has_cell_id',
);
has cell_hyperlink =>(
isa => ArrayRef,
reader => 'get_hyperlink',
predicate => 'has_hyperlink',
);
has unformatted_converter =>(
isa => CodeRef,
reader => '_convert_output',
required => 1,
);
has cell_coercion =>(
isa => HasMethods[ 'assert_coerce', 'display_name' ],
reader => 'get_coercion',
writer => 'set_coercion',
predicate => 'has_coercion',
clearer => 'clear_coercion',
handles =>{
coercion_name => 'display_name',
},
);
#########1 Public Methods 3#########4#########5#########6#########7#########8#########9
sub unformatted{
my( $self, ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new(
###LogSD name_space => $self->get_log_space . '::unformatted', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Attempting to return the unformatted value of the cell" ] );
###LogSD $phone->talk( level => 'trace', message => [ "Cell:", $self ] );
# check if empty
return undef if !$self->has_unformatted;
# get the value
my $unformatted = $self->_unformatted;
###LogSD $phone->talk( level => 'debug', message => [ "unformatted:", $unformatted ] );
my $converter = $self->_convert_output;
$unformatted = $converter->( $unformatted );
###LogSD $phone->talk( level => 'debug', message => [ "converted:", $unformatted ] );
return $unformatted;
}
sub value{
my( $self, ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new(
###LogSD name_space => $self->get_log_space . '::value', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD 'Reached the -value- function' ] );
###LogSD $phone->talk( level => 'trace', message => [ "Cell:", $self ] );
my $unformatted = $self->unformatted;
return $self->_return_value_only(
$unformatted,
$self->get_coercion,
$self->_get_error_inst,
###LogSD $self->get_log_space,
);
}
#~ sub get_merge_range{
#~ my( $self, $modifier ) = @_;
#~ ###LogSD my $phone = Log::Shiras::Telephone->new(
#~ ###LogSD name_space => $self->get_log_space . '::get_merge_range', );
#~ if( !$self->is_merged ){
#~ $self->set_error(
#~ "Attempted to collect merge range but the cell is not merged with any others"
#~ );
#~ return undef;
#~ }
#~ my $merge_range = $self->merge_range;
#~ ###LogSD $phone->talk( level => 'debug', message => [
#~ ###LogSD "Returning merge_range: $merge_range",
#~ ###LogSD (( $modifier ) ? "Modified by: $modifier" : ''),
#~ ###LogSD ] );
#~ if( $modifier ){
#~ if( $modifier eq 'array' ){
#~ my ( $start, $end ) = split /:/, $merge_range;
#~ my ( $start_col, $start_row, $end_col, $end_row ) =
#~ ( $self->parse_column_row( $start ), $self->parse_column_row( $end ) );
#~ $merge_range = [ [ $start_col, $start_row ], [ $end_col, $end_row ] ];
#~ }else{
#~ $self->set_error(
#~ "Un-recognized modifier -$modifier- passed to 'get_merge_range' - it only accepts 'array'"
#~ );
#~ }
#~ }
#~ ###LogSD $phone->talk( level => 'info', message => [
#~ ###LogSD "Final merge range:", $merge_range ] );
#~ return $merge_range;
#~ }
#########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
#########1 Private Methods 3#########4#########5#########6#########7#########8#########9
after 'set_coercion' => sub{
my ( $self, ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD ($self->get_log_space . '::set_coercion' ), );
###LogSD $phone->talk( level => 'debug', message =>[
###LogSD "Setting 'cell_type' to custom since the coercion has been set" ] );
$self->_set_cell_type( 'Custom' );
};
sub _return_value_only{
my ( $self, $unformatted, $coercion, $error_inst, $log_space ) = @_;# To be used by GetCell too
###LogSD my $phone = Log::Shiras::Telephone->new( name_space =>
###LogSD ($log_space . '::_return_value_only' ), );
###LogSD $phone->talk( level => 'debug', message =>[
###LogSD "Returning the coerced value of: " . ($unformatted ? $unformatted : ''),
###LogSD ($coercion ? ( '..using coercion:' , $coercion ) : ''), ] );
my $formatted = $unformatted;
if( !$coercion ){
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "No coercion passed" ) ] );
return $unformatted;
}elsif( !defined $unformatted ){
$error_inst->set_error( "The cell does not have a value" );
}elsif( $unformatted eq '' ){
$error_inst->set_error( "The cell has the empty string for a value" );
}else{
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Attempting to return the value of the cell formatted to " .
###LogSD (($coercion) ? $coercion->display_name : 'No conversion available' ) ] );
my $sig_warn = $SIG{__WARN__};
$SIG{__WARN__} = sub{};
eval '$formatted = $coercion->assert_coerce( $unformatted )';
$error_inst->set_error( $@ ) if( $@ );
$SIG{__WARN__} = $sig_warn;
}
$formatted =~ s/\\//g if $formatted;
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "Format is:", $coercion->display_name,
###LogSD "Returning the formated value: " .
###LogSD ( $formatted ? $formatted : '' ), ] );
return $formatted;
}
#########1 Private Methods 3#########4#########5#########6#########7#########8#########9
sub DEMOLISH{
my ( $self ) = @_;
###LogSD my $phone = Log::Shiras::Telephone->new(
###LogSD name_space => $self->get_log_space . '::Cell::DEMOLISH', );
###LogSD $phone->talk( level => 'debug', message => [
###LogSD "clearing the cell for cell ID:" . $self->cell_id, ] );
#~ print "Clearing coercion\n";
$self->clear_coercion;
#~ print "Clearing error instance\n";
$self->_clear_error_inst;
}
#########1 Phinish 3#########4#########5#########6#########7#########8#########9
no Moose;
__PACKAGE__->meta->make_immutable;
1;
#########1 Documentation 3#########4#########5#########6#########7#########8#########9
__END__
=head1 NAME
Spreadsheet::XLSX::Reader::LibXML::Cell - XLSX Cell data class
=head1 SYNOPSIS
See the SYNOPSIS in the L<Workbook class|Spreadsheet::XLSX::Reader::LibXML/SYNOPSIS>
=head1 DESCRIPTION
This is the class that contains cell data. There are no XML parsing actions taken in the
background of this class. All data has been pre-coalated/built from the L<Worksheet
|Spreadsheet::XLSX::Reader::LibXML::Worksheet> class. In general the Worksheet class
will populate the attributes of this class when it is generated. If you want to use it
as a standalone class just fill in the L<Attributes|/Attributes> below.
=head2 Primary Methods
These are methods used to transform data stored in the L<Attributes|/Attributes>
(not just return it directly). All methods are object methods and should be implemented
on the instance.
B<Example:>
my $unformatted_value = $cell_intance->unformatted;
=head3 unformatted
=over
B<Definition:> Returns the unformatted value of the cell transformed with the
L<change_output_encoding|Spreadsheet::XLSX::Reader::LibXML::FmtDefault/change_output_encoding( $string )>
method.
B<Accepts:>Nothing
B<Returns:> the cell value processed by the encoding conversion
=back
=head3 has_unformatted
=over
B<Definition:> This is a predicate method to determine if the cell had any value stored in it.
Sometimes this class will be generated by the
L<Worksheet|Spreadsheet::XLSX::Reader::LibXML::Worksheet> class when there is cell formatting but
no value. Ex. Merged cells store the value in the left upper corner of the merged area but have
cell specific formatting for all cells in the merge area.
B<Accepts:>Nothing
B<Returns:> True if the cell holds an unformatted value (even if it is just a space or empty string)
=back
=head3 value
=over
B<Definition:> Returns the formatted value of the cell transformed from the
L<unformatted|/unformatted> string. This method uses the conversion stored in the
L<cell_coercion|/cell_coercion> attribute. If there is no format/conversion set
then this will return the unformatted value. Any failures to process this value can be
retrieved with L<$self-E<gt>error|/error>.
B<Accepts:>Nothing
B<Returns:> the cell value processed by the set conversion
=back
=head2 Attributes
This class is just a storage of coallated information about the requested cell stored
in the following attributes. For more information on attributes see
L<Moose::Manual::Attributes>. The meta data about the cell can be retrieved from each
attribute using the 'attribute methods'.
=head3 error_inst
=over
B<Definition:> This attribute holds an 'error' object instance. In general
the package shares a reference for this instance accross the workbook with all
worksheets and all cells so any set or get action should return the latest error state
from anywhere. (not just the instance you are working on)
B<Default:> a L<Spreadsheet::XLSX::Reader::LibXML::Error> instance with the
attributes set as;
( should_warn => 0 )
B<Range:> The minimum list of methods to implement for your own instance is;
error set_error clear_error set_warnings if_warn
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_error_inst>
=over
B<Definition:> returns this instance
=back
B<error>
=over
B<Definition:> Used to get the most recently logged error
=back
B<set_error>
=over
B<Definition:> used to set a new error string
=back
B<clear_error>
=over
B<Definition:> used to clear the current error string in this attribute
=back
B<set_warnings>
=over
B<Definition:> used to turn on or off real time warnings when errors are set.
This is a delegated method from the error instance.
=back
B<if_warn>
=over
B<Definition:> a method mostly used to extend this package and see if warnings
should be emitted.
=back
=back
=back
=head3 cell_unformatted
=over
B<Definition:> This holds the unformatted value of the cell (if any)
B<Default:> undef
B<Range:> a string
B<attribute methods> Methods provided to adjust this attribute
=over
B<unformatted>
=over
B<Definition:> returns the attribute value
=back
=over
B<has_unformatted>
=over
B<Definition:> a predicate method to determine if any value is in the cell
=back
=back
=back
=back
=head3 rich_text
=over
B<Definition:> This attribute hold a rich text data structure like
L<Spreadsheet::ParseExcel::Cell/get_rich_text()> with the exception that it
doesn't bless each hashref into an object. The hashref's are also organized
per the Excel xlsx information in the the sharedStrings.xml file. In general
this is an arrayref of arrayrefs where the second level contains two positions.
The first position is the place (from zero) where the formatting is implemented.
The second position is a hashref of the formatting values. The format is inforce
until the next start place is identified.
=over
B<note:> It is important to understand that Excel can store two formats for the
same cell and often they don't agree. For example using the attribute L<cell_font
|/cell_font> will not always contain the same value as specific fonts (or any font)
listed in the rich text array.
f
=back
B<Default:> undef = no rich text defined for this cell
B<Range:> an array ref of rich_text positions and definitions
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_rich_text>
=over
B<Definition:> returns the attribute value
=back
B<has_rich_text>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=back
=head3 cell_font
=over
B<Definition:> This holds the font assigned to the cell
B<Default:> undef
B<Range:> a hashref of definitions for the font
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_font>
=over
B<Definition:> returns the attribute value
=back
B<has_font>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_border
=over
B<Definition:> This holds the border settings assigned to the cell
B<Default:> undef
B<Range:> a hashref of border definitions
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_border>
=over
B<Definition:> returns the attribute value
=back
B<has_border>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_style
=over
B<Definition:> This holds the border settings assigned to the cell
B<Default:> undef
B<Range:> a hashref of style definitions
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_style>
=over
B<Definition:> returns the attribute value
=back
B<has_style>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_fill
=over
B<Definition:> This holds the fill settings assigned to the cell
B<Default:> undef
B<Range:> a hashref of style definitions
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_fill>
=over
B<Definition:> returns the attribute value
=back
B<has_fill>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_type
=over
B<Definition:> This holds the type of data stored in the cell. In general it
follows the convention of L<ParseExcel
|Spreadsheet::ParseExcel/ChkType($self, $is_numeric, $format_index)> (Date, Numeric,
or Text) however, since custom coercions will change data to some possible non excel
standard state this also allows a 'Custom' type representing any cell with a custom
conversion assigned to it (by you either at the worksheet level or here).
B<Default:> Text
B<Range:> Text = Strings, Numeric = Real Numbers, Date = Real Numbers with an
assigned Date conversion, Custom = any stored value with a custom conversion
B<attribute methods> Methods provided to adjust this attribute
=over
B<type>
=over
B<Definition:> returns the attribute value
=back
B<has_type>
=over
B<Definition:> Indicates if the attribute has anything stored (Always true)
=back
=back
=back
=head3 cell_encoding
=over
B<Definition:> This holds the byte encodeing of the data stored in the cell
B<Default:> Unicode
B<Range:> Traditional encoding options
B<attribute methods> Methods provided to adjust this attribute
=over
B<encoding>
=over
B<Definition:> returns the attribute value
=back
B<has_encoding>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_merge
=over
B<Definition:> if the cell is part of a group of merged cells this will
store the upper left and lower right cell ID's in a string concatenated
with a ':'
B<Default:> undef
B<Range:> two cell ID's
B<attribute methods> Methods provided to adjust this attribute
=over
B<merge_range>
=over
B<Definition:> returns the attribute value
=back
B<is_merged>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_formula
=over
B<Definition:> if the cell value (unformatted) is calculated based on a
formula the Excel formula string is stored in this attribute.
B<Default:> undef
B<Range:> Excel formula string
B<attribute methods> Methods provided to adjust this attribute
=over
B<formula>
=over
B<Definition:> returns the attribute value
=back
B<has_formula>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_row
=over
B<Definition:> This is the sheet row that the cell was read from
B<Range:> the minimum row to the maximum row
B<attribute methods> Methods provided to adjust this attribute
=over
B<row>
=over
B<Definition:> returns the attribute value
=back
B<has_row>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_col
=over
B<Definition:> This is the sheet column that the cell was read from
B<Range:> the minimum column to the maximum column
B<attribute methods> Methods provided to adjust this attribute
=over
B<col>
=over
B<Definition:> returns the attribute value
=back
B<has_col>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 r
=over
B<Definition:> This is the cell ID of the cell
B<attribute methods> Methods provided to adjust this attribute
=over
B<cell_id>
=over
B<Definition:> returns the attribute value
=back
B<has_cell_id>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_hyperlink
=over
B<Definition:> This stores any hyperlink from the cell
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_hyperlink>
=over
B<Definition:> returns the attribute value
=back
B<has_hyperlink>
=over
B<Definition:> Indicates if the attribute has anything stored
=back
=back
=back
=head3 cell_coercion
=over
B<Definition:> This attribute holds the tranformation code to turn an
unformatted value into a formatted value.
B<Default:> a L<Type::Tiny> instance with sub types set to assign different
inbound data types to different coercions for the target outcome of formatted
data.
B<Range:> If you wish to set this with your own code it must have two
methods. First, 'assert_coerce' which will be applied when transforming
the unformatted value. Second, 'display_name' which will be used to self
identify. For an example of how to build a custom format see
L<Spreadsheet::XLSX::Reader::LibXML::Worksheet/custom_formats>.
B<attribute methods> Methods provided to adjust this attribute
=over
B<get_coercion>
=over
B<Definition:> returns this instance
=back
B<clear_coercion>
=over
B<Definition:> used to clear the this attribute
=back
B<set_coercion>
=over
B<Definition:> used to set a new coercion instance. Implementation of this
method will also switch the cell type to 'Custom'.
=back
B<has_coercion>
=over
B<Definition:> Indicate if any coecion code is applied
=back
B<coercion_name>
=over
B<Definition:> calls 'display_name' on the code in the background
=back
=back
=back
=head1 SUPPORT