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
10281028 }
10291029
10301030 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 );
10321042 } catch (\DOMException $ dex ) {
10331043 throw new XlsxFastEditorXmlException ('Error creating <t> in shared strings! ' , $ dex ->code , $ dex );
10341044 }
1035- if ($ t === false ) {
1036- throw new XlsxFastEditorXmlException ('Error creating <t> in shared strings! ' );
1037- }
10381045 $ si ->appendChild ($ t );
10391046 if (!($ dom ->firstElementChild instanceof \DOMElement)) {
10401047 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
307307 throw new XlsxFastEditorXmlException ("Internal error accessing cell {$ this ->name ()}! " );
308308 }
309309 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 ) {
312313 throw new XlsxFastEditorXmlException ("Error creating DOMElement of formula for cell {$ this ->name ()}! " );
313314 }
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 );
314321 } catch (\DOMException $ dex ) {
315322 throw new XlsxFastEditorXmlException ("Error creating formula for cell {$ this ->name ()}! " , $ dex ->code , $ dex );
316323 }
Original file line number Diff line number Diff line change 119119 $ xlsxFastEditor ->writeInt ($ sheet2 , 'C3 ' , -7 );
120120 $ xlsxFastEditor ->writeFloat ($ sheet2 , 'D3 ' , 273.15 );
121121
122+ // Writing special XML characters
123+ $ xlsxFastEditor ->writeString ($ sheet2 , 'B5 ' , '< " & \' > ' );
124+ $ xlsxFastEditor ->writeFormula ($ sheet2 , 'C5 ' , '=LEN("< & \' >") ' );
125+
122126 // Writing non-existing cells but existing lines
123127 $ xlsxFastEditor ->writeFormula ($ sheet2 , 'I2 ' , '=7*3 ' );
124128 $ xlsxFastEditor ->writeString ($ sheet2 , 'F2 ' , 'γ ' );
166170
167171 assert ($ xlsxFastEditor ->readString ($ sheet1 , 'B2 ' ) === 'World ' );
168172
173+ // Test special XML characters
174+ assert ($ xlsxFastEditor ->readString ($ sheet2 , 'B5 ' ) === '< " & \' > ' );
175+ assert ($ xlsxFastEditor ->readFormula ($ sheet2 , 'C5 ' ) === '=LEN("< & \' >") ' );
176+
169177 $ xlsxFastEditor ->close ();
170178
171179 // 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