File tree Expand file tree Collapse file tree 3 files changed +28
-6
lines changed Expand file tree Collapse file tree 3 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -1028,13 +1028,20 @@ public function _makeNewSharedString(string $value): int
1028
1028
}
1029
1029
1030
1030
try {
1031
- $ t = $ dom ->createElement ('t ' , $ value );
1031
+ // First, we create an empty element t
1032
+ $ t = $ dom ->createElement ('t ' );
1033
+ if ($ t === false ) {
1034
+ throw new XlsxFastEditorXmlException ('Failed to create <t> element ' );
1035
+ }
1036
+ // Add content as a text node
1037
+ $ textNode = $ dom ->createTextNode ($ value );
1038
+ if ($ textNode === false ) {
1039
+ throw new XlsxFastEditorXmlException ('Failed to create <t> text node ' );
1040
+ }
1041
+ $ t ->appendChild ($ textNode );
1032
1042
} catch (\DOMException $ dex ) {
1033
1043
throw new XlsxFastEditorXmlException ('Error creating <t> in shared strings! ' , $ dex ->code , $ dex );
1034
1044
}
1035
- if ($ t === false ) {
1036
- throw new XlsxFastEditorXmlException ('Error creating <t> in shared strings! ' );
1037
- }
1038
1045
$ si ->appendChild ($ t );
1039
1046
if (!($ dom ->firstElementChild instanceof \DOMElement)) {
1040
1047
throw new XlsxFastEditorXmlException ('Invalid shared strings! ' );
Original file line number Diff line number Diff line change @@ -307,10 +307,17 @@ public function writeFormula(string $value): void
307
307
throw new XlsxFastEditorXmlException ("Internal error accessing cell {$ this ->name ()}! " );
308
308
}
309
309
try {
310
- $ f = $ dom ->createElement ('f ' , $ value );
311
- if (!($ f instanceof \DOMElement)) {
310
+ // First, we create an empty element t
311
+ $ f = $ dom ->createElement ('f ' );
312
+ if ($ f === false ) {
312
313
throw new XlsxFastEditorXmlException ("Error creating DOMElement of formula for cell {$ this ->name ()}! " );
313
314
}
315
+ // Add content as a text node
316
+ $ textNode = $ dom ->createTextNode ($ value );
317
+ if ($ textNode === false ) {
318
+ throw new XlsxFastEditorXmlException ("Error creating text node of formula for cell {$ this ->name ()}! " );
319
+ }
320
+ $ f ->appendChild ($ textNode );
314
321
} catch (\DOMException $ dex ) {
315
322
throw new XlsxFastEditorXmlException ("Error creating formula for cell {$ this ->name ()}! " , $ dex ->code , $ dex );
316
323
}
Original file line number Diff line number Diff line change 119
119
$ xlsxFastEditor ->writeInt ($ sheet2 , 'C3 ' , -7 );
120
120
$ xlsxFastEditor ->writeFloat ($ sheet2 , 'D3 ' , 273.15 );
121
121
122
+ // Writing special XML characters
123
+ $ xlsxFastEditor ->writeString ($ sheet2 , 'B5 ' , '< " & \' > ' );
124
+ $ xlsxFastEditor ->writeFormula ($ sheet2 , 'C5 ' , '=LEN("< & \' >") ' );
125
+
122
126
// Writing non-existing cells but existing lines
123
127
$ xlsxFastEditor ->writeFormula ($ sheet2 , 'I2 ' , '=7*3 ' );
124
128
$ xlsxFastEditor ->writeString ($ sheet2 , 'F2 ' , 'γ ' );
166
170
167
171
assert ($ xlsxFastEditor ->readString ($ sheet1 , 'B2 ' ) === 'World ' );
168
172
173
+ // Test special XML characters
174
+ assert ($ xlsxFastEditor ->readString ($ sheet2 , 'B5 ' ) === '< " & \' > ' );
175
+ assert ($ xlsxFastEditor ->readFormula ($ sheet2 , 'C5 ' ) === '=LEN("< & \' >") ' );
176
+
169
177
$ xlsxFastEditor ->close ();
170
178
171
179
// Verify by hand that the resulting file opens without warning in Microsoft Excel.
You can’t perform that action at this time.
0 commit comments