New line in rich text cell prior to variable not working

Topics: Developer Forum
Feb 24, 2015 at 11:52 AM
Hi There,

I'm having issues forcing a new line in rich text as below
$objBold = $objRichText->createTextRun('Component '.$i.".\n");
$objSmall = $objRichText->createTextRun("\r".$spec['containertype'].": ".$component[1]['name']." (".$fragname.")");
both of those lines comprise my rich text object, and the above does not work.

however if i do the following it does function fine
$objBold = $objRichText->createTextRun('Component '.$i);
$objSmall = $objRichText->createTextRun("\nA".$spec['containertype'].": ".$component[1]['name']." (".$fragname.")");
if i put a normal text character after the new line it works fine, but obviously I don't want that character.

any ideas?
Mar 9, 2015 at 10:36 AM
bump
Coordinator
Mar 9, 2015 at 10:48 AM
"\r" in your first example (which doesn't work) isn't a new line character, it's a return character. You correctly use a new line character ("\n") in the second example, which does work.

"\n" is a new line character, and will work whether it has characters after it or not
Mar 9, 2015 at 10:58 AM
MarkBaker wrote:
"\r" in your first example (which doesn't work) isn't a new line character, it's a return character. You correctly use a new line character ("\n") in the second example, which does work.

"\n" is a new line character, and will work whether it has characters after it or not
Thanks for the reply,

Unfortunately I have to disagree
$objRichText = new PHPExcel_RichText();
$objRichText->createText('');
$objBold = $objRichText->createTextRun('Component '.$i);
$objBold->getFont()->setBold(true);
$objBold->getFont()->setSize(13);
$objSmall = $objRichText->createTextRun("\n".$spec['containertype'].": ".$component[1]['name']." (".$fragname.")");
$objSmall->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getCell('H1')->setValue($objRichText);
the above does not work, there is now new line, if after the \n i put a standard character i.e. \nA it does work, however i do not require or want that standard character to be apart of the string.
Coordinator
Mar 9, 2015 at 11:56 PM
Edited Mar 10, 2015 at 12:10 AM
Seems to work
$outputFileType = 'Excel2007';
$outputFileName = './outputTest.xlsx';

$objPHPExcel = new PHPExcel();
$worksheet = $objPHPExcel->getActiveSheet();

// Plaintext example
$worksheet->setCellValue('A1',"Hello\nWorld");
$worksheet->getRowDimension(1)->setRowHeight(-1);
$worksheet->getStyle('A1')->getAlignment()->setWrapText(true);

// Rich text example #1
$objRichText1 = new PHPExcel_RichText();
$objGreen1 = $objRichText1->createTextRun("Hello");
$objGreen1->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKGREEN)
);
$objRed1 = $objRichText1->createTextRun("\nWorld");
$objRed1->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKRED)
);

$worksheet->getCell('A2')->setValue($objRichText1);
$worksheet->getRowDimension(1)->setRowHeight(-1);
$worksheet->getStyle('A2')->getAlignment()->setWrapText(true);

// Rich text example #2
$words = ['Hello', 'World'];
$objRichText2 = new PHPExcel_RichText();
$objGreen2 = $objRichText2->createTextRun("\n" . $words[0]);
$objGreen2->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKGREEN) 
);
$objRed2 = $objRichText2->createTextRun("\n" . $words[1] . "\n");
$objRed2->getFont()->setColor(
    new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_DARKRED) 
);

$worksheet->getCell('A3')->setValue($objRichText2);
$worksheet->getRowDimension(1)->setRowHeight(-1);
$worksheet->getStyle('A3')->getAlignment()->setWrapText(true);


$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType);
$objPHPExcel = $objPHPExcelWriter->save($outputFileName);
I suspect that the problem is that you're not setting the cell to allow text wrapping:
$objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setWrapText(true);
and you may also want to set the column width to autosize as well
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);