Reading text properties

Topics: Developer Forum, User Forum
Apr 2, 2013 at 10:58 AM
Hi All,

I am new to PHPExcel. i just need to read cell but with text formatting.

Image

As per image "this is bold". so how can i detect that "is bold" text is bold?

Sorry for my English.

Thanks All.
Coordinator
Apr 3, 2013 at 11:44 AM
Using PHPExcel, you can identify if a cell contains rich text because a getValue() call will return a PHPExcel_RichText object rather than a string/integer/float/boolean scalar type.

You can then loop through the collection of PHPExcel_RichText_Run objects for that PHPExcel_RichText object looking at the style associated with it:
$cellValueAsString = '';
$elements = $cell->getValue()->getRichTextElements();
foreach ($elements as $element) {
    // Rich text start?
    if ($element instanceof PHPExcel_RichText_Run) {
        if ($element->getFont()->getSuperScript()) {
            $cellValueAsString .= '<sup>';
        } else if ($element->getFont()->getSubScript()) {
            $cellValueAsString .= '<sub>';
        }
    }
    // Convert UTF8 data to PCDATA
    $cellText = $element->getText();
    $cellValueAsString .= htmlspecialchars($cellText);
    if ($element instanceof PHPExcel_RichText_Run) {
        if ($element->getFont()->getSuperScript()) {
            $cellValueAsString .= '</sup>';
        } else if ($element->getFont()->getSubScript()) {
            $cellValueAsString .= '</sub>';
        }
    }
}
The logic can easily be modified to test for other formatting feaqtures such as bold, italic, underline, etc
Apr 8, 2013 at 8:54 AM
Edited Apr 8, 2013 at 8:56 AM
Thanks a lot. it help me very much.

I m facing another problem.

i tried to read a colum as bellow

here UnitNo is very important for me.
e.g
Image

for($row = $startRowIndex;;$row++)
{
    $unitNo = $objSheet->getCellByColumnAndRow(0, $row)->getValue();
    $unitNo = trim($unitNo);
    if(empty ($unitNo))
    {
        break;
    }
   // ........
   // ........
   // other stuff which depends on unitNo
}

for value 2.1 it gave me value 2.1 thats fine. but for 2.10 also it gave me 2.1 but i need 2.10 as it is. To solve this i changed all such cells' formatting to "Text" in xlsx sheet. but it didnt work for me. How to change or instruct getValue that it return me text as it is in xlsx ??
Coordinator
Apr 8, 2013 at 11:01 AM
That's what the getFormattedValue() method is intended for... getValue() returns the raw value, getCalculatedValue() retunrs the calculated result (if the cell contained a formula), while getFormattedValue() applies any numberFormat mask that was applied to the cell
Apr 8, 2013 at 2:05 PM
Thank you, MarkBaker

PHPExcel rocks..

Once again. Thanks