Skip to content
This repository was archived by the owner on Jun 2, 2022. It is now read-only.

Commit 7fc1bf3

Browse files
authored
Merge pull request #3 from mgreter/master
Basic image embedding and row height feature
2 parents eb891c8 + 372c484 commit 7fc1bf3

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

xlsx.js

+71-3
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ return exports;
12551255
if(typeof require !== 'undefined' && typeof module !== 'undefined' && typeof DO_NOT_EXPORT_CFB === 'undefined') { module.exports = CFB; }
12561256
function isval(x) { return x !== undefined && x !== null; }
12571257

1258-
function keys(o) { return Object.keys(o); }
1258+
function keys(o) { return o != null ? Object.keys(o) : []; }
12591259

12601260
function evert_key(obj, key) {
12611261
var o = [], K = keys(obj);
@@ -2435,6 +2435,8 @@ var CTYPE_XML_ROOT = writextag('Types', null, {
24352435

24362436
var CTYPE_DEFAULTS = [
24372437
['xml', 'application/xml'],
2438+
['png', 'image/png'],
2439+
['jpg', 'image/jpeg'],
24382440
['bin', 'application/vnd.ms-excel.sheet.binary.macroEnabled.main'],
24392441
['rels', type2ct.rels[0]]
24402442
].map(function(x) {
@@ -2476,6 +2478,7 @@ function write_ct(ct, opts) {
24762478
f3('themes');
24772479
['strs', 'styles'].forEach(f1);
24782480
['coreprops', 'extprops', 'custprops'].forEach(f3);
2481+
o[o.length] = '<Override PartName="/xl/drawings/drawing1.xml" ContentType="application/vnd.openxmlformats-officedocument.drawing+xml"/>';
24792482
if(o.length>2){ o[o.length] = ('</Types>'); o[1]=o[1].replace("/>",">"); }
24802483
return o.join("");
24812484
}
@@ -2529,6 +2532,39 @@ var RELS_ROOT = writextag('Relationships', null, {
25292532
'xmlns': XMLNS.RELS
25302533
});
25312534

2535+
var DRAW_ROOT = writextag('xdr:wsDr', null, {
2536+
'xmlns:xdr': 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing',
2537+
'xmlns:a': 'http://schemas.openxmlformats.org/drawingml/2006/main'
2538+
//'xmlns:ns0': XMLNS.RELS,
2539+
// 'xmlns': XMLNS.RELS
2540+
});
2541+
2542+
function write_drawing(images) {
2543+
var o = [];
2544+
o[o.length] = (XML_HEADER);
2545+
o[o.length] = (DRAW_ROOT);
2546+
2547+
for (var i = 0; i < images.length; i++) {
2548+
var image = images[i];
2549+
var pos = image.position || {};
2550+
if (pos.type === 'twoCellAnchor') {
2551+
var from = pos.from || {}, to = pos.to || {},
2552+
fromCol = from.col || 0, toCol = to.col || 0,
2553+
fromRow = from.row || 0, toRow = to.row || 0;
2554+
2555+
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>';
2556+
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>';
2557+
twoCell += '<xdr:pic><xdr:nvPicPr><xdr:cNvPr id="'+(i+1)+'" name="'+image.name+'">'
2558+
twoCell += '</xdr:cNvPr><xdr:cNvPicPr><a:picLocks noChangeAspect="1"/></xdr:cNvPicPr></xdr:nvPicPr>';
2559+
twoCell += '<xdr:blipFill><a:blip xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="rId'+(i+1)+'"/>';
2560+
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/>';
2561+
o[o.length] = (writextag('xdr:twoCellAnchor', twoCell, images[i].attrs));
2562+
}
2563+
}
2564+
2565+
if(o.length>2){ o[o.length] = ('</xdr:wsDr>'); o[1]=o[1].replace("/>",">"); }
2566+
return o.join("");
2567+
}
25322568
/* TODO */
25332569
function write_rels(rels) {
25342570
var o = [];
@@ -4601,6 +4637,7 @@ function rgb_tint(hex, tint) {
46014637
var DEF_MDW = 7, MAX_MDW = 15, MIN_MDW = 1, MDW = DEF_MDW;
46024638
function width2px(width) { return (( width + ((128/MDW)|0)/256 )* MDW )|0; }
46034639
function px2char(px) { return (((px - 5)/MDW * 100 + 0.5)|0)/100; }
4640+
function px2pt(px) { return px * 72 / 96; }
46044641
function char2width(chr) { return (((chr * MDW + 5)/MDW*256)|0)/256; }
46054642
function cycle_width(collw) { return char2width(px2char(width2px(collw))); }
46064643
function find_mdw(collw, coll) {
@@ -7542,8 +7579,20 @@ function write_ws_xml_data(ws, opts, idx, wb) {
75427579
if(ws[ref] === undefined) continue;
75437580
if((cell = write_ws_xml_cell(ws[ref], ref, ws, opts, idx, wb)) != null) r.push(cell);
75447581
}
7545-
if(r.length > 0) o[o.length] = (writextag('row', r.join(""), {r:rr}));
7546-
}
7582+
if(r.length > 0) {
7583+
// 18.3.1.73 row
7584+
var params = {r:rr};
7585+
if(typeof ws['!rows'] !== 'undefined' && ws['!rows'].length > R) {
7586+
var row = ws['!rows'][R];
7587+
if (row.hidden) params.hidden = 1;
7588+
var height = -1;
7589+
if (row.hpx) height = px2pt(row.hpx);
7590+
else if (row.hpt) height = row.hpt;
7591+
if (height > -1) { params.ht = height; params.customHeight = 1; }
7592+
};
7593+
o[o.length] = (writextag('row', r.join(""), params));
7594+
}
7595+
}
75477596
return o.join("");
75487597
}
75497598

@@ -7570,6 +7619,9 @@ function write_ws_xml(idx, opts, wb) {
75707619

75717620
if(ws['!merges'] !== undefined && ws['!merges'].length > 0) o[o.length] = (write_ws_xml_merges(ws['!merges']));
75727621

7622+
var images = ws['!images'] || [];
7623+
if (images.length) o[o.length] = '<drawing r:id="rId1"/>';
7624+
75737625
if(o.length>2) { o[o.length] = ('</worksheet>'); o[1]=o[1].replace("/>",">"); }
75747626
return o.join("");
75757627
}
@@ -11285,6 +11337,9 @@ function add_rels(rels, rId, f, type, relobj) {
1128511337
rels[('/' + relobj.Target).replace("//","/")] = relobj;
1128611338
}
1128711339

11340+
RELS.IMG = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
11341+
RELS.DRAW = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
11342+
1128811343
function write_zip(wb, opts) {
1128911344
if(wb && !wb.SSF) {
1129011345
wb.SSF = SSF.get_table();
@@ -11332,6 +11387,19 @@ function write_zip(wb, opts) {
1133211387
add_rels(opts.rels, 1, f, RELS.WB);
1133311388

1133411389
for(rId=1;rId <= wb.SheetNames.length; ++rId) {
11390+
var s = wb.SheetNames[rId-1], ws = wb.Sheets[s],
11391+
images = ws['!images'] || [];
11392+
var rels = ws['!rels'] = [], draw_rels = [];
11393+
for (var sId=1; sId < images.length+1; ++sId) {
11394+
var image = images[sId - 1];
11395+
f = 'xl/media/' + image.name;
11396+
zip.file(f, image.data, image.opts);
11397+
add_rels(draw_rels, sId, "../media/" + image.name, RELS.IMG);
11398+
}
11399+
zip.file("xl/drawings/drawing" + rId + "." + wbext, write_drawing(images));
11400+
add_rels(rels, rId, "../drawings/drawing" + rId + "." + wbext, RELS.DRAW);
11401+
zip.file("xl/drawings/_rels/drawing" + rId + "." + wbext + ".rels", write_rels(draw_rels));
11402+
zip.file("xl/worksheets/_rels/sheet" + rId + "." + wbext + '.rels', write_rels(rels));
1133511403
f = "xl/worksheets/sheet" + rId + "." + wbext;
1133611404
zip.file(f, write_ws(rId-1, f, opts, wb));
1133711405
ct.sheets.push(f);

0 commit comments

Comments
 (0)