Home » excel » javascript – Example of fill bgColor cell in excel4node

javascript – Example of fill bgColor cell in excel4node

Posted by: admin March 9, 2020 Leave a comment

Questions:

I am using excel4node to create Excel file in nodeJS.
The site gives following guideline to use fill property.

fill: { // §18.8.20 fill (Fill)
      type: string, // Currently only 'pattern' is implimented. Non-   implimented option is 'gradient'
      patternType: string, //§18.18.55 ST_PatternType (Pattern Type)
      bgColor: string // HTML style hex value. optional. defaults to black
      fgColor: string // HTML style hex value. required.
  },

I could not work around this. Anyone can help me, please?

How to&Answers:

Basically what you can do with the package in its current state is the following:

fill: {
    type: 'pattern', // the only one implemented so far.
    patternType: 'solid', // most common.
    fgColor: '2172d7', // you can add two extra characters to serve as alpha, i.e. '2172d7aa'.
    // bgColor: 'ffffff' // bgColor only applies on patternTypes other than solid.
}

Note that the background color of your cell will be the foreground color (fgColor) of the fill property. It sounds confusing, but it makes sense when you understand that the fill property have patterns that use more than one color, hence the property having foreground and background colors.

The ‘solid’ patternType is probably what you’re looking for, but there are others, such as ‘darkDown’, ‘darkHorizontal’, ‘lightGrid’, and etc, as can be seen here:
excel4node/fillPattern.js

Answer:

Define function inside your excel generate function like this :

var wb = new xl.Workbook();
var ws = wb.addWorksheet(‘Daily Activity Reports’);

function colorCell(color, pattern) {

        return wb.createStyle({
            fill: {
                type: 'pattern',
                fgColor: color,
                patternType: pattern || 'solid',
            }
        });
    }

And then apply it like

ws.cell(1,1).string("Cell Content").style(colorCell('#CDDC39'))

Optionally you can send pattern-type in function parameters.