Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Creating RichText cell with blank "" creates blank tags in sharedStrings.xml / corrupts the sharedStrings table #974

Open
@stephenfrank

Description

@stephenfrank

Hi,

I've taken a deep dive on this but I don't know what the most appropriate fix would be so... either it can be fixed in the Reader or the Writer...

(Using version 1.8.1)

Error is reproducible by doing something like this

$doc = new PHPExcel();

$doc->getActiveSheet()->getCell('A1')->setValue('A');
$doc->getActiveSheet()->getCell('B1')->setValue('B');
$doc->getActiveSheet()->getCell('D1')->setValue('D');
$doc->getActiveSheet()->getCell('E1')->setValue('E');

$html = new PHPExcel_Helper_HTML();
$obj = $html->toRichTextObject('');
$doc->getActiveSheet()->getCell('C1')->setValue($obj);

$writer = new PHPExcel_Writer_Excel2007($doc);
$writer->save('test.xlsx');

$reader = new PHPExcel_Reader_Excel2007();
$doc2 = $reader->load('test.xlsx');

$writer = new PHPExcel_Writer_Excel2007($doc2);
$writer->save('test.xlsx');

In this case and error is thrown when reading the new file because the index in SharedStrings is not set

PHP Notice:  Undefined offset: 4 in .../Classes/PHPExcel/Reader/Excel2007.php on line 862

This is because sharedStrings.xml looks like this

<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" uniqueCount="4">
<si>
<t>A</t>
</si>
<si>
<t>B</t>
</si>
<si/>
<si>
<t>E</t>
</si>
</sst>

The Reader will skip over the empty tag...

Classes/PHPExcel/Reader/Excel2007.php:473-ish

foreach ($xmlStrings->si as $val) {
    if (isset($val->t)) {
        $sharedStrings[] = PHPExcel_Shared_String::ControlCharacterOOXML2PHP( (string) $val->t );
    } elseif (isset($val->r)) {
        $sharedStrings[] = $this->_parseRichText($val);
    }
}

In the example case, the index error is presented... in my current (worse) case, the cells that use shared strings become completely garbled (every string off-by-1).

Either the empty RichText objects should be written as a properly empty tag or the Reader must account for the possibility of an empty/self-closing tag.

In the meantime I am simply avoiding this by conditionally not creating an empty RichText object

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions