Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/XlsxFastEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,13 +1028,20 @@ public function _makeNewSharedString(string $value): int
}

try {
$t = $dom->createElement('t', $value);
// First, we create an empty element t
$t = $dom->createElement('t');
if ($t === false) {
throw new XlsxFastEditorXmlException('Failed to create <t> element');
}
// Add content as a text node
$textNode = $dom->createTextNode($value);
if ($textNode === false) {
throw new XlsxFastEditorXmlException('Failed to create <t> text node');
}
$t->appendChild($textNode);
} catch (\DOMException $dex) {
throw new XlsxFastEditorXmlException('Error creating <t> in shared strings!', $dex->code, $dex);
}
if ($t === false) {
throw new XlsxFastEditorXmlException('Error creating <t> in shared strings!');
}
$si->appendChild($t);
if (!($dom->firstElementChild instanceof \DOMElement)) {
throw new XlsxFastEditorXmlException('Invalid shared strings!');
Expand Down
11 changes: 9 additions & 2 deletions src/XlsxFastEditorCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,17 @@ public function writeFormula(string $value): void
throw new XlsxFastEditorXmlException("Internal error accessing cell {$this->name()}!");
}
try {
$f = $dom->createElement('f', $value);
if (!($f instanceof \DOMElement)) {
// First, we create an empty element t
$f = $dom->createElement('f');
if ($f === false) {
throw new XlsxFastEditorXmlException("Error creating DOMElement of formula for cell {$this->name()}!");
}
// Add content as a text node
$textNode = $dom->createTextNode($value);
if ($textNode === false) {
throw new XlsxFastEditorXmlException("Error creating text node of formula for cell {$this->name()}!");
}
$f->appendChild($textNode);
} catch (\DOMException $dex) {
throw new XlsxFastEditorXmlException("Error creating formula for cell {$this->name()}!", $dex->code, $dex);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
$xlsxFastEditor->writeInt($sheet2, 'C3', -7);
$xlsxFastEditor->writeFloat($sheet2, 'D3', 273.15);

// Writing special XML characters
$xlsxFastEditor->writeString($sheet2, 'B5', '< " & \' >');
$xlsxFastEditor->writeFormula($sheet2, 'C5', '=LEN("< & \' >")');

// Writing non-existing cells but existing lines
$xlsxFastEditor->writeFormula($sheet2, 'I2', '=7*3');
$xlsxFastEditor->writeString($sheet2, 'F2', 'γ');
Expand Down Expand Up @@ -166,6 +170,10 @@

assert($xlsxFastEditor->readString($sheet1, 'B2') === 'World');

// Test special XML characters
assert($xlsxFastEditor->readString($sheet2, 'B5') === '< " & \' >');
assert($xlsxFastEditor->readFormula($sheet2, 'C5') === '=LEN("< & \' >")');

$xlsxFastEditor->close();

// Verify by hand that the resulting file opens without warning in Microsoft Excel.
Expand Down