exceljs / exceljs

Excel Workbook Manager
MIT License
13.87k stars 1.78k forks source link

[BUG] Setting specific cell fill applies to entire column #1705

Open hydenz opened 3 years ago

hydenz commented 3 years ago

🐛 Bug Report

When I try to set a specific cell fill using worksheet.getCell('A2'), the fill is applied to all cells in A column. Lib version: 4.2.1

Steps To Reproduce

const Excel = require('exceljs');
const workbook = new Excel.Workbook();

(async () => {
  await workbook.xlsx.readFile(
    './exampleSpreadsheet.xlsx'
  );
  const ws = workbook.getWorksheet('example sheet');
  ws.getCell('A2').fill = {
    type: 'pattern',
    pattern: 'solid',
    fgColor: { argb: 'FFFFFF00' },
  };
  await workbook.xlsx.writeFile(
    'exampleSpreadsheet.xlsx'
  );
})();

The expected behaviour:

A2 cell color is changed to argb FFFFFF00 (yellow)

Actual behaviour:

All 'A' column cell colors are changed to argb FFFFFF00 (yellow)

warmtone commented 3 years ago

I got the same problem, and set cell.font also aplied to entire column

MarlboroTwo commented 2 years ago

It appears that the style object of a cell can be shared among multiple cells or ranges, so changing the fill of one cell affects all of the cells that reference the same style object. If you want to force color a specific cell, a work around is to just create a new style object and assign it to the cell's style property.

ganybin commented 8 months ago

try this

ws.getCell('A2').style = {
    ...ws.getCell('A2').style,
    fill: {
      type: 'pattern',
      pattern: 'solid',
      fgColor: { argb: 'FFFFFF00' },
     }
}