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

Impl <xdr:twoCellAnchor editAs=oneCell> #407

Merged
merged 7 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Impl <xdr:twoCellAnchor editAs=oneCell>
  • Loading branch information
Ockejanssen committed Sep 21, 2017
commit c7c213216c2047517e275d4c513bb7434bf8c805
2 changes: 1 addition & 1 deletion lib/xlsx/xform/drawing/cell-position-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ utils.inherits(CellPositionXform, BaseXform, {
this.map['xdr:col'].render(xmlStream, col);
this.map['xdr:colOff'].render(xmlStream, colOff);

var row = Math.floor(model.row);
var row = model.editAsOneCell ? 0 : Math.floor(model.row);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't the same adjustment be done to col (above)?

Also - is there any adjustment required for the parse functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll take a look at the parse function. About the col I don't know, I just looked into what OOo writes and did the same ;-)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've finally had some time to look into this. From what I can see (based on how Excel behaves) the row adjustment is not necessary, so I've removed it.

var rowOff = Math.floor((model.row - row) * 180000);
this.map['xdr:row'].render(xmlStream, row);
this.map['xdr:rowOff'].render(xmlStream, rowOff);
Expand Down
25 changes: 18 additions & 7 deletions lib/xlsx/xform/drawing/two-cell-anchor-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ utils.inherits(TwoCellAnchorXform, BaseXform, {
// Note - zero based
model.tl = {
col: range.left - 1,
row: range.top - 1,
row: range.top - 1
};
// zero based but also +1 to cover to bottom right of br cell
model.br = {
Expand All @@ -49,7 +49,12 @@ utils.inherits(TwoCellAnchorXform, BaseXform, {
},

render: function(xmlStream, model) {
xmlStream.openNode(this.tag);
if(model.range.editAs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know it's pedantic but could you add braces to this if statement

xmlStream.openNode(this.tag, {editAs: model.range.editAs});
else
xmlStream.openNode(this.tag);
model.tl.editAsOneCell = model.range.editAs == 'oneCell';
model.br.editAsOneCell = model.range.editAs == 'oneCell';

this.map['xdr:from'].render(xmlStream, model.tl);
this.map['xdr:to'].render(xmlStream, model.br);
Expand All @@ -67,6 +72,9 @@ utils.inherits(TwoCellAnchorXform, BaseXform, {
switch (node.name) {
case this.tag:
this.reset();
this.model = {
editAs: node.attributes['editAs']
};
break;
default:
this.parser = this.map[node.name];
Expand All @@ -93,11 +101,10 @@ utils.inherits(TwoCellAnchorXform, BaseXform, {
}
switch (name) {
case this.tag:
this.model = {
tl: this.map['xdr:from'].model,
br: this.map['xdr:to'].model,
picture: this.map['xdr:pic'].model,
};
this.model = this.model || {};
this.model.tl = this.map['xdr:from'].model;
this.model.br = this.map['xdr:to'].model;
this.model.picture = this.map['xdr:pic'].model;
return false;
default:
// could be some unrecognised tags
Expand All @@ -121,6 +128,10 @@ utils.inherits(TwoCellAnchorXform, BaseXform, {
br: model.br,
};
}
if (model.editAs) {
model.range.editAs = model.editAs;
delete model.editAs;
}
delete model.tl;
delete model.br;
}
Expand Down
39 changes: 39 additions & 0 deletions spec/integration/workbook/images.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,44 @@ describe('Workbook', function() {
expect(Buffer.compare(imageData, image.buffer)).to.equal(0);
});
});

it('stores embedded image with oneCell', function() {
var wb = new Excel.Workbook();
var ws = wb.addWorksheet('blort');
var wb2, ws2;

var imageId = wb.addImage({
filename: IMAGE_FILENAME,
extension: 'jpeg',
});

ws.addImage(imageId, {
tl: { col: 0.1125, row: 0.4 },
br: { col: 2.101046875, row: 3.4 },
editAs: 'oneCell'
});

return wb.xlsx.writeFile(TEST_XLSX_FILE_NAME)
.then(function() {
wb2 = new Excel.Workbook();
return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);
})
.then(function() {
ws2 = wb2.getWorksheet('blort');
expect(ws2).to.not.be.undefined();

return fsReadFileAsync(IMAGE_FILENAME);
})
.then(function(imageData) {
const images = ws2.getImages();
expect(images.length).to.equal(1);

const imageDesc = images[0];
expect(imageDesc.range.editAs).to.equal('oneCell');

const image = wb2.getImage(imageDesc.imageId);
expect(Buffer.compare(imageData, image.buffer)).to.equal(0);
});
});
});
});
33 changes: 33 additions & 0 deletions test/test-image-oneCell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var fs = require('fs');
var path = require('path');

var HrStopwatch = require('./utils/hr-stopwatch');

var Workbook = require('../excel').Workbook;

var filename = process.argv[2];

var wb = new Workbook();
var ws = wb.addWorksheet('blort');

var imageId = wb.addImage({
filename: path.join(__dirname, 'data/image2.png'),
extension: 'png',
});
ws.addImage(imageId, {
tl: { col: 0.1125, row: 0.4 },
br: { col: 2.101046875, row: 3.4 },
editAs: 'oneCell'
});

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