Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/p3rio/exceljs into p3rio-ma…
Browse files Browse the repository at this point in the history
…ster
  • Loading branch information
guyonroche committed Mar 29, 2019
2 parents b0bf7f7 + 4613acc commit 28b9294
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 120 deletions.
27 changes: 24 additions & 3 deletions 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 lib/doc/anchor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2018 Paweł Siemienik
* 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 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 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 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 spec/integration/data/images.xlsx
Binary file not shown.
Loading

0 comments on commit 28b9294

Please sign in to comment.