guigrpa / docx-templates

Template-based docx report creation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting text color in a table cell

rbeeler-ha opened this issue · comments

I'm trying to dynamically set the text color in a cell based on a value, and am struggling to find the right way to do this via XML. @davidjb was great in helping figure out how to set the background for a cell:

||<w:tcPr><w:shd w:val="clear" w:color="auto" w:fill="00FF00"/></w:tcPr>||

That works as advertised. Any advice on setting the color of the text?

To set the colour of the text, use the XML:

<w:color w:val="FFFFFF"/>

This can be combined with the background colouring to do both for a given cell:

||<w:rPr><w:tcPr><w:shd w:val="clear" w:color="auto" w:fill="00FF00"/></w:tcPr><w:color w:val="FFFFFF"/></w:rPr>||Your cell value here

Edit: fixed up the XML above.

Thanks. I tried this and it's not working for me. I tried it both with and without the background changes and while the background changes worked the text color didn't. This is the function I'm using to return the XML string:

styledText: (value: string, color: string) => {
return ||<w:tcPr><w:color w:val="${color}"/></w:tcPr>||${value};
}

I've tried hard-coding the color as well, to no avail. Any thoughts?

Give this a try:

styledText: (value: string, color: string, bgColor: string) => {
  return `||<w:rPr><w:tcPr><w:shd w:val="clear" w:color="auto" w:fill="${bgColor}"/></w:tcPr><w:color w:val="${color}"/></w:rPr>||${value}`;
}

image image

I missed adding the <w:rPr> (run properties) wrapper in my comment above. Also, note the <w:color> needs to be outside the <w:tcPr> (table cell properties) element.

That did it - thanks! Really appreciate the help!

Thanks for your reply, @davidjb !