formatted cells and richtext

Topics: User Forum
Aug 23, 2013 at 9:09 AM
Hello,

I want to format a cell with backgroundcolor and borders and use richtext to style a partitial word.

At the moment, when I just use the richtext, the text is formated as expected. When I just format the cell, the cell gets formatted as expected. When I'm doing both, the formatting of the cell wins.

Has someone a hint?

I'm using PHPExcel 1.7.9 with PHP 5.3.3 on Debian Linux.

Thanks in advance,
Marc
Coordinator
Aug 26, 2013 at 4:14 PM
There shouldn't be any clash when doing this:
// Create a rich text run
$objRichText = new PHPExcel_RichText();
$objRichText->createText('Hello ');

$objRichTextStyled = $objRichText->createTextRun('World');
$objRichTextStyled->getFont()->setBold(true);
$objRichTextStyled->getFont()->setItalic(true);
$objRichTextStyled->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );

// Write the rich text run to a cell
$objPHPExcel->getActiveSheet()->setCellValue('A1', $objRichText);

// Set the cell background colour
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()
    ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
    ->getStartColor()->setARGB( PHPExcel_Style_Color::COLOR_RED );
Aug 27, 2013 at 8:31 AM
Yes you're right, your example is working. The problem was in my (more complex) code! Thanks!

But one more question:

when I'm adding the line

$objRichText->createText(' another Text. ');

after creating the textRun, 'another Text' also appears in bold, italic and green. Why the text isn't styled in the cells default style?

What made me wondering too was the fact, that it isn't possible to first create a textRun and afterwards create a normal text. I'm writing a little parser which parses: '[b]hello[/b] World' into the bold word 'hello' (->createTextRun()) and then the default styled word ' World' (->createText()). This won't work unfortunately.

Thanks in advance,
Marc
Coordinator
Aug 27, 2013 at 8:52 AM
Only the actual text is modified from the examples in 02types.php and 05featuredemo.inc.php
// Create a rich text run
$objRichText = new PHPExcel_RichText();
$objRichText->createText('Goodbye ');

$objRichTextStyled = $objRichText->createTextRun('cruel ');
$objRichTextStyled->getFont()->setBold(true);
$objRichTextStyled->getFont()->setItalic(true);
$objRichTextStyled->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );

$objRichText->createText('world!');

// Write the rich text run to a cell
$objPHPExcel->getActiveSheet()->setCellValue('A1', $objRichText);

// Set the cell background colour
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()
    ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
    ->getStartColor()->setARGB( PHPExcel_Style_Color::COLOR_RED );