fetch style inside a cell

Topics: User Forum
Sep 22, 2009 at 2:35 PM

Hello,

First thanks a lot for this library, it is very useful !

I have the following question: say I have a cell which content is text.

Say that this text is black for the major part, and a word in it is colored in red.

Do I Have a way to get to that piece of information somehow ?

From what I could understand so far, I can easily acces the cell style, but I did not get how to get to the cell content syle.

Thank you for your help.

Best,

Thomas

Developer
Sep 22, 2009 at 2:57 PM

>> Say that this text is black for the major part, and a word in it is colored in red.

>> Do I Have a way to get to that piece of information somehow ?

Excel allows ordinary strings as well as Rich-Text strings. It sounds like you have a Rich-Text string. Here is some code taken from the HTML writer which shows how it outputs Rich-Text string as HTML:

 

// Value
if ($cell->getValue() instanceof PHPExcel_RichText) {
    // Loop through rich text elements
    $elements = $cell->getValue()->getRichTextElements();
    foreach ($elements as $element) {
        // Rich text start?
        if ($element instanceof PHPExcel_RichText_Run) {
            $cellData .= '<span style="' . $this->_assembleCSS($this->_createCSSStyleFont($element->getFont())) . '">';

            if ($element->getFont()->getSuperScript()) {
                $cellData .= '<sup>';
            } else if ($element->getFont()->getSubScript()) {
                $cellData .= '<sub>';
            }
        }

        // Convert UTF8 data to PCDATA
        $cellText = $element->getText();
        $cellData .= htmlspecialchars($cellText);

        if ($element instanceof PHPExcel_RichText_Run) {
            if ($element->getFont()->getSuperScript()) {
                $cellData .= '</sup>';
            } else if ($element->getFont()->getSubScript()) {
                $cellData .= '</sub>';
            }

            $cellData .= '</span>';
        }
    }
}

 

You can extract the various font properties such as color, bold, italic, etc. from the 

$element->getFont()

which is an PHPExcel_Style_Font object. Try to see if the above contains the answer to your question. Otherwise post back.

Sep 22, 2009 at 3:24 PM

Hi,

Thank you for your quick answer.

In fact, I do not "see" where the link between a cell (of type PHPExcel_Cell) is linked to rich text (of type PHPExcel_RichText).

Below is my test code:

/** PHPExcel */
require_once '../Classes/PHPExcel.php';

/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';

//open xls
$objReader = PHPExcel_IOFactory::createReaderForFile('s38.xls');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('s38.xls');

$objPHPExcel->setActiveSheetIndex(0);
$sheet = $objPHPExcel->getActiveSheet();
for($i=0;$i<30;$i++){
	for($j=0;$j<30;$j++){
		$cell = $sheet->getCellByColumnAndRow($i,$j);
		if ($cell->getValue() instanceof PHPExcel_RichText) {
			$elements = $cell->getValue()->getRichTextElements();		
		}
	}
}

The problem is that I never get in :

if ($cell->getValue() instanceof PHPExcel_RichText)

Thanks again.

BR,

Thomas

Developer
Sep 22, 2009 at 3:30 PM

You will need to remove this:

$objReader->setReadDataOnly(true);

It strips all styles, including the font styles in Rich-Text strings.

Sep 23, 2009 at 8:21 AM

It works perfectly.

Thanks a lot for your support.

BR,

Thomas