7
7
use Illuminate \Support \Collection ;
8
8
use Illuminate \Support \Str ;
9
9
use OpenSpout \Common \Entity \Cell ;
10
+ use OpenSpout \Common \Entity \Cell \FormulaCell ;
10
11
use OpenSpout \Common \Entity \Row ;
11
12
12
13
class Sanitize
@@ -25,7 +26,7 @@ public static function sheets(Collection $sheets): Collection
25
26
public static function cells (array $ cells , int $ length ): array
26
27
{
27
28
return Collection::wrap ($ cells )
28
- ->map (fn ($ cell ) => self ::cell ($ cell-> getValue () ))
29
+ ->map (fn ($ cell ) => self ::cell ($ cell ))
29
30
->slice (0 , $ length )
30
31
->pad ($ length , null )
31
32
->toArray ();
@@ -36,24 +37,28 @@ public static function name(string $name): string
36
37
return Str::of ($ name )->lower ()->snake ();
37
38
}
38
39
39
- private static function cell ($ cell )
40
+ private static function cell (Cell $ cell )
40
41
{
41
- if ($ cell instanceof DateTime) {
42
- return Carbon::instance ($ cell )->toDateTimeString ();
42
+ $ value = $ cell instanceof FormulaCell
43
+ ? $ cell ->getComputedValue ()
44
+ : $ cell ->getValue ();
45
+
46
+ if ($ value instanceof DateTime) {
47
+ return Carbon::instance ($ value )->toDateTimeString ();
43
48
}
44
49
45
- if (is_string ($ cell )) {
46
- $ cell = Str::of ($ cell )->trim ();
50
+ if (is_string ($ value )) {
51
+ $ value = Str::of ($ value )->trim ();
47
52
$ to = 'UTF-8 ' ;
48
- $ from = mb_detect_encoding ($ cell , ['auto ' ]);
53
+ $ from = mb_detect_encoding ($ value , ['auto ' ]);
49
54
50
55
if (! $ from ) {
51
- $ cell = '' ;
56
+ $ value = '' ;
52
57
} elseif ($ from !== $ to ) {
53
- $ cell = mb_convert_encoding ($ cell , $ to , $ from );
58
+ $ value = mb_convert_encoding ($ value , $ to , $ from );
54
59
}
55
60
}
56
61
57
- return $ cell === '' ? null : $ cell ;
62
+ return $ value === '' ? null : $ value ;
58
63
}
59
64
}
0 commit comments