Exceljs: How to insert image FIT inside a cell ?

Created on 19 Nov 2019  Â·  4Comments  Â·  Source: exceljs/exceljs

Thanks for the great package. I want to insert image inside a cell. Currently, I'm using fixed width and height, but have no idea how to make image size fit inside a cell. How can I do that with exceljs ? Thanks in advance !
I want to

        const tmpWorkBook = new ExcelJS.Workbook();
        tmpWorkBook.xlsx.readFile(filePath)
            .then(function() {
              const worksheet = tmpWorkBook.getWorksheet("Sheet1");
              var imageID = tmpWorkBook.addImage({
                filename: imagePath,
                extension: 'png',
              });
              console.log("imageID", imageID);
              const relCol = cell.columnNumber() - 1;
              const relNum = cell.rowNumber() - 1;

              worksheet.addImage(imageID, {
                tl: { col: relCol, row: relNum },
                ext: { width: 100, height: 50 } // currently I'm using fixed width and height.
              });
              tmpWorkBook.xlsx.writeFile(outputPath);
            });

All 4 comments

I noticed that I can insert image fit inside a range but wonder how I can do that with cell?

worksheet.addImage(imageId2, 'B2:D6');

See the example of add image to a cell.

worksheet.addImage(imageId2, {
  tl: { col: 0, row: 0 },
  ext: { width: 500, height: 200 }
});

Thanks for opening this request.
Please re-open if you still have issues.
Best of luck.

See the example of add image to a cell.

worksheet.addImage(imageId2, {
  tl: { col: 0, row: 0 },
  ext: { width: 500, height: 200 }
});

It doesn't fit cell

Was this page helpful?
0 / 5 - 0 ratings

Related issues

faradaytrs picture faradaytrs  Â·  3Comments

dbsxdbsx picture dbsxdbsx  Â·  3Comments

CountryGuide picture CountryGuide  Â·  3Comments

Siemienik picture Siemienik  Â·  4Comments

ljkmchale picture ljkmchale  Â·  4Comments