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

fix: addImage position is wrong #2614

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

newbeea
Copy link

@newbeea newbeea commented Dec 5, 2023

Summary

This code covers fix for #1820 #894
The image's position is wrong when set range with float numbers and change ws.properties.defaultRowHeight or defaultColWidth.
The position is also wrong when set column's width or row's height

Test plan

Change defaultRowHeight defaultColWidth:

const fs = require("fs");
const path = require("path");

const HrStopwatch = require("./utils/hr-stopwatch");

const { Workbook } = require("../lib/exceljs.nodejs");

const filename = process.argv[2];

const wb = new Workbook();
const ws = wb.addWorksheet("blort");
ws.properties.defaultRowHeight = 100;
ws.properties.defaultColWidth = 20;

const imageId = wb.addImage({
  filename: path.join(__dirname, "data/image2.png"),
  extension: "png",
});

// ws.getRow(1).height = 100;
// ws.getColumn(1).width = 20;

ws.addImage(imageId, {
  tl: { col: 0.5, row: 0.5 },
  ext: { width: 100, height: 100 },
});


const stopwatch = new HrStopwatch();
stopwatch.start();
wb.xlsx
  .writeFile(filename)
  .then(() => {
    const micros = stopwatch.microseconds;
    console.log("Done.");
    console.log("Time taken:", micros);
  })
  .catch((error) => {
    console.error(error.stack);
  });

Bug: image lefttop does not at center of cell
default-height100-width20
default-height100-width20.xlsx

After this PR:
default-height100-width20-fixed
default-height100-width20-fixed.xlsx

Change colWidth rowHeight

const fs = require("fs");
const path = require("path");

const HrStopwatch = require("./utils/hr-stopwatch");

const { Workbook } = require("../lib/exceljs.nodejs");

const filename = process.argv[2];

const wb = new Workbook();
const ws = wb.addWorksheet("blort");
// ws.properties.defaultRowHeight = 100;
// ws.properties.defaultColWidth = 20;

const imageId = wb.addImage({
  filename: path.join(__dirname, "data/image2.png"),
  extension: "png",
});

ws.getRow(1).height = 100;
ws.getColumn(1).width = 20;

ws.addImage(imageId, {
  tl: { col: 0.5, row: 0.5 },
  ext: { width: 100, height: 100 },
});


const stopwatch = new HrStopwatch();
stopwatch.start();
wb.xlsx
  .writeFile(filename)
  .then(() => {
    const micros = stopwatch.microseconds;
    console.log("Done.");
    console.log("Time taken:", micros);
  })
  .catch((error) => {
    console.error(error.stack);
  });

Bug: image lefttop does not at center of cell
custom-height100-width20
custom-height100-width20.xlsx

After this PR:
custom-height100-width20-fixed
custom-height100-width20-fixed.xlsx

Related to source code (for typings update)

Typings update is not required.

@thomasmison
Copy link

I have the same issue, the fix supplied seems to work correctly on my side.

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

Successfully merging this pull request may close these issues.

None yet

2 participants