Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions 27 index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,31 @@ export interface Image {
filename?: string;
buffer?: Buffer;
}

export interface IAnchor {
col: number;
row: number;
nativeCol: number;
nativeRow: number;
nativeColOff: number;
nativeRowOff: number;
}
export class Anchor implements IAnchor{
col: number;
nativeCol: number;
nativeColOff: number;
nativeRow: number;
nativeRowOff: number;
row: number;

private readonly colWidth: number;
private readonly rowHeight: number;
worksheet: Worksheet;

constructor(model: IAnchor|object = {});
}
export interface ImageRange {
tl: { col: number; row: number };
br: { col: number; row: number };
tl: { col: number; row: number } | Anchor;
br: { col: number; row: number } | Anchor;
}

export interface Range extends Location {
Expand Down
56 changes: 56 additions & 0 deletions 56 lib/doc/anchor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2018 Paweł Siemienik
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think this shouldn't be in individual files

Copy link
Member Author

@Siemienik Siemienik Jan 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alubbe In each file is copyright/author block similar to this, if it's wrong could you write how it should looks like?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah indeed - I had completely missed that. Let's leave that in then.

@guyonroche I've only seen licenses repeated in every file if you want to charge license fees for your IP, but since we use MIT I think there's no need to have this repeated everywhere - it's just noise that needs maintenance. Would you be okay with removing them everywhere?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alubbe Yeah - We could probably get away with just the LICENCE file with links from the README.md and main excel.js file.
I finally have more spare time now so I can start working through the PRs. I'll test this one tomorrow morning

* LICENCE: MIT - please refer to LICENCE file included with this module
* or https://github.com/guyonroche/exceljs/blob/master/LICENSE
*/

'use strict';

var Anchor = module.exports = function (model = {}) {
this.nativeCol = model.nativeCol || 0;
this.nativeColOff = model.nativeColOff || 0;
this.nativeRow = model.nativeRow || 0;
this.nativeRowOff = model.nativeRowOff || 0;

if (model.col) {
this.col = model.col;
}

if (model.row) {
this.row = model.row;
}
};

Anchor.asInstance = function (model) {
return model instanceof Anchor || model == null ? model : new Anchor(model);
};

Anchor.prototype = {
worksheet: null,
get col() {
return this.nativeCol + Math.min(this.colWidth - 1, this.nativeColOff) / this.colWidth;
},
set col(v) {
if (v === this.col) return;
this.nativeCol = Math.floor(v);
this.nativeColOff = Math.floor((v - this.nativeCol) * this.colWidth);
},
get row() {
return this.nativeRow + Math.min(this.rowHeight - 1, this.nativeRowOff) / this.rowHeight;
},
set row(v) {
if (v === this.row) return;
this.nativeRow = Math.floor(v);
this.nativeRowOff = Math.floor((v - this.nativeRow) * this.rowHeight);
},
get colWidth() {
return this.worksheet && this.worksheet.getColumn(this.nativeCol + 1) && this.worksheet.getColumn(this.nativeCol + 1).isCustomWidth ?
Math.floor(this.worksheet.getColumn(this.nativeCol + 1).width * 10000) :
640000;
},
get rowHeight() {
return this.worksheet && this.worksheet.getRow(this.nativeRow + 1) && this.worksheet.getRow(this.nativeRow + 1).height ?
Math.floor(this.worksheet.getRow(this.nativeRow + 1).height * 10000) :
180000;
}
};
18 changes: 18 additions & 0 deletions 18 lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Range = require('./range');
var Row = require('./row');
var Column = require('./column');
var Enums = require('./enums');
var Anchor = require('./anchor');
var DataValidations = require('./data-validations');

// Worksheet requirements
Expand Down Expand Up @@ -515,6 +516,16 @@ Worksheet.prototype = {
// =========================================================================
// Images
addImage: function addImageToCells(imageId, range) {
if (range.tl && !(range.tl instanceof Anchor)) {
range.tl = new Anchor(range.tl);
range.tl.worksheet = this;
}

if (range.br && !(range.br instanceof Anchor)) {
range.br = new Anchor(range.br);
range.br.worksheet = this;
}

this._media.push({
type: 'image',
imageId,
Expand Down Expand Up @@ -620,5 +631,12 @@ Worksheet.prototype = {
this.views = value.views;
this.autoFilter = value.autoFilter;
this._media = value.media;

this._media
.filter(m => m.type === 'image' && typeof m.range !== 'string')
.forEach(i => {
i.range.tl.worksheet = this;
i.range.br.worksheet = this;
})
}
};
27 changes: 15 additions & 12 deletions 27 lib/xlsx/xform/drawing/cell-position-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
var utils = require('../../../utils/utils');
var BaseXform = require('../base-xform');
var IntegerXform = require('../simple/integer-xform');
var Anchor = require('../../../doc/anchor');

var CellPositionXform = module.exports = function(options) {
this.tag = options.tag;
Expand All @@ -19,21 +20,21 @@ var CellPositionXform = module.exports = function(options) {
'xdr:rowOff': new IntegerXform({tag: 'xdr:rowOff', zero: true})
};
};
CellPositionXform.buildModel = function (model) {
return new Anchor(model);
};


utils.inherits(CellPositionXform, BaseXform, {

render: function(xmlStream, model) {
xmlStream.openNode(this.tag);

var col = Math.floor(model.col);
var colOff = Math.floor((model.col - col) * 640000);
this.map['xdr:col'].render(xmlStream, col);
this.map['xdr:colOff'].render(xmlStream, colOff);
this.map['xdr:col'].render(xmlStream, model.nativeCol);
this.map['xdr:colOff'].render(xmlStream, model.nativeColOff);

var row = Math.floor(model.row);
var rowOff = Math.floor((model.row - row) * 180000);
this.map['xdr:row'].render(xmlStream, row);
this.map['xdr:rowOff'].render(xmlStream, rowOff);
this.map['xdr:row'].render(xmlStream, model.nativeRow);
this.map['xdr:rowOff'].render(xmlStream, model.nativeRowOff);

xmlStream.closeNode();
},
Expand Down Expand Up @@ -72,10 +73,12 @@ utils.inherits(CellPositionXform, BaseXform, {
}
switch (name) {
case this.tag:
this.model = {
col: this.map['xdr:col'].model + (this.map['xdr:colOff'].model / 640000),
row: this.map['xdr:row'].model + (this.map['xdr:rowOff'].model / 180000)
};
this.model = CellPositionXform.buildModel({
nativeCol: this.map['xdr:col'].model,
nativeColOff: this.map['xdr:colOff'].model,
nativeRow: this.map['xdr:row'].model,
nativeRowOff: this.map['xdr:rowOff'].model
});
return false;
default:
// not quite sure how we get here!
Expand Down
23 changes: 15 additions & 8 deletions 23 lib/xlsx/xform/drawing/two-cell-anchor-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var utils = require('../../../utils/utils');
var colCache = require('../../../utils/col-cache');
var BaseXform = require('../base-xform');
var StaticXform = require('../static-xform');
var Anchor = require('../../../doc/anchor');

var CellPositionXform = require('./cell-position-xform');
var PicXform = require('./pic-xform');
Expand All @@ -33,16 +34,18 @@ utils.inherits(TwoCellAnchorXform, BaseXform, {
if (typeof model.range === 'string') {
var range = colCache.decode(model.range);
// Note - zero based
model.tl = {
col: range.left - 1,
row: range.top - 1
};
model.tl = CellPositionXform.buildModel({
nativeCol: range.left - 1,
nativeRow: range.top - 1
});
// zero based but also +1 to cover to bottom right of br cell
model.br = {
col: range.right,
row: range.bottom
};
model.br = CellPositionXform.buildModel({
nativeCol: range.right,
nativeRow: range.bottom
});
} else {
model.range.tl = Anchor.asInstance(model.range.tl);
model.range.br = Anchor.asInstance(model.range.br);
model.tl = model.range.tl;
model.br = model.range.br;
}
Expand Down Expand Up @@ -121,6 +124,10 @@ utils.inherits(TwoCellAnchorXform, BaseXform, {
model.medium = options.media[mediaId];
}
}

model.tl = Anchor.asInstance(model.tl);
model.br = Anchor.asInstance(model.br);

if (model.tl && model.br && Number.isInteger(model.tl.row) && Number.isInteger(model.tl.col) && Number.isInteger(model.br.row) && Number.isInteger(model.br.col)) {
model.range = colCache.encode(model.tl.row + 1, model.tl.col + 1, model.br.row, model.br.col);
} else {
Expand Down
Binary file added BIN +23.2 KB spec/integration/data/images.xlsx
Binary file not shown.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.