| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Reader\Ods;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
6 |
|
|
|
7 |
class FormulaTranslator
|
|
|
8 |
{
|
|
|
9 |
private static function replaceQuotedPeriod(string $value): string
|
|
|
10 |
{
|
|
|
11 |
$value2 = '';
|
|
|
12 |
$quoted = false;
|
|
|
13 |
foreach (mb_str_split($value, 1, 'UTF-8') as $char) {
|
|
|
14 |
if ($char === "'") {
|
|
|
15 |
$quoted = !$quoted;
|
|
|
16 |
} elseif ($char === '.' && $quoted) {
|
|
|
17 |
$char = "\u{fffe}";
|
|
|
18 |
}
|
|
|
19 |
$value2 .= $char;
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
return $value2;
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public static function convertToExcelAddressValue(string $openOfficeAddress): string
|
|
|
26 |
{
|
|
|
27 |
// Cell range 3-d reference
|
|
|
28 |
// As we don't support 3-d ranges, we're just going to take a quick and dirty approach
|
|
|
29 |
// and assume that the second worksheet reference is the same as the first
|
|
|
30 |
$excelAddress = (string) preg_replace(
|
|
|
31 |
[
|
|
|
32 |
'/\$?([^\.]+)\.([^\.]+):\$?([^\.]+)\.([^\.]+)/miu',
|
|
|
33 |
'/\$?([^\.]+)\.([^\.]+):\.([^\.]+)/miu', // Cell range reference in another sheet
|
|
|
34 |
'/\$?([^\.]+)\.([^\.]+)/miu', // Cell reference in another sheet
|
|
|
35 |
'/\.([^\.]+):\.([^\.]+)/miu', // Cell range reference
|
|
|
36 |
'/\.([^\.]+)/miu', // Simple cell reference
|
|
|
37 |
'/\x{FFFE}/miu', // restore quoted periods
|
|
|
38 |
],
|
|
|
39 |
[
|
|
|
40 |
'$1!$2:$4',
|
|
|
41 |
'$1!$2:$3',
|
|
|
42 |
'$1!$2',
|
|
|
43 |
'$1:$2',
|
|
|
44 |
'$1',
|
|
|
45 |
'.',
|
|
|
46 |
],
|
|
|
47 |
self::replaceQuotedPeriod($openOfficeAddress)
|
|
|
48 |
);
|
|
|
49 |
|
|
|
50 |
return $excelAddress;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public static function convertToExcelFormulaValue(string $openOfficeFormula): string
|
|
|
54 |
{
|
|
|
55 |
$temp = explode(Calculation::FORMULA_STRING_QUOTE, $openOfficeFormula);
|
|
|
56 |
$tKey = false;
|
|
|
57 |
$inMatrixBracesLevel = 0;
|
|
|
58 |
$inFunctionBracesLevel = 0;
|
|
|
59 |
foreach ($temp as &$value) {
|
|
|
60 |
// @var string $value
|
|
|
61 |
// Only replace in alternate array entries (i.e. non-quoted blocks)
|
|
|
62 |
// so that conversion isn't done in string values
|
|
|
63 |
$tKey = $tKey === false;
|
|
|
64 |
if ($tKey) {
|
|
|
65 |
$value = (string) preg_replace(
|
|
|
66 |
[
|
|
|
67 |
'/\[\$?([^\.]+)\.([^\.]+):\.([^\.]+)\]/miu', // Cell range reference in another sheet
|
|
|
68 |
'/\[\$?([^\.]+)\.([^\.]+)\]/miu', // Cell reference in another sheet
|
|
|
69 |
'/\[\.([^\.]+):\.([^\.]+)\]/miu', // Cell range reference
|
|
|
70 |
'/\[\.([^\.]+)\]/miu', // Simple cell reference
|
|
|
71 |
'/\x{FFFE}/miu', // restore quoted periods
|
|
|
72 |
],
|
|
|
73 |
[
|
|
|
74 |
'$1!$2:$3',
|
|
|
75 |
'$1!$2',
|
|
|
76 |
'$1:$2',
|
|
|
77 |
'$1',
|
|
|
78 |
'.',
|
|
|
79 |
],
|
|
|
80 |
self::replaceQuotedPeriod($value)
|
|
|
81 |
);
|
|
|
82 |
// Convert references to defined names/formulae
|
|
|
83 |
$value = str_replace('$$', '', $value);
|
|
|
84 |
|
|
|
85 |
// Convert ODS function argument separators to Excel function argument separators
|
|
|
86 |
$value = Calculation::translateSeparator(';', ',', $value, $inFunctionBracesLevel);
|
|
|
87 |
|
|
|
88 |
// Convert ODS matrix separators to Excel matrix separators
|
|
|
89 |
$value = Calculation::translateSeparator(
|
|
|
90 |
';',
|
|
|
91 |
',',
|
|
|
92 |
$value,
|
|
|
93 |
$inMatrixBracesLevel,
|
|
|
94 |
Calculation::FORMULA_OPEN_MATRIX_BRACE,
|
|
|
95 |
Calculation::FORMULA_CLOSE_MATRIX_BRACE
|
|
|
96 |
);
|
|
|
97 |
$value = Calculation::translateSeparator(
|
|
|
98 |
'|',
|
|
|
99 |
';',
|
|
|
100 |
$value,
|
|
|
101 |
$inMatrixBracesLevel,
|
|
|
102 |
Calculation::FORMULA_OPEN_MATRIX_BRACE,
|
|
|
103 |
Calculation::FORMULA_CLOSE_MATRIX_BRACE
|
|
|
104 |
);
|
|
|
105 |
|
|
|
106 |
$value = (string) preg_replace('/COM\.MICROSOFT\./ui', '', $value);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
// Then rebuild the formula string
|
|
|
111 |
$excelFormula = implode('"', $temp);
|
|
|
112 |
|
|
|
113 |
return $excelFormula;
|
|
|
114 |
}
|
|
|
115 |
}
|