1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Support. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zhaqq\Xlsx; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Support. |
11
|
|
|
*/ |
12
|
|
|
class Support |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param int $rowNumber |
16
|
|
|
* @param int $columnNumber |
17
|
|
|
* @param bool $absolute |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public static function xlsCell(int $rowNumber, int $columnNumber, bool $absolute = false) |
22
|
|
|
{ |
23
|
|
|
$n = $columnNumber; |
24
|
|
|
for ($r = ''; $n >= 0; $n = intval($n / 26) - 1) { |
25
|
|
|
$r = chr($n % 26 + 0x41).$r; |
26
|
|
|
} |
27
|
|
|
if ($absolute) { |
28
|
|
|
return '$'.$r.'$'.($rowNumber + 1); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $r.($rowNumber + 1); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $numFormat |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function numberFormatStandardized($numFormat) |
40
|
|
|
{ |
41
|
|
|
if ('money' == $numFormat) { |
42
|
|
|
$numFormat = 'dollar'; |
43
|
|
|
} |
44
|
|
|
if ('number' == $numFormat) { |
45
|
|
|
$numFormat = 'integer'; |
46
|
|
|
} |
47
|
|
|
switch ($numFormat) { |
48
|
|
|
case 'string': |
49
|
|
|
$numFormat = '@'; |
50
|
|
|
|
51
|
|
|
break; |
52
|
|
|
case 'integer': |
53
|
|
|
$numFormat = '0'; |
54
|
|
|
|
55
|
|
|
break; |
56
|
|
|
case 'date': |
57
|
|
|
$numFormat = 'YYYY-MM-DD'; |
58
|
|
|
|
59
|
|
|
break; |
60
|
|
|
case 'datetime': |
61
|
|
|
$numFormat = 'YYYY-MM-DD HH:MM:SS'; |
62
|
|
|
|
63
|
|
|
break; |
64
|
|
|
case 'price': |
65
|
|
|
$numFormat = '#,##0.00'; |
66
|
|
|
|
67
|
|
|
break; |
68
|
|
|
case 'float3': |
69
|
|
|
$numFormat = '#,###0.000'; |
70
|
|
|
|
71
|
|
|
break; |
72
|
|
|
case 'dollar': |
73
|
|
|
$numFormat = '[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00'; |
74
|
|
|
|
75
|
|
|
break; |
76
|
|
|
case 'euro': |
77
|
|
|
$numFormat = '#,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]'; |
78
|
|
|
|
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
$ignoreUntil = ''; |
82
|
|
|
$escaped = ''; |
83
|
|
|
for ($i = 0, $ix = strlen($numFormat); $i < $ix; ++$i) { |
84
|
|
|
$c = $numFormat[$i]; |
85
|
|
|
if ('' == $ignoreUntil && '[' == $c) { |
86
|
|
|
$ignoreUntil = ']'; |
87
|
|
|
} elseif ('' == $ignoreUntil && '"' == $c) { |
88
|
|
|
$ignoreUntil = '"'; |
89
|
|
|
} elseif ($ignoreUntil == $c) { |
90
|
|
|
$ignoreUntil = ''; |
91
|
|
|
} |
92
|
|
|
if ('' == $ignoreUntil && (' ' == $c || '-' == $c || '(' == $c || ')' == $c) && (0 == $i || '_' != $numFormat[$i - 1])) { |
93
|
|
|
$escaped .= '\\'.$c; |
94
|
|
|
} else { |
95
|
|
|
$escaped .= $c; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $escaped; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public static function determineNumberFormatType($numFormat, $function = false) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$numFormat = preg_replace("/\[(Black|Blue|Cyan|Green|Magenta|Red|White|Yellow)\]/i", '', $numFormat); |
105
|
|
|
|
106
|
|
|
switch ($numFormat) { |
107
|
|
|
case 'GENERAL': |
108
|
|
|
return 'n_auto'; |
109
|
|
|
case '@': |
110
|
|
|
return 'n_string'; |
111
|
|
|
case '0': |
112
|
|
|
return 'n_numeric'; |
113
|
|
|
case 0 != preg_match('/[H]{1,2}:[M]{1,2}(?![^"]*+")/i', $numFormat): |
114
|
|
|
case 0 != preg_match('/[M]{1,2}:[S]{1,2}(?![^"]*+")/i', $numFormat): |
115
|
|
|
return 'n_datetime'; |
116
|
|
|
case 0 != preg_match('/[Y]{2,4}(?![^"]*+")/i', $numFormat): |
117
|
|
|
case 0 != preg_match('/[D]{1,2}(?![^"]*+")/i', $numFormat): |
118
|
|
|
case 0 != preg_match('/[M]{1,2}(?![^"]*+")/i', $numFormat): |
119
|
|
|
return 'n_date'; |
120
|
|
|
case 0 != preg_match('/$(?![^"]*+")/', $numFormat): |
121
|
|
|
case 0 != preg_match('/%(?![^"]*+")/', $numFormat): |
122
|
|
|
case 0 != preg_match('/0(?![^"]*+")/', $numFormat): |
123
|
|
|
return 'n_numeric'; |
124
|
|
|
default: |
125
|
|
|
return 'n_auto'; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $haystack |
131
|
|
|
* @param $needle |
132
|
|
|
* |
133
|
|
|
* @return false|int|string |
134
|
|
|
*/ |
135
|
|
|
public static function add2listGetIndex(&$haystack, $needle) |
136
|
|
|
{ |
137
|
|
|
$existingIdx = array_search($needle, $haystack, $strict = true); |
138
|
|
|
if (false === $existingIdx) { |
139
|
|
|
$existingIdx = count($haystack); |
140
|
|
|
$haystack[] = $needle; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $existingIdx; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $val |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public static function xmlSpecialChars($val) |
152
|
|
|
{ |
153
|
|
|
//note, badchars does not include \t\n\r (\x09\x0a\x0d) |
154
|
|
|
static $badchars = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x7f"; |
155
|
|
|
static $goodchars = ' '; |
156
|
|
|
|
157
|
|
|
return strtr(htmlspecialchars($val, ENT_QUOTES | ENT_XML1), $badchars, $goodchars); //strtr appears to be faster than str_replace |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $dateInput |
162
|
|
|
* |
163
|
|
|
* @return float|int |
164
|
|
|
*/ |
165
|
|
|
public static function convertDateTime($dateInput) |
166
|
|
|
{ |
167
|
|
|
$seconds = 0; // Time expressed as fraction of 24h hours in seconds |
168
|
|
|
$year = $month = $day = 0; |
169
|
|
|
|
170
|
|
|
$date_time = $dateInput; |
171
|
|
|
if (preg_match("/(\d{4})\-(\d{2})\-(\d{2})/", $date_time, $matches)) { |
172
|
|
|
list($junk, $year, $month, $day) = $matches; |
173
|
|
|
} |
174
|
|
|
if (preg_match("/(\d+):(\d{2}):(\d{2})/", $date_time, $matches)) { |
175
|
|
|
list($junk, $hour, $min, $sec) = $matches; |
176
|
|
|
$seconds = ($hour * 60 * 60 + $min * 60 + $sec) / (24 * 60 * 60); |
177
|
|
|
} |
178
|
|
|
unset($junk); |
179
|
|
|
|
180
|
|
|
//using 1900 as epoch, not 1904, ignoring 1904 special case |
181
|
|
|
// Special cases for Excel. |
182
|
|
|
if ('1899-12-31' == "$year-$month-$day") { |
183
|
|
|
return $seconds; |
184
|
|
|
} // Excel 1900 epoch |
185
|
|
|
if ('1900-01-00' == "$year-$month-$day") { |
186
|
|
|
return $seconds; |
187
|
|
|
} // Excel 1900 epoch |
188
|
|
|
if ('1900-02-29' == "$year-$month-$day") { |
189
|
|
|
return 60 + $seconds; |
190
|
|
|
} // Excel false leapday |
191
|
|
|
// We calculate the date by calculating the number of days since the epoch |
192
|
|
|
// and adjust for the number of leap days. We calculate the number of leap |
193
|
|
|
// days by normalising the year in relation to the epoch. Thus the year 2000 |
194
|
|
|
// becomes 100 for 4 and 100 year leapdays and 400 for 400 year leapdays. |
195
|
|
|
$epoch = 1900; |
196
|
|
|
$offset = 0; |
197
|
|
|
$norm = 300; |
198
|
|
|
$range = $year - $epoch; |
199
|
|
|
// Set month days and check for leap year. |
200
|
|
|
$leap = ((0 == $year % 400) || ((0 == $year % 4) && ($year % 100))) ? 1 : 0; |
201
|
|
|
$mdays = [31, ($leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
202
|
|
|
// Some boundary checks |
203
|
|
|
if ($year < $epoch || $year > 9999) { |
204
|
|
|
return 0; |
205
|
|
|
} |
206
|
|
|
if ($month < 1 || $month > 12) { |
207
|
|
|
return 0; |
208
|
|
|
} |
209
|
|
|
if ($day < 1 || $day > $mdays[$month - 1]) { |
210
|
|
|
return 0; |
211
|
|
|
} |
212
|
|
|
// Accumulate the number of days since the epoch. |
213
|
|
|
$days = $day; // Add days for current month |
214
|
|
|
$days += array_sum(array_slice($mdays, 0, $month - 1)); // Add days for past months |
215
|
|
|
$days += $range * 365; // Add days for past years |
216
|
|
|
$days += intval(($range) / 4); // Add leapdays |
217
|
|
|
$days -= intval(($range + $offset) / 100); // Subtract 100 year leapdays |
218
|
|
|
$days += intval(($range + $offset + $norm) / 400); // Add 400 year leapdays |
219
|
|
|
$days -= $leap; // Already counted above |
220
|
|
|
// Adjust for Excel erroneously treating 1900 as a leap year. |
221
|
|
|
if ($days > 59) { |
222
|
|
|
++$days; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $days + $seconds; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public static function sanitizeSheetname($sheetname) |
229
|
|
|
{ |
230
|
|
|
static $badchars = '\\/?*:[]'; |
231
|
|
|
static $goodchars = ' '; |
232
|
|
|
$sheetname = strtr($sheetname, $badchars, $goodchars); |
233
|
|
|
$sheetname = mb_substr($sheetname, 0, 31); |
234
|
|
|
$sheetname = trim(trim(trim($sheetname), "'")); //trim before and after trimming single quotes |
235
|
|
|
|
236
|
|
|
return !empty($sheetname) ? $sheetname : 'Sheet'.((rand() % 900) + 100); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.