excel-builder
Version:
An easy way of building Excel files with javascript
48 lines (43 loc) • 1.59 kB
JavaScript
/**
* @module Excel/Drawings
*/
;
var _ = require('lodash');
var util = require('./util');
var RelationshipManager = require('./RelationshipManager');
var Drawings = function () {
this.drawings = [];
this.relations = new RelationshipManager();
this.id = _.uniqueId('Drawings');
};
_.extend(Drawings.prototype, {
/**
* Adds a drawing (more likely a subclass of a Drawing) to the 'Drawings' for a particular worksheet.
*
* @param {Drawing} drawing
* @returns {undefined}
*/
addDrawing: function (drawing) {
this.drawings.push(drawing);
},
getCount: function () {
return this.drawings.length;
},
toXML: function () {
var doc = util.createXmlDoc(util.schemas.spreadsheetDrawing, 'xdr:wsDr');
var drawings = doc.documentElement;
drawings.setAttribute('xmlns:a', util.schemas.drawing);
drawings.setAttribute('xmlns:r', util.schemas.relationships);
drawings.setAttribute('xmlns:xdr', util.schemas.spreadsheetDrawing);
for(var i = 0, l = this.drawings.length; i < l; i++) {
var rId = this.relations.getRelationshipId(this.drawings[i].getMediaData());
if(!rId) {
rId = this.relations.addRelation(this.drawings[i].getMediaData(), this.drawings[i].getMediaType()); //chart
}
this.drawings[i].setRelationshipId(rId);
drawings.appendChild(this.drawings[i].toXML(doc));
}
return doc;
}
});
module.exports = Drawings;