Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to change the color of a cell in a word table created with template? #984

Open
emateos opened this issue Jan 27, 2017 · 2 comments

Comments

@emateos
Copy link

emateos commented Jan 27, 2017

Good Morning.
I have a php that generates a word document from a template using phpword.

In this template I have created a table, which is filled perfectly with the values ​​I extract from the database.

The problem is that the background color of the cell should vary depending on the value of the cell.

For example if the value> 50 Color red
If the value> 20 and value <50 yellow.

It's the first time I use phpword and I haven´t idea how to do it.

Please, any suggestions?

// Contenido table TT
$templateWord->setValue('NUA#'.$i,$UA);
$templateWord->setValue('NombreUA#'.$i,utf8_encode($row_RsTT['S1']));
$templateWord->setValue('Sabados#'.$i,$PP01."%");
$templateWord->setValue('Festivos#'.$i,$PP02."%");
$templateWord->setValue('Descanso#'.$i,$PP05."%");
$templateWord->setValue('Personal#'.$i,$PP06."%");

I execute a clonerow from there the #

Thank you very much!!


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@huuvan20
Copy link

huuvan20 commented Jun 8, 2017

I also get into the same issue needing to change the color of template text. Is there anyone have done with this? Much appreciate your help!

@FBnil
Copy link

FBnil commented Oct 7, 2017

@emateos
@huuvan20 There was a bug, see #916
But Templates are different

For now, it is not easy. I managed a method, but I doubt it is optimal, first you need my PHPWord branch that contains the Segment() TemplateProcessor commands.

Now this only works on the LibreOffice generated tables, MSOffice has another structure, but maybe it is helpful:

# First load the template file
$file_dir = storage_path('table.docx');
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($file_dir);

# Now use the getSegment() command to get a table cell
$xml = $templateProcessor->getSegment('${NUA#1}','w:tc');

# Option 1: XML: Make a proper xml
$xml = '<?xml version="1.0" encoding="utf-8"?><cell xmlns:w="w">'.$xml.'</cell>';

# test the xml to be ok (or put everything in a try-catch block)
$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($xml);
$errors = libxml_get_errors();
if (!$errors){
$SX = new \SimpleXMLElement($xml); # really should use xpath...
# make it purple
$SX->children("w")->children("w")->{'tcPr'}->children("w")->{'shd'}[0]->attributes('w')->{'fill'} = "FF00FF";
}

# Ok, now back to text (we want only what is in <cell> not the full <xml>)
$newxml = preg_replace('/^[\s\S]*<cell[^>]*>(.*)<\/cell>[\s\r\n]*$/', '$1', $SX->asXML());

# Option 2: the 1 line quick and dirty (just replace <w:shd> attributes)
$newxml = preg_replace('/<w:shd [^>]+>/', '<w:shd w:val="clear" w:fill="FFF000"/>', $xml);

# and put the new text back
$templateProcessor->replaceSegment('${NUA#1}','w:tc', $newxml);

# save and display
$templateProcessor->saveAs(storage_path('app/public/x.docx'));
$templateProcessor = null;
return response()->download(storage_path('app/public/x.docx'))->deleteFileAfterSend(true);

getSegment() is new and has not made it yet into PHPWord, check my fork https://github.com/FBnil/PHPWord or copy all segment commands from pastebin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants