Hello,
I’m a bit struggling with the rendering of the cells in my HOT instance.
Purpose is to apply a specific style (backgroundColor, color, fontWeight, fontStyle) to every cells in column A based on their value. If the value has no dedicated style, just leave it as is. There’s approximately 100 different styles.
My current solution is to define a big array of Objects (each Object represent a style) in a custom renderer JavaScript file, and to handle the logic in a function like in the Conditional formatting demo.
Here’s a snippet of my code :
const STYLES = [
{
CellValue: 'Input',
FillType: 'ROW',
CellStyle: {
backgroundColor: 'rgb(255, 153, 0)',
color: 'black',
fontWeight: 'bold',
fontStyle: 'normal'
}
},
{
CellValue: 'EndInput',
FillType: 'ROW',
CellStyle: {
backgroundColor: 'rgb(255, 153, 0)',
color: 'black',
fontWeight: 'bold',
fontStyle: 'normal'
}
},
{
CellValue: 'Version Number',
FillType: 'CELL',
CellStyle: {
backgroundColor: 'rgb(192, 192, 192)',
color: 'black',
fontWeight: 'bold',
fontStyle: 'normal'
}
},
{
CellValue: 'InsertMessage',
FillType: 'CELL',
CellStyle: {
backgroundColor: 'rgb(196, 215, 155)',
color: 'black',
fontWeight: 'bold',
fontStyle: 'normal'
}
},
…
export default function MyCustomRenderer(instance, td, row, col, prop, value, cellProperties) {
// Do not forget to set the value of the cell
td.innerHTML = value;
// General style
if (value && value.length > 0) {
td.style.borderBottom = 'thin solid #000000';
td.style.borderRight = 'thin solid #000000';
}
td.style.color = 'black';
td.style.fontFamily = 'Calibri, sans-serif';
td.style.fontSize = '0.8rem';
if (col === 0) {
console.log(`Cell [${row},${col}] is going to be rendered.`);
// Specific rendering for cells in column A
const columnAStyle = STYLES.find(o => o.CellValue === value);
if (typeof columnAStyle !== 'undefined') {
console.log(`Value [${value}] has a specific style.`);
// Cell value corresponds to the start/end of a function/section
td.style.backgroundColor = columnAStyle.CellStyle.backgroundColor;
td.style.color = columnAStyle.CellStyle.color;
td.style.fontWeight = columnAStyle.CellStyle.fontWeight;
td.style.fontStyle = columnAStyle.CellStyle.fontStyle;
}
}
return td;
}
Is it the right way to do it ? Or is it better to move all these styles in a css file and call the different classnames ?
Thanks a lot for your help,
Caroline