I'm using ExcelJS 4.4.0 + Node.js to export a sheet with images in cells. First open looks fine — each row has its image in column D. But once I turn on AutoFilter and hide some rows, things break: images from the hidden rows don't go away. They stick around or land on the wrong row.
What I want is basically "picture stays with its cell" when filtering. I tried editAs: 'oneCell' but it didn't help.
Here's a minimal repro:
const ExcelJS = require('exceljs');
async function main() {
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet('test');
sheet.columns = [
{ header: 'ID', key: 'id', width: 8 },
{ header: 'Name', key: 'name', width: 12 },
{ header: 'Category', key: 'category', width: 12 },
{ header: 'Image', key: 'image', width: 18 },
];
sheet.autoFilter = 'A1:D1';
const png = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
'base64'
);
const imageId = workbook.addImage({ buffer: png, extension: 'png' });
const rows = [
[1, 'Alpha', 'A'],
[2, 'Beta', 'B'],
[3, 'Gamma', 'A'],
[4, 'Delta', 'B'],
[5, 'Epsilon', 'A'],
];
for (const [id, name, category] of rows) {
const row = sheet.addRow([id, name, category, '']);
sheet.getRow(row.number).height = 40;
sheet.addImage(imageId, {
tl: { col: 3, row: row.number - 1 },
ext: { width: 100, height: 50 },
editAs: 'oneCell',
});
}
await workbook.xlsx.writeFile('output.xlsx');
console.log('Done: output.xlsx');
}
main();
How to repro:
npm init -y && npm install exceljs@4.4.0save as
generate.js, runnode generate.jsopen
output.xlsxin WPS 12.1.25897 (macOS)filter Category (col C), uncheck A
rows 1/3/5 hide, but the image from row 5 (Category A) is still there — sitting below the last visible row
So only rows 2 and 4 should have images. In practice, at least one hidden-row image keeps showing. Screenshots below — I used different colored images per row so it's easier to see which one is wrong.
Before filter:
After filter (hide A):
My setup:
ExcelJS 4.4.0
Node 20.x
macOS 15
WPS Office 12.1.25897
Also saw this thread on GitHub — looks related but I couldn't find a fix that works for me.
So my questions are:
Does ExcelJS actually support real "picture in cell" embedding?
If not, is there a
addImagesetup that behaves OK with AutoFilter? (oneCellvstwoCell,tl/brvsext, etc.)
Any pointers would be really appreciated — been stuck on this for a while. Thanks!