question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong image position for non-integer coordinates

See original GitHub issue

Hi all, I’m trying to place 16x16 image at arbitrary place of the worksheet. I set col number to 1.5, but it looks like image appears at the beginning of “B” column PLUS half of default column width, not half “B” column width.

Code:

const exceljs = require('exceljs');

const workbook = new exceljs.Workbook();
const worksheet = workbook.addWorksheet();

worksheet.columns = [{ width: 10 }, { width: 100 }, { width: 50 }];

const imageData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAA'
	+ 'AQCAYAAAAf8/9hAAADBklEQVR42n2Se0hTURzHv3f3Xh/VysdmzlXLaiaIZtnTtMh'
	+ 'qVmNUaNADXFlBDwqCHlD/VERRRmVJ6001wqCgl9ibXKYSZajhtNK9tG1OF97yzrbr'
	+ 'vbdb/ZU0D/w4cM6Pz5fzOT8Cg9ZinSFW2t5LNUEURT9E8S0IYTzHDey3VD1/MLifG'
	+ 'HyQt2iZmxd4FSGK6GUCYNkgaJpCbEwUBEGYW1drqQ0LyM5ZoCQI+ESRgLPjG1YVpC'
	+ 'NVq0BFZTM+NHqhVsn3vntbUxIWMGfO3HHebtb5vZ/HOGUkLM82Q64cCwgMitaZ8bL'
	+ 'OvsftbDkZ/gmqGepDxVM7V+eORMqUZOmWBu9nQGpGoaHJh5kGs5Hzf7oZFnArZ8TY'
	+ 'tY+uuxB0AF4vBvo8kkMO9HASPmcAV858zD1gsb0ZUqLYcFQMdVVD7GcgIymIkCEiJ'
	+ 'hI/bL14X2bV5r370TYkwHcwvVE5X5PBe+wQqFF/OmjSD9P5zhfbXrCLh/5GKnHq8d'
	+ '1rK/fqoxOZumuQ8wz6Qz8RnVqA6z4d9pku7Oux1p/4L0Cl1mwsLCy8cvaMJFkU0P+'
	+ '5HvaOz0hUJCAu82+wftlKNDU3VXa6bPp/AIoEla7YuOEpFUH/HhYcO3oY1X6gzAVs'
	+ 'SQEWUAGcu2yGyXQJCkUs3O6v59vbWrf/ASgTVEuSkyceWW8syrp48TLYAAvDUh1Ol'
	+ 'Z7Grtc9OD1PAVOZCUeOlUCbMgkURcHj8YDt+25wOm0VREZmVoVOl693OFxoa/+CUJ'
	+ 'BD2mQtyu/expr7X3FrhRonDh7CVXM5ElUqyGQyBCUvvq6uGpejPYfIW5jfNH3mrPS'
	+ 'qV1XgeQ4cx0Mpp1H68DWW3mjB461puLBjE+49sUCdlARp1CGjCHQ4XZ5Olz2JmDE7'
	+ 'u36MWj3NZnOAJEkpgQLFduNUdSuyr/bgklEBf8lOlN15gtGKeCmAw4BUDMNIKpxqY'
	+ 'oQ8Jl7Sro2IiDLSNDmLkFGp+MnyU3TL8fRTHzI1cYi2v6Ga7W5v9LBhjVwoaBV41N'
	+ 'CRpLXb57X/AnGNSiD8VccPAAAAAElFTkSuQmCC';

const imageIndex = workbook.addImage({ base64: imageData, extension: 'png' });
const imageRange = {
	tl: { col: 1.5, row: 1 },
	ext: { width: 16, height: 16 }
};

worksheet.addImage(imageIndex, imageRange);
workbook.xlsx.writeFile('./output.xlsx');

Expected result: Image appear in the middle of “B” column.

Observed result: Image appear near left border of “B” column.

Environment:

  • exceljs: 1.13.0
  • nodejs: 10.13.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:6

github_iconTop GitHub Comments

1reaction
SuyongSuncommented, Mar 23, 2021

@Siemienik @r4tz52

I read the code related with this problem. Finally, I found the reason occured in anchor.js. There is my fix below.

Put the following code in the first line in constructor function. After this we can get the real column width and row height in worksheet. this.worksheet = worksheet;

Then, modify the colWidth and rowHeight getters methods to get the correct number. As the default height or width can be undefined in worksheet properties, so we can add some protection code for below sample. We can check if the default heigth or width defined, if not, use constant value instead. (for example, height is 15, width is 9.14XXXXXX.) Math.floor(this.worksheet.getRow(this.nativeRow + 1).height/this.worksheet.properties.defaultRowHeight * 180000) Math.floor(this.worksheet.getColumn(this.nativeCol + 1).width/this.worksheet.properties.defaultColWidth * 640000)

as the following code we put the image in xlsx with correct positions. worksheet.addImage(imageId, { tl: {col: 1.1, row: 5.83 }, br: {col: 8.85, row: 10.2 } }); testimage

0reactions
SuyongSuncommented, Mar 17, 2021

I make some test and lookup the data affter unziping the drawing.xml file. I found that the error is caused by the wrong colwidth and rowheight. The tl and br are set to float number, if the col and row are not resized, the result is ok. But when width or height of the cell, in which tl or br defined, were resized, the result were still calculated with the standard cell width and row. So we got the wrong result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does imshow display non-integer x and y values for the ...
My guess is, that the float values you worry about while hovering over the shown image with your mouse is just the mouse...
Read more >
How do I transfer points from/to rectified images? - MathWorks
I am using rectifyStereoImages to rectify a pair of images. How can I transfer the coordinates of a non-integer point between normal and...
Read more >
Images with non-integer coordinates are incorrectly clipped in ...
Something went wrong while fetching related merge requests. ... Images with non-integer coordinates are incorrectly clipped in pdf backend.
Read more >
How to get the non-integer coordinate pixels. [closed] edit
I am using affine transformation to get the warped image and I am ... to get the location of newly created pixels (non-integer...
Read more >
How to verify that the Image Geolocation and ... - Pix4D Support
3. If the wrong coordinate system is selected, click Edit... in the Coordinate System under the Image Geolocation section. 4. On the Select...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found