Ashutosh Jha
Ashutosh Jha

Reputation: 16337

How to fill background color of row in node js xlsx npm

I need to read a excel sheet and after checking data from backend need to color row . I am able to read and create new sheet with new data but fill is not working here for a cell/row .

This is my code , I am using xlsx npm

  let cell = workingSheet['C'+i];
  console.log(cell)
  workingSheet['A'+i].v = "PROCESSED"+i;
  workingSheet['A'+i].s = { fill: { fgColor: { rgb: "#111111" } } };
  console.log(
    workingSheet['A'+i]
  )
  xlsx.writeFile(spreadsheet, `./sheets/output/mysheet.xls`);

Upvotes: 2

Views: 31491

Answers (2)

This example shows how to color the cell background. The problems was that, there were no pattern type: "solid", and the color code does not needs the beginning #. This does not works with xlsx package, but xlsx-color.

"use strict";
var xlsx = require("xlsx-color");
var spreadsheet = xlsx.utils.book_new();
var xf = "./sheets/output/mysheet.xls";

var sheets = {
  "Munkafüzet1": [
    ["M1A1", "M1B1", "M1C1", "M1D1"],
    ["M1A2", "M1B2", "M1C2", "M1D2"],
    ["M1A2", "M1B2", "M1C2", "M1D2"],
    ["M1A2", "M1B2", "M1C2", "M1D2"],
    ["M1A2", "M1B2", "M1C2", "M1D2"],
  ]
};

for (var sheet of Object.keys(sheets)) {
  xlsx.utils.book_append_sheet(
    spreadsheet,
    xlsx.utils.aoa_to_sheet(sheets[sheet]),
    sheet
  );
}

var i;
for (i = 1; i <= sheets["Munkafüzet1"].length; i++) {
spreadsheet.Sheets["Munkafüzet1"]["A"+i].s = {
  fill: {
    patternType: "solid",
    fgColor: { rgb: "111111" }
  }
  };
}

xlsx.writeFile(spreadsheet, xf);

Upvotes: 4

robert
robert

Reputation: 6142

According to this thread.

We offer this in the Pro compendium. Since companies have paid for the features already, it would be unfair to them if we turned around and made it available as open source. We have a longer comment in a gist.

Upvotes: 2

Related Questions