forked from sheetjs/sheetjs
Implement POC for image embedding
This commit is contained in:
parent
53f7f6d944
commit
f5721e4027
55
xlsx.js
55
xlsx.js
@ -2435,6 +2435,8 @@ var CTYPE_XML_ROOT = writextag('Types', null, {
|
||||
|
||||
var CTYPE_DEFAULTS = [
|
||||
['xml', 'application/xml'],
|
||||
['png', 'image/png'],
|
||||
['jpg', 'image/jpeg'],
|
||||
['bin', 'application/vnd.ms-excel.sheet.binary.macroEnabled.main'],
|
||||
['rels', type2ct.rels[0]]
|
||||
].map(function(x) {
|
||||
@ -2476,6 +2478,7 @@ function write_ct(ct, opts) {
|
||||
f3('themes');
|
||||
['strs', 'styles'].forEach(f1);
|
||||
['coreprops', 'extprops', 'custprops'].forEach(f3);
|
||||
o[o.length] = '<Override PartName="/xl/drawings/drawing1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>';
|
||||
if(o.length>2){ o[o.length] = ('</Types>'); o[1]=o[1].replace("/>",">"); }
|
||||
return o.join("");
|
||||
}
|
||||
@ -2529,6 +2532,39 @@ var RELS_ROOT = writextag('Relationships', null, {
|
||||
'xmlns': XMLNS.RELS
|
||||
});
|
||||
|
||||
var DRAW_ROOT = writextag('xdr:wsDr', null, {
|
||||
'xmlns:xdr': 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing',
|
||||
'xmlns:a': 'http://schemas.openxmlformats.org/drawingml/2006/main'
|
||||
//'xmlns:ns0': XMLNS.RELS,
|
||||
// 'xmlns': XMLNS.RELS
|
||||
});
|
||||
|
||||
function write_drawing(images) {
|
||||
var o = [];
|
||||
o[o.length] = (XML_HEADER);
|
||||
o[o.length] = (DRAW_ROOT);
|
||||
|
||||
for (var i = 0; i < images.length; i++) {
|
||||
var image = images[i];
|
||||
var pos = image.position || {};
|
||||
if (pos.type === 'twoCellAnchor') {
|
||||
var from = pos.from || {}, to = pos.to || {},
|
||||
fromCol = from.col || 0, toCol = to.col || 0,
|
||||
fromRow = from.row || 0, toRow = to.row || 0;
|
||||
|
||||
var twoCell = '<xdr:from><xdr:col>'+fromCol+'</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>'+fromRow+'</xdr:row><xdr:rowOff>0</xdr:rowOff></xdr:from>';
|
||||
twoCell += '<xdr:to><xdr:col>'+toCol+'</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>'+toRow+'</xdr:row><xdr:rowOff>99999</xdr:rowOff></xdr:to>';
|
||||
twoCell += '<xdr:pic><xdr:nvPicPr><xdr:cNvPr id="'+(i+1)+'" name="'+image.name+'">'
|
||||
twoCell += '</xdr:cNvPr><xdr:cNvPicPr><a:picLocks noChangeAspect="1"/></xdr:cNvPicPr></xdr:nvPicPr>';
|
||||
twoCell += '<xdr:blipFill><a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId1"/>';
|
||||
twoCell += '<a:stretch><a:fillRect/></a:stretch></xdr:blipFill><xdr:spPr><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></xdr:spPr></xdr:pic><xdr:clientData/>';
|
||||
o[o.length] = (writextag('xdr:twoCellAnchor', twoCell, images[0].attrs));
|
||||
}
|
||||
}
|
||||
|
||||
if(o.length>2){ o[o.length] = ('</xdr:wsDr>'); o[1]=o[1].replace("/>",">"); }
|
||||
return o.join("");
|
||||
}
|
||||
/* TODO */
|
||||
function write_rels(rels) {
|
||||
var o = [];
|
||||
@ -7562,6 +7598,9 @@ function write_ws_xml(idx, opts, wb) {
|
||||
|
||||
if(ws['!merges'] !== undefined && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges']));
|
||||
|
||||
var images = ws['!images'] || [];
|
||||
if (images.length) o[o.length] = '<drawing r:id="rId1"/>';
|
||||
|
||||
if(o.length>2) { o[o.length] = ('</worksheet>'); o[1]=o[1].replace("/>",">"); }
|
||||
return o.join("");
|
||||
}
|
||||
@ -11277,6 +11316,9 @@ function add_rels(rels, rId, f, type, relobj) {
|
||||
rels[('/' + relobj.Target).replace("//","/")] = relobj;
|
||||
}
|
||||
|
||||
RELS.IMG = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
|
||||
RELS.DRAW = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
|
||||
|
||||
function write_zip(wb, opts) {
|
||||
if(wb && !wb.SSF) {
|
||||
wb.SSF = SSF.get_table();
|
||||
@ -11324,6 +11366,19 @@ function write_zip(wb, opts) {
|
||||
add_rels(opts.rels, 1, f, RELS.WB);
|
||||
|
||||
for(rId=1;rId <= wb.SheetNames.length; ++rId) {
|
||||
var s = wb.SheetNames[rId-1], ws = wb.Sheets[s],
|
||||
images = ws['!images'] || [];
|
||||
var rels = ws['!rels'] = [], draw_rels = [];
|
||||
for (var sId=1; sId <= images.length; ++sId) {
|
||||
var image = images[sId - 1];
|
||||
f = 'xl/media/' + image.name;
|
||||
zip.file(f, image.data, image.opts);
|
||||
add_rels(draw_rels, sId, "../media/" + image.name, RELS.IMG);
|
||||
}
|
||||
zip.file("xl/drawings/drawing" + rId + "." + wbext, write_drawing(images));
|
||||
add_rels(rels, rId, "../drawings/drawing" + rId + "." + wbext, RELS.DRAW);
|
||||
zip.file("xl/drawings/_rels/drawing" + rId + "." + wbext + ".rels", write_rels(draw_rels));
|
||||
zip.file("xl/worksheets/_rels/sheet" + rId + "." + wbext + '.rels', write_rels(rels));
|
||||
f = "xl/worksheets/sheet" + rId + "." + wbext;
|
||||
zip.file(f, write_ws(rId-1, f, opts, wb));
|
||||
ct.sheets.push(f);
|
||||
|
Loading…
Reference in New Issue
Block a user