|
8 | 8 | require(__DIR__ . '/../autoload.php'); |
9 | 9 |
|
10 | 10 | use alexandrainst\XlsxFastEditor\XlsxFastEditor; |
| 11 | +use alexandrainst\XlsxFastEditor\XlsxFastEditorException; |
11 | 12 |
|
12 | 13 | copy(__DIR__ . '/test.xlsx', __DIR__ . '/copy.xlsx'); |
13 | 14 |
|
14 | | -$xlsxFastEditor = new XlsxFastEditor(__DIR__ . '/copy.xlsx'); |
15 | | - |
16 | | -$sheet1 = $xlsxFastEditor->getWorksheetNumber('Sheet1'); |
17 | | -assert($sheet1 === 1); |
18 | | - |
19 | | -assert($xlsxFastEditor->readFloat($sheet1, 'D2') === 3.14159); |
20 | | -assert($xlsxFastEditor->readFloat($sheet1, 'D4') === -1.0); |
21 | | -assert($xlsxFastEditor->readFloat($sheet1, 'e5') === null); |
22 | | -assert($xlsxFastEditor->readInt($sheet1, 'c3') === -5); |
23 | | -assert($xlsxFastEditor->readInt($sheet1, 'F6') === null); |
24 | | -assert($xlsxFastEditor->readString($sheet1, 'b4') === 'naïveté'); |
25 | | -assert($xlsxFastEditor->readString($sheet1, 'F7') === null); |
26 | | - |
27 | | -$sheet2 = $xlsxFastEditor->getWorksheetNumber('Sheet2'); |
28 | | -assert($sheet2 === 2); |
29 | | - |
30 | | -assert($xlsxFastEditor->readFormula($sheet2, 'c2') === '=Sheet1!C2*2'); |
31 | | -assert($xlsxFastEditor->readFloat($sheet2, 'D2') === 3.14159 * 2); |
32 | | -assert($xlsxFastEditor->readFloat($sheet2, 'D4') === -1.0 * 2); |
33 | | -assert($xlsxFastEditor->readInt($sheet2, 'c3') === -5 * 2); |
34 | | -assert($xlsxFastEditor->readString($sheet2, 'B3') === 'déjà-vu'); |
35 | | - |
36 | | -// Existing cells |
37 | | -$xlsxFastEditor->writeFormula($sheet1, 'c2', '=2*3'); |
38 | | -$xlsxFastEditor->writeString($sheet1, 'b4', 'α'); |
39 | | -$xlsxFastEditor->writeInt($sheet1, 'c4', 15); |
40 | | -$xlsxFastEditor->writeFloat($sheet1, 'd4', -66.6); |
41 | | - |
42 | | -// Existing cells with formulas |
43 | | -$xlsxFastEditor->writeFormula($sheet2, 'c2', '=Sheet1!C2*3'); |
44 | | -$xlsxFastEditor->writeString($sheet2, 'B3', 'β'); |
45 | | -$xlsxFastEditor->writeInt($sheet2, 'C3', -7); |
46 | | -$xlsxFastEditor->writeFloat($sheet2, 'D3', 273.15); |
47 | | - |
48 | | -// Non-existing cells but existing lines |
49 | | -$xlsxFastEditor->writeFormula($sheet2, 'I2', '=7*3'); |
50 | | -$xlsxFastEditor->writeString($sheet2, 'F2', 'γ'); |
51 | | -$xlsxFastEditor->writeInt($sheet2, 'G3', -7); |
52 | | -$xlsxFastEditor->writeFloat($sheet2, 'H4', 273.15); |
53 | | - |
54 | | -// Non-existing lines |
55 | | -$xlsxFastEditor->writeFormula($sheet2, 'E11', '=7*5'); |
56 | | -$xlsxFastEditor->writeString($sheet2, 'B10', 'δ'); |
57 | | -$xlsxFastEditor->writeInt($sheet2, 'C9', 13); |
58 | | -$xlsxFastEditor->writeFloat($sheet2, 'D10', -273.15); |
59 | | - |
60 | | -assert($xlsxFastEditor->textReplace('/Hello/i', 'World') > 0); |
61 | | - |
62 | | -$xlsxFastEditor->save(); |
63 | | - |
64 | | -$xlsxFastEditor = new XlsxFastEditor(__DIR__ . '/copy.xlsx'); |
65 | | - |
66 | | -assert($xlsxFastEditor->readFormula($sheet1, 'c2') === '=2*3'); |
67 | | -assert($xlsxFastEditor->readString($sheet1, 'B4') === 'α'); |
68 | | -assert($xlsxFastEditor->readInt($sheet1, 'C4') === 15); |
69 | | -assert($xlsxFastEditor->readFloat($sheet1, 'D4') === -66.6); |
70 | | - |
71 | | -assert($xlsxFastEditor->readFormula($sheet2, 'c2') === '=Sheet1!C2*3'); |
72 | | -assert($xlsxFastEditor->readString($sheet2, 'B3') === 'β'); |
73 | | -assert($xlsxFastEditor->readInt($sheet2, 'C3') === -7); |
74 | | -assert($xlsxFastEditor->readFloat($sheet2, 'D3') === 273.15); |
75 | | - |
76 | | -assert($xlsxFastEditor->readFormula($sheet2, 'I2') === '=7*3'); |
77 | | -assert($xlsxFastEditor->readString($sheet2, 'F2') === 'γ'); |
78 | | -assert($xlsxFastEditor->readInt($sheet2, 'G3') === -7); |
79 | | -assert($xlsxFastEditor->readFloat($sheet2, 'H4') === 273.15); |
80 | | - |
81 | | -assert($xlsxFastEditor->readFormula($sheet2, 'E11') === '=7*5'); |
82 | | -assert($xlsxFastEditor->readString($sheet2, 'B10') === 'δ'); |
83 | | -assert($xlsxFastEditor->readInt($sheet2, 'C9') === 13); |
84 | | -assert($xlsxFastEditor->readFloat($sheet2, 'D10') === -273.15); |
85 | | - |
86 | | -assert($xlsxFastEditor->readString($sheet1, 'B2') === 'World'); |
87 | | - |
88 | | -$xlsxFastEditor->close(); |
89 | | - |
90 | | -// Verify by hand that the resulting file opens without warning in Microsoft Excel |
91 | | -// unlink(__DIR__ . '/copy.xlsx'); |
| 15 | +try { |
| 16 | + $xlsxFastEditor = new XlsxFastEditor(__DIR__ . '/copy.xlsx'); |
| 17 | + |
| 18 | + $sheet1 = $xlsxFastEditor->getWorksheetNumber('Sheet1'); |
| 19 | + assert($sheet1 === 1); |
| 20 | + |
| 21 | + assert($xlsxFastEditor->readFloat($sheet1, 'D2') === 3.14159); |
| 22 | + assert($xlsxFastEditor->readFloat($sheet1, 'D4') === -1.0); |
| 23 | + assert($xlsxFastEditor->readFloat($sheet1, 'e5') === null); |
| 24 | + assert($xlsxFastEditor->readInt($sheet1, 'c3') === -5); |
| 25 | + assert($xlsxFastEditor->readInt($sheet1, 'F6') === null); |
| 26 | + assert($xlsxFastEditor->readString($sheet1, 'b4') === 'naïveté'); |
| 27 | + assert($xlsxFastEditor->readString($sheet1, 'F7') === null); |
| 28 | + |
| 29 | + $sheet2 = $xlsxFastEditor->getWorksheetNumber('Sheet2'); |
| 30 | + assert($sheet2 === 2); |
| 31 | + |
| 32 | + assert($xlsxFastEditor->readFormula($sheet2, 'c2') === '=Sheet1!C2*2'); |
| 33 | + assert($xlsxFastEditor->readFloat($sheet2, 'D2') === 3.14159 * 2); |
| 34 | + assert($xlsxFastEditor->readFloat($sheet2, 'D4') === -1.0 * 2); |
| 35 | + assert($xlsxFastEditor->readInt($sheet2, 'c3') === -5 * 2); |
| 36 | + assert($xlsxFastEditor->readString($sheet2, 'B3') === 'déjà-vu'); |
| 37 | + |
| 38 | + // Existing cells |
| 39 | + $xlsxFastEditor->writeFormula($sheet1, 'c2', '=2*3'); |
| 40 | + $xlsxFastEditor->writeString($sheet1, 'b4', 'α'); |
| 41 | + $xlsxFastEditor->writeInt($sheet1, 'c4', 15); |
| 42 | + $xlsxFastEditor->writeFloat($sheet1, 'd4', -66.6); |
| 43 | + |
| 44 | + // Existing cells with formulas |
| 45 | + $xlsxFastEditor->writeFormula($sheet2, 'c2', '=Sheet1!C2*3'); |
| 46 | + $xlsxFastEditor->writeString($sheet2, 'B3', 'β'); |
| 47 | + $xlsxFastEditor->writeInt($sheet2, 'C3', -7); |
| 48 | + $xlsxFastEditor->writeFloat($sheet2, 'D3', 273.15); |
| 49 | + |
| 50 | + // Non-existing cells but existing lines |
| 51 | + $xlsxFastEditor->writeFormula($sheet2, 'I2', '=7*3'); |
| 52 | + $xlsxFastEditor->writeString($sheet2, 'F2', 'γ'); |
| 53 | + $xlsxFastEditor->writeInt($sheet2, 'G3', -7); |
| 54 | + $xlsxFastEditor->writeFloat($sheet2, 'H4', 273.15); |
| 55 | + |
| 56 | + // Non-existing lines |
| 57 | + $xlsxFastEditor->writeFormula($sheet2, 'E11', '=7*5'); |
| 58 | + $xlsxFastEditor->writeString($sheet2, 'B10', 'δ'); |
| 59 | + $xlsxFastEditor->writeInt($sheet2, 'C9', 13); |
| 60 | + $xlsxFastEditor->writeFloat($sheet2, 'D10', -273.15); |
| 61 | + |
| 62 | + assert($xlsxFastEditor->textReplace('/Hello/i', 'World') > 0); |
| 63 | + |
| 64 | + $xlsxFastEditor->save(); |
| 65 | + |
| 66 | + $xlsxFastEditor = new XlsxFastEditor(__DIR__ . '/copy.xlsx'); |
| 67 | + |
| 68 | + assert($xlsxFastEditor->readFormula($sheet1, 'c2') === '=2*3'); |
| 69 | + assert($xlsxFastEditor->readString($sheet1, 'B4') === 'α'); |
| 70 | + assert($xlsxFastEditor->readInt($sheet1, 'C4') === 15); |
| 71 | + assert($xlsxFastEditor->readFloat($sheet1, 'D4') === -66.6); |
| 72 | + |
| 73 | + assert($xlsxFastEditor->readFormula($sheet2, 'c2') === '=Sheet1!C2*3'); |
| 74 | + assert($xlsxFastEditor->readString($sheet2, 'B3') === 'β'); |
| 75 | + assert($xlsxFastEditor->readInt($sheet2, 'C3') === -7); |
| 76 | + assert($xlsxFastEditor->readFloat($sheet2, 'D3') === 273.15); |
| 77 | + |
| 78 | + assert($xlsxFastEditor->readFormula($sheet2, 'I2') === '=7*3'); |
| 79 | + assert($xlsxFastEditor->readString($sheet2, 'F2') === 'γ'); |
| 80 | + assert($xlsxFastEditor->readInt($sheet2, 'G3') === -7); |
| 81 | + assert($xlsxFastEditor->readFloat($sheet2, 'H4') === 273.15); |
| 82 | + |
| 83 | + assert($xlsxFastEditor->readFormula($sheet2, 'E11') === '=7*5'); |
| 84 | + assert($xlsxFastEditor->readString($sheet2, 'B10') === 'δ'); |
| 85 | + assert($xlsxFastEditor->readInt($sheet2, 'C9') === 13); |
| 86 | + assert($xlsxFastEditor->readFloat($sheet2, 'D10') === -273.15); |
| 87 | + |
| 88 | + assert($xlsxFastEditor->readString($sheet1, 'B2') === 'World'); |
| 89 | + |
| 90 | + $xlsxFastEditor->close(); |
| 91 | + |
| 92 | + // Verify by hand that the resulting file opens without warning in Microsoft Excel |
| 93 | + // unlink(__DIR__ . '/copy.xlsx'); |
| 94 | +} catch (XlsxFastEditorException $xlsxe) { |
| 95 | + die($xlsxe); |
| 96 | +} |
0 commit comments