| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Cell; |
| 9 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
| 10 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
| 11 |
use TablePress\PhpOffice\PhpSpreadsheet\NamedRange; |
| 12 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 13 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 14 |
|
| 15 |
class Value |
| 16 |
{ |
| 17 |
use ArrayEnabled; |
| 18 |
|
| 19 |
/** |
| 20 |
* IS_BLANK. |
| 21 |
* |
| 22 |
* @param mixed $value Value to check |
| 23 |
* Or can be an array of values |
| 24 |
* |
| 25 |
* @return array<mixed>|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
| 26 |
* with the same dimensions |
| 27 |
*/ |
| 28 |
public static function isBlank($value = null) |
| 29 |
{ |
| 30 |
if (is_array($value)) { |
| 31 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
| 32 |
} |
| 33 |
|
| 34 |
return $value === null; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* IS_REF. |
| 39 |
* |
| 40 |
* @param mixed $value Value to check |
| 41 |
*/ |
| 42 |
public static function isRef($value, ?Cell $cell = null): bool |
| 43 |
{ |
| 44 |
if ($cell === null) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
$value = StringHelper::convertToString($value); |
| 49 |
$cellValue = Functions::trimTrailingRange($value); |
| 50 |
if (preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/ui', $cellValue) === 1) { |
| 51 |
[$worksheet, $cellValue] = Worksheet::extractSheetTitle($cellValue, true, true); |
| 52 |
if (!empty($worksheet) && $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheet) === null) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
try { |
| 57 |
[$column, $row] = Coordinate::indexesFromString($cellValue ?? ''); //* @phpstan-ignore nullCoalesce.variable (Not sure whether Phpstan is correct) |
| 58 |
} catch (SpreadsheetException $exception) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
$namedRange = $cell->getWorksheet()->getParentOrThrow()->getNamedRange($value); |
| 66 |
|
| 67 |
return $namedRange instanceof NamedRange; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* IS_EVEN. |
| 72 |
* |
| 73 |
* @param mixed $value Value to check |
| 74 |
* Or can be an array of values |
| 75 |
* |
| 76 |
* @return array<mixed>|bool|string If an array of numbers is passed as an argument, then the returned result will also be an array |
| 77 |
* with the same dimensions |
| 78 |
*/ |
| 79 |
public static function isEven($value = null) |
| 80 |
{ |
| 81 |
if (is_array($value)) { |
| 82 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
| 83 |
} |
| 84 |
|
| 85 |
if ($value === null) { |
| 86 |
return ExcelError::NAME(); |
| 87 |
} |
| 88 |
if (!is_numeric($value)) { |
| 89 |
return ExcelError::VALUE(); |
| 90 |
} |
| 91 |
|
| 92 |
return ((int) fmod($value + 0, 2)) === 0; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* IS_ODD. |
| 97 |
* |
| 98 |
* @param mixed $value Value to check |
| 99 |
* Or can be an array of values |
| 100 |
* |
| 101 |
* @return array<mixed>|bool|string If an array of numbers is passed as an argument, then the returned result will also be an array |
| 102 |
* with the same dimensions |
| 103 |
*/ |
| 104 |
public static function isOdd($value = null) |
| 105 |
{ |
| 106 |
if (is_array($value)) { |
| 107 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
| 108 |
} |
| 109 |
|
| 110 |
if ($value === null) { |
| 111 |
return ExcelError::NAME(); |
| 112 |
} |
| 113 |
if (!is_numeric($value)) { |
| 114 |
return ExcelError::VALUE(); |
| 115 |
} |
| 116 |
|
| 117 |
return ((int) fmod($value + 0, 2)) !== 0; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* IS_NUMBER. |
| 122 |
* |
| 123 |
* @param mixed $value Value to check |
| 124 |
* Or can be an array of values |
| 125 |
* |
| 126 |
* @return array<mixed>|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
| 127 |
* with the same dimensions |
| 128 |
*/ |
| 129 |
public static function isNumber($value = null) |
| 130 |
{ |
| 131 |
if (is_array($value)) { |
| 132 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
| 133 |
} |
| 134 |
|
| 135 |
if (is_string($value)) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
return is_numeric($value); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* IS_LOGICAL. |
| 144 |
* |
| 145 |
* @param mixed $value Value to check |
| 146 |
* Or can be an array of values |
| 147 |
* |
| 148 |
* @return array<mixed>|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
| 149 |
* with the same dimensions |
| 150 |
*/ |
| 151 |
public static function isLogical($value = null) |
| 152 |
{ |
| 153 |
if (is_array($value)) { |
| 154 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
| 155 |
} |
| 156 |
|
| 157 |
return is_bool($value); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* IS_TEXT. |
| 162 |
* |
| 163 |
* @param mixed $value Value to check |
| 164 |
* Or can be an array of values |
| 165 |
* |
| 166 |
* @return array<mixed>|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
| 167 |
* with the same dimensions |
| 168 |
*/ |
| 169 |
public static function isText($value = null) |
| 170 |
{ |
| 171 |
if (is_array($value)) { |
| 172 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
| 173 |
} |
| 174 |
|
| 175 |
return is_string($value) && !ErrorValue::isError($value); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* IS_NONTEXT. |
| 180 |
* |
| 181 |
* @param mixed $value Value to check |
| 182 |
* Or can be an array of values |
| 183 |
* |
| 184 |
* @return array<mixed>|bool If an array of numbers is passed as an argument, then the returned result will also be an array |
| 185 |
* with the same dimensions |
| 186 |
*/ |
| 187 |
public static function isNonText($value = null) |
| 188 |
{ |
| 189 |
if (is_array($value)) { |
| 190 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); |
| 191 |
} |
| 192 |
|
| 193 |
return !self::isText($value); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* ISFORMULA. |
| 198 |
* |
| 199 |
* @param mixed $cellReference The cell to check |
| 200 |
* @param ?Cell $cell The current cell (containing this formula) |
| 201 |
* |
| 202 |
* @return array<mixed>|bool|string |
| 203 |
*/ |
| 204 |
public static function isFormula($cellReference = '', ?Cell $cell = null) |
| 205 |
{ |
| 206 |
if ($cell === null) { |
| 207 |
return ExcelError::REF(); |
| 208 |
} |
| 209 |
$cellReference = StringHelper::convertToString($cellReference); |
| 210 |
|
| 211 |
$fullCellReference = Functions::expandDefinedName($cellReference, $cell); |
| 212 |
|
| 213 |
if (str_contains($cellReference, '!')) { |
| 214 |
$cellReference = Functions::trimSheetFromCellReference($cellReference); |
| 215 |
$cellReferences = Coordinate::extractAllCellReferencesInRange($cellReference); |
| 216 |
if (count($cellReferences) > 1) { |
| 217 |
return self::evaluateArrayArgumentsSubset([self::class, __FUNCTION__], 1, $cellReferences, $cell); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
$fullCellReference = Functions::trimTrailingRange($fullCellReference); |
| 222 |
|
| 223 |
$worksheetName = ''; |
| 224 |
if (1 == preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $fullCellReference, $matches)) { |
| 225 |
$fullCellReference = $matches[6] . $matches[7]; |
| 226 |
$worksheetName = str_replace("''", "'", trim($matches[2], "'")); |
| 227 |
} |
| 228 |
|
| 229 |
$worksheet = (!empty($worksheetName)) |
| 230 |
? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName) |
| 231 |
: $cell->getWorksheet(); |
| 232 |
if ($worksheet === null) { |
| 233 |
return ExcelError::REF(); |
| 234 |
} |
| 235 |
|
| 236 |
try { |
| 237 |
return $worksheet->getCell($fullCellReference)->isFormula(); |
| 238 |
} catch (SpreadsheetException $exception) { |
| 239 |
return true; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* N. |
| 245 |
* |
| 246 |
* Returns a value converted to a number |
| 247 |
* |
| 248 |
* @param null|mixed $value The value you want converted |
| 249 |
* |
| 250 |
* @return number|string N converts values listed in the following table |
| 251 |
* If value is or refers to N returns |
| 252 |
* A number That number value |
| 253 |
* A date The Excel serialized number of that date |
| 254 |
* TRUE 1 |
| 255 |
* FALSE 0 |
| 256 |
* An error value The error value |
| 257 |
* Anything else 0 |
| 258 |
*/ |
| 259 |
public static function asNumber($value = null) |
| 260 |
{ |
| 261 |
while (is_array($value)) { |
| 262 |
$value = array_shift($value); |
| 263 |
} |
| 264 |
if (is_float($value) || is_int($value)) { |
| 265 |
return $value; |
| 266 |
} |
| 267 |
if (is_bool($value)) { |
| 268 |
return (int) $value; |
| 269 |
} |
| 270 |
if (is_string($value) && str_starts_with($value, '#')) { |
| 271 |
return $value; |
| 272 |
} |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* TYPE. |
| 279 |
* |
| 280 |
* Returns a number that identifies the type of a value |
| 281 |
* |
| 282 |
* @param null|mixed $value The value you want tested |
| 283 |
* |
| 284 |
* @return int N converts values listed in the following table |
| 285 |
* If value is or refers to N returns |
| 286 |
* A number 1 |
| 287 |
* Text 2 |
| 288 |
* Logical Value 4 |
| 289 |
* An error value 16 |
| 290 |
* Array or Matrix 64 |
| 291 |
*/ |
| 292 |
public static function type($value = null): int |
| 293 |
{ |
| 294 |
$value = Functions::flattenArrayIndexed($value); |
| 295 |
if (count($value) > 1) { |
| 296 |
end($value); |
| 297 |
$a = key($value); |
| 298 |
// Range of cells is an error |
| 299 |
if (Functions::isCellValue($a)) { |
| 300 |
return 16; |
| 301 |
// Test for Matrix |
| 302 |
} elseif (Functions::isMatrixValue($a)) { |
| 303 |
return 64; |
| 304 |
} |
| 305 |
} elseif (empty($value)) { |
| 306 |
// Empty Cell |
| 307 |
return 1; |
| 308 |
} |
| 309 |
|
| 310 |
$value = Functions::flattenSingleValue($value); |
| 311 |
if (($value === null) || (is_float($value)) || (is_int($value))) { |
| 312 |
return 1; |
| 313 |
} elseif (is_bool($value)) { |
| 314 |
return 4; |
| 315 |
} elseif (is_array($value)) { |
| 316 |
return 64; |
| 317 |
} elseif (is_string($value)) { |
| 318 |
// Errors |
| 319 |
if (($value !== '') && ($value[0] == '#')) { |
| 320 |
return 16; |
| 321 |
} |
| 322 |
|
| 323 |
return 2; |
| 324 |
} |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
} |
| 329 |
|