From f5721e4027fe375b2ff35438df9121e5c9dd9e5c Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Tue, 29 Nov 2016 10:30:53 +0100 Subject: [PATCH] Implement POC for image embedding --- xlsx.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/xlsx.js b/xlsx.js index c29f5ce..c07557b 100644 --- a/xlsx.js +++ b/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] = ''; if(o.length>2){ o[o.length] = (''); 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 = ''+fromCol+'0'+fromRow+'0'; + twoCell += ''+toCol+'0'+toRow+'99999'; + twoCell += '' + twoCell += ''; + twoCell += ''; + twoCell += ''; + o[o.length] = (writextag('xdr:twoCellAnchor', twoCell, images[0].attrs)); + } + } + + if(o.length>2){ o[o.length] = (''); 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] = ''; + if(o.length>2) { o[o.length] = (''); 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);