@@ -23,11 +23,67 @@ public static function getSheet(Event $event): Worksheet
23
23
return $ sheet ;
24
24
}
25
25
26
- public static function getSheetMaxRowAndColumn (Event $ event ): array
26
+ public static function getSheetMaxRowAndColumnInfo (Event $ event ): array
27
27
{
28
28
$ sheet = Excel::getSheet ($ event );
29
29
30
- return $ sheet ->getHighestRowAndColumn ();
30
+ ['row ' => $ maxRow , 'column ' => $ maxColName ] = $ sheet ->getHighestRowAndColumn ();
31
+
32
+ // maxRow, maxCol 从 1 开始
33
+ $ maxCol = \PhpOffice \PhpSpreadsheet \Cell \Coordinate::columnIndexFromString ($ maxColName );
34
+ // A=65
35
+ $ maxColumnLetter = \PhpOffice \PhpSpreadsheet \Cell \Coordinate::stringFromColumnIndex ($ maxCol );
36
+
37
+ return [
38
+ 'maxRow ' => $ maxRow ,
39
+ 'maxCol ' => $ maxCol ,
40
+ 'maxColumnLetter ' => $ maxColumnLetter ,
41
+ ];
42
+ }
43
+
44
+ public static function getSheetCellNameByRowAndColumn (int $ col , int $ row )
45
+ {
46
+ $ columnLetter = Excel::getSheetColumnLetter ($ col );
47
+
48
+ $ cell = "{$ columnLetter }{$ row }" ;
49
+
50
+ return [
51
+ 'columnLetter ' => $ columnLetter ,
52
+ 'cell ' => $ cell ,
53
+ 'row ' => $ row ,
54
+ ];
55
+ }
56
+
57
+ public static function getSheetColumnLetter (int $ col )
58
+ {
59
+ $ columnLetter = \PhpOffice \PhpSpreadsheet \Cell \Coordinate::stringFromColumnIndex ($ col );
60
+
61
+ return $ columnLetter ;
62
+ }
63
+
64
+ public static function handleAllCell (Event $ event , callable $ callable )
65
+ {
66
+ $ sheet = Excel::getSheet ($ event );
67
+
68
+ $ sheetInfo = Excel::getSheetMaxRowAndColumnInfo ($ event );
69
+
70
+ foreach (range (0 , $ sheetInfo ['maxRow ' ]) as $ row ) {
71
+ foreach (range (1 , $ sheetInfo ['maxCol ' ]) as $ col ) {
72
+ $ cellInfo = Excel::getSheetCellNameByRowAndColumn ($ col , $ row );
73
+
74
+ $ callable ($ event , $ sheet , $ sheetInfo , $ cellInfo );
75
+ }
76
+ }
77
+
78
+ $ backTrace = debug_backtrace (2 , 2 );
79
+ $ callFunctionName = $ backTrace [1 ]['function ' ];
80
+
81
+ info (sprintf ("%s: 最大单元格为 %s, 最大列: %s 最大行号: %s " ,
82
+ $ callFunctionName ,
83
+ $ cellInfo ['cell ' ],
84
+ $ sheetInfo ['maxCol ' ],
85
+ $ sheetInfo ['maxRow ' ]
86
+ ));
31
87
}
32
88
33
89
/**
@@ -41,40 +97,25 @@ public static function getSheetMaxRowAndColumn(Event $event): array
41
97
*/
42
98
public static function handleCalculateSheet (Event $ event )
43
99
{
44
- $ sheet = Excel::getSheet ($ event );
45
-
46
- ['row ' => $ maxRow , 'column ' => $ maxColName ] = Excel::getSheetMaxRowAndColumn ($ event );
47
-
48
- // A=65
49
- $ maxCol = ord ($ maxColName ) - 64 ;
50
- $ maxColName = chr ($ maxCol + 65 );
51
-
52
- foreach (range (0 , $ maxRow ) as $ row ) {
53
- foreach (range (0 , $ maxCol ) as $ col ) {
54
- $ colName = chr ($ col + 65 );
55
- $ cell = "{$ colName }{$ row }" ;
56
-
57
- try {
58
- $ calcValue = $ sheet ->getCell ($ cell )->getCalculatedValue ();
59
- $ newValue = $ calcValue ;
60
- } catch (\Throwable $ e ) {
61
- $ value = $ sheet ->getCell ($ cell )->getValue ();
62
-
63
- info ("获取单元格 {$ cell } 计算结果错误 " , [
64
- 'code ' => $ e ->getCode (),
65
- 'message ' => $ e ->getMessage (),
66
- 'cell ' => $ cell ,
67
- 'origin_value ' => $ value ,
68
- ]);
69
-
70
- $ newValue = $ value ;
71
- }
72
-
73
- $ sheet ->getCell ($ cell )->setValue ($ newValue );
100
+ Excel::handleAllCell ($ event , function ($ event , $ sheet , $ sheetInfo , $ cellInfo ) {
101
+ try {
102
+ $ calcValue = $ sheet ->getCell ($ cellInfo ['cell ' ])->getCalculatedValue ();
103
+ $ newValue = $ calcValue ;
104
+ } catch (\Throwable $ e ) {
105
+ $ value = $ sheet ->getCell ($ cellInfo ['cell ' ])->getValue ();
106
+
107
+ info ("获取单元格 {$ cellInfo ['cell ' ]} 计算结果错误 " , [
108
+ 'code ' => $ e ->getCode (),
109
+ 'message ' => $ e ->getMessage (),
110
+ 'cell ' => $ cellInfo ['cell ' ],
111
+ 'origin_value ' => $ value ,
112
+ ]);
113
+
114
+ $ newValue = $ value ;
74
115
}
75
- }
76
116
77
- info ("handleCalculateSheet: 最大单元格为 {$ cell }, 最大列: {$ maxColName } 最大行号: {$ maxRow }" );
117
+ $ sheet ->getCell ($ cellInfo ['cell ' ])->setValue ($ newValue );
118
+ });
78
119
}
79
120
80
121
/**
@@ -158,53 +199,36 @@ public static function datetimeFromCell(mixed $datetime = null, $format = 'Y-m-d
158
199
*/
159
200
public static function handleRequireCellTextColorForRedAndHyperLink (Event $ event )
160
201
{
161
- $ sheet = Excel::getSheet ($ event );
162
-
163
- ['row ' => $ maxRow , 'column ' => $ maxColName ] = Excel::getSheetMaxRowAndColumn ($ event );
164
-
165
- // A=65
166
- $ maxCol = ord ($ maxColName ) - 64 ;
167
- $ maxColName = chr ($ maxCol + 65 );
168
-
169
- foreach (range (0 , $ maxRow ) as $ row ) {
170
- foreach (range (0 , $ maxCol ) as $ col ) {
171
- $ columnLetter = chr ($ col + 65 );
172
- $ cell = "{$ columnLetter }{$ row }" ;
173
-
174
- // 设置列宽 autoSize
175
- $ sheet ->getColumnDimension ($ columnLetter )->setAutoSize (true );
176
-
177
- try {
178
- $ calcValue = $ sheet ->getCell ($ cell )->getCalculatedValue ();
179
- $ newValue = $ calcValue ;
180
- } catch (\Throwable $ e ) {
181
- $ value = $ sheet ->getCell ($ cell )->getValue ();
182
-
183
- info ("获取单元格 {$ cell } 计算结果错误 " , [
184
- 'code ' => $ e ->getCode (),
185
- 'message ' => $ e ->getMessage (),
186
- 'cell ' => $ cell ,
187
- 'origin_value ' => $ value ,
188
- ]);
189
-
190
- $ newValue = $ value ;
191
- }
192
-
193
- $ newValue = $ sheet ->getCell ($ cell )->getValue ();
194
-
195
- if (str_contains ($ newValue ?? '' , '* ' )) {
196
- $ sheet ->getStyle ($ cell )->getFont ()->getColor ()->setARGB (Color::COLOR_RED );
197
- }
202
+ Excel::handleAllCell ($ event , function ($ event , $ sheet , $ sheetInfo , $ cellInfo ) {
203
+ // 设置列宽 autoSize
204
+ $ sheet ->getColumnDimension ($ cellInfo ['columnLetter ' ])->setAutoSize (true );
205
+
206
+ try {
207
+ $ calcValue = $ sheet ->getCell ($ cellInfo ['cell ' ])->getCalculatedValue ();
208
+ $ newValue = $ calcValue ;
209
+ } catch (\Throwable $ e ) {
210
+ $ value = $ sheet ->getCell ($ cellInfo ['cell ' ])->getValue ();
211
+
212
+ info ("获取单元格 {$ cellInfo ['cell ' ]} 计算结果错误 " , [
213
+ 'code ' => $ e ->getCode (),
214
+ 'message ' => $ e ->getMessage (),
215
+ 'cell ' => $ cellInfo ['cell ' ],
216
+ 'origin_value ' => $ value ,
217
+ ]);
218
+
219
+ $ newValue = $ value ;
220
+ }
198
221
199
- if (str_contains ($ newValue ?? '' , ':// ' )) {
200
- Excel:: cellAddHyperLink ( $ event , $ cell );
201
- }
222
+ if (str_contains ($ newValue ?? '' , '* ' )) {
223
+ $ sheet -> getStyle ( $ cellInfo [ ' cell ' ])-> getFont ()-> getColor ()-> setARGB (Color:: COLOR_RED );
224
+ }
202
225
203
- $ sheet ->getCell ($ cell )->setValue ($ newValue );
226
+ if (str_contains ($ newValue ?? '' , ':// ' )) {
227
+ Excel::cellAddHyperLink ($ event , $ cellInfo ['cell ' ]);
204
228
}
205
- }
206
229
207
- info ("handleRequireCellTextColorForRedAndHyperLink: 最大单元格为 {$ cell }, 最大列: {$ maxColName } 最大行号: {$ maxRow }" );
230
+ $ sheet ->getCell ($ cellInfo ['cell ' ])->setValue ($ newValue );
231
+ });
208
232
}
209
233
210
234
/**
0 commit comments