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

OOCalc parsing links inside cells error. #139

Open
Strate opened this issue Feb 5, 2013 · 4 comments
Open

OOCalc parsing links inside cells error. #139

Strate opened this issue Feb 5, 2013 · 4 comments

Comments

@Strate
Copy link

Strate commented Feb 5, 2013

I have cell in *.ods file, which actually contains this structure:

->p->span->a, where a contains link or email. My file origin from LibreOffice Calc.

Accordings to ec9256a there was attempt to fix same bug, but there is hardcoded ->p->span(s) structure. How about to create more complex indiferrent to structure recursive algoritm?

@Strate
Copy link
Author

Strate commented Feb 5, 2013

Something like:

$fetchTextFromChildren = function( SimpleXmlElement $el ) use( &$fetchTextFromChildren )
{
    $result = '';
    if ( $el->count() )
    {
        foreach ( $el as $subElement ) {
            $result .= $fetchTextFromChildren( $subElement );
        }
    }
    else {
        $result = (string) $el;
    }
    return $result;
};
$allCellDataText = $fetchTextFromChildren( $cellDataText );

@dbonsch
Copy link
Member

dbonsch commented Feb 10, 2013

Hm.

seems that isset($dataValue->a) is always false cause $allCellDataText "should" always be a string after implode.

I check if we can replace the loop and check by a xpath query.

line: 538 /PHPExcel/Classes/PHPExcel/Reader/OOCalc.php

   $allCellDataText = implode($dataArray, "\n");

   // echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
   switch ($cellDataOfficeAttributes['value-type']) {
     case 'string' :
       $type = PHPExcel_Cell_DataType::TYPE_STRING;
       $dataValue = $allCellDataText;
       if (isset($dataValue->a)) {
       $dataValue = $dataValue->a;
       $cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']);
       $hyperlink = $cellXLinkAttributes['href'];
   }
   break;

@dbonsch dbonsch closed this as completed Feb 10, 2013
@dbonsch dbonsch reopened this Feb 10, 2013
@losinggeneration
Copy link

I'm having pretty much the same issue with email links. libreOffice automatically created links for email addresses:

  <office:body>
    <office:spreadsheet>
      <table:table table:name="Sheet1" table:style-name="ta1">
        <table:table-column table:style-name="co1" table:default-cell-style-name="Default" />
        <table:table-row table:style-name="ro1">
          <table:table-cell office:value-type="string">
            <text:p>
              <text:a xlink:href="mailto:some@email.com" xlink:type="simple">some@email.com</text:a>
            </text:p>
          </table:table-cell>
        </table:table-row>
      </table:table>
      <table:named-expressions />
    </office:spreadsheet>
  </office:body>

You can see that libreOffice puts the text:a within a text:p.

My test case was basically this:

<?php
/** Include PHPExcel */
require_once '../Classes/PHPExcel.php';

if (!file_exists("email.ods")) {
    exit(1);
}
$objPHPExcel = PHPExcel_IOFactory::load("email.ods");

foreach ($objPHPExcel->getAllSheets() as $s) {
    foreach ($s->getRowIterator() as $r) {
        foreach ($r->getCellIterator() as $c) {
            echo "| {$c->getValue()},{$c->getCalculatedValue()},{$c->getFormattedValue()} |";
        }
        echo "\n";
    }
}

And it outputs the following:

| ,, |

Tested with PHPExcel 1.7.9 & PHP 5.4.4-15.1

@acelaya
Copy link

acelaya commented Nov 3, 2014

Hello. I have this same issue.
I found someone who told how to fix this, but it implies to change PHPExcel's source code.
https://phpexcel.codeplex.com/workitem/18957

The link says to place this in Classes\PHPExcel\Reader\OOCalc.php, after line 487.

if (isset($cellDataText->p) && isset($cellDataText->p->a)) {
    $cellDataText->p = $cellDataText->p->a;
}

Since I'm handling dependencies with composer, I can't edit anything inside the vendor directory.
I checked that modification and it works in my case (I'm not really sure what it does), but I don't know if it'll work in every use case.
I could make some tests and open a pull request if you want.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants