Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert image FIT inside a cell ? #1029

Closed
TranBaVinhSon opened this issue Nov 19, 2019 · 7 comments
Closed

How to insert image FIT inside a cell ? #1029

TranBaVinhSon opened this issue Nov 19, 2019 · 7 comments

Comments

@TranBaVinhSon
Copy link

TranBaVinhSon commented Nov 19, 2019

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);
            });
@TranBaVinhSon
Copy link
Author

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');

@TranBaVinhSon TranBaVinhSon reopened this Nov 26, 2019
@Alanscut
Copy link
Member

Alanscut commented Jan 7, 2020

See the example of add image to a cell.

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

@Alanscut
Copy link
Member

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

@ibalaji777
Copy link

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

@Knight-H
Copy link

Not sure if you still need this, but what I did was using the same cellId fits inside the cell. I think this works because the same cell is used for the top left (tl) and the bottom right (br).

sheet.addImage(imageId, `${cellId}:${cellId}`)

Example:

sheet.addImage(imageId, `J13:J13`)

@elyohan14
Copy link

Not sure if you still need this, but what I did was using the same cellId fits inside the cell. I think this works because the same cell is used for the top left (tl) and the bottom right (br).

sheet.addImage(imageId, `${cellId}:${cellId}`)

Example:

sheet.addImage(imageId, `J13:J13`)

Great, that is what I need! Thank you

@yogithesymbian
Copy link

yogithesymbian commented Jan 25, 2023

how to fit but have margin ? because i have border of the row and make it gone when use fit ?

Not sure if you still need this, but what I did was using the same cellId fits inside the cell. I think this works because the same cell is used for the top left (tl) and the bottom right (br).

sheet.addImage(imageId, `${cellId}:${cellId}`)

Example:

sheet.addImage(imageId, `J13:J13`)

Great, that is what I need! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants