Skip to content

fix issue #899 Support for inserting pictures with hyperlinks #1071

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

Merged
merged 4 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,21 @@ worksheet.addImage(imageId2, {
});
```

### Add image with hyperlinks

You can add an image with hyperlinks to a cell, and defines the hyperlinks in image range.

```javascript
worksheet.addImage(imageId2, {
tl: { col: 0, row: 0 },
ext: { width: 500, height: 200 },
hyperlinks: {
hyperlink: 'http://www.somewhere.com',
tooltip: 'http://www.somewhere.com'
}
});
```

## Sheet Protection

Worksheets can be protected from modification by adding a password.
Expand Down
15 changes: 15 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,21 @@ worksheet.addImage(imageId2, {
});
```

### <a id="add-image-with-hyperlinks">添加带超链接的图片</a>

You can add an image with hyperlinks to a cell.

```javascript
worksheet.addImage(imageId2, {
tl: { col: 0, row: 0 },
ext: { width: 500, height: 200 },
hyperlinks: {
hyperlink: 'http://www.somewhere.com',
tooltip: 'http://www.somewhere.com'
}
});
```

## <a id="file-io">文件 I/O</a>

### <a id="xlsx">XLSX</a>
Expand Down
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,11 @@ export interface ImagePosition {
ext: { width: number; height: number };
}

export interface ImageHyperlinkValue {
hyperlink: string;
tooltip?: string;
}

export interface Range extends Location {
sheetName: string;

Expand Down Expand Up @@ -1142,7 +1147,7 @@ export interface Worksheet {
* Using the image id from `Workbook.addImage`,
* embed an image within the worksheet to cover a range
*/
addImage(imageId: number, range: string | { editAs?: string; } & ImageRange | { editAs?: string; } & ImagePosition): void;
addImage(imageId: number, range: string | { editAs?: string; } & ImageRange & {hyperlinks?: ImageHyperlinkValue} | { editAs?: string; } & ImagePosition & {hyperlinks?: ImageHyperlinkValue}): void;

getImages(): Array<{
type: 'image',
Expand Down
6 changes: 4 additions & 2 deletions lib/doc/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Image {
return {
type: this.type,
imageId: this.imageId,
hyperlinks: this.range.hyperlinks,
range: {
tl: this.range.tl.model,
br: this.range.br && this.range.br.model,
Expand All @@ -29,7 +30,7 @@ class Image {
}
}

set model({type, imageId, range}) {
set model({type, imageId, range, hyperlinks}) {
this.type = type;
this.imageId = imageId;

Expand All @@ -47,10 +48,11 @@ class Image {
br: range.br && new Anchor(this.worksheet, range.br, 0),
ext: range.ext,
editAs: range.editAs,
hyperlinks: hyperlinks || range.hyperlinks,
};
}
}
}
}

module.exports = Image;
module.exports = Image;
39 changes: 39 additions & 0 deletions lib/xlsx/xform/drawing/c-nv-pic-pr-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const BaseXform = require('../base-xform');

class CNvPicPrXform extends BaseXform {

get tag() {
return 'xdr:cNvPicPr';
}

render(xmlStream) {
xmlStream.openNode(this.tag);
xmlStream.leafNode('a:picLocks', {
noChangeAspect: '1',
});
xmlStream.closeNode();
}

parseOpen(node) {
switch (node.name) {
case this.tag:
return true;
default:
return true;
}
}

parseText() {}

parseClose(name) {
switch (name) {
case this.tag:
return false;
default:
// unprocessed internal nodes
return true;
}
}
}

module.exports = CNvPicPrXform;
68 changes: 68 additions & 0 deletions lib/xlsx/xform/drawing/c-nv-pr-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const BaseXform = require('../base-xform');
const HlickClickXform = require('./hlink-click-xform');
const ExtLstXform = require('./ext-lst-xform');

class CNvPrXform extends BaseXform {
constructor() {
super();

this.map = {
'a:hlinkClick': new HlickClickXform(),
'a:extLst': new ExtLstXform(),
};
}

get tag() {
return 'xdr:cNvPr';
}

render(xmlStream, model) {
xmlStream.openNode(this.tag, {
id: model.index,
name: `Picture ${model.index}`,
});
this.map['a:hlinkClick'].render(xmlStream, model);
this.map['a:extLst'].render(xmlStream, model);
xmlStream.closeNode();
}

parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}

switch (node.name) {
case this.tag:
this.reset();
break;
default:
this.parser = this.map[node.name];
if (this.parser) {
this.parser.parseOpen(node);
}
break;
}
return true;
}

parseText() {}

parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
this.parser = undefined;
}
return true;
}
switch (name) {
case this.tag:
this.model = this.map['a:hlinkClick'].model;
return false;
default:
return true;
}
}
}

module.exports = CNvPrXform;
43 changes: 43 additions & 0 deletions lib/xlsx/xform/drawing/ext-lst-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const BaseXform = require('../base-xform');

class ExtLstXform extends BaseXform {
get tag() {
return 'a:extLst';
}

render(xmlStream) {
xmlStream.openNode(this.tag);
xmlStream.openNode('a:ext', {
uri: '{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}',
});
xmlStream.leafNode('a16:creationId', {
'xmlns:a16': 'http://schemas.microsoft.com/office/drawing/2014/main',
id: '{00000000-0008-0000-0000-000002000000}',
});
xmlStream.closeNode();
xmlStream.closeNode();
}

parseOpen(node) {
switch (node.name) {
case this.tag:
return true;
default:
return true;
}
}

parseText() {}

parseClose(name) {
switch (name) {
case this.tag:
return false;
default:
// unprocessed internal nodes
return true;
}
}
}

module.exports = ExtLstXform;
42 changes: 42 additions & 0 deletions lib/xlsx/xform/drawing/hlink-click-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const BaseXform = require('../base-xform');

class HLinkClickXform extends BaseXform {

get tag() {
return 'a:hlinkClick';
}

render(xmlStream, model) {
if (!(model.hyperlinks && model.hyperlinks.rId)) {
return;
}
xmlStream.leafNode(this.tag, {
'xmlns:r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
'r:id': model.hyperlinks.rId,
'tooltip': model.hyperlinks.tooltip,
});
}

parseOpen(node) {
switch (node.name) {
case this.tag:
this.model = {
hyperlinks: {
rId: node.attributes['r:id'],
tooltip: node.attributes.tooltip,
},
};
return true;
default:
return true;
}
}

parseText() {}

parseClose() {
return false;
}
}

module.exports = HLinkClickXform;
88 changes: 53 additions & 35 deletions lib/xlsx/xform/drawing/nv-pic-pr-xform.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
const BaseXform = require('../base-xform');
const StaticXform = require('../static-xform');
const CNvPrXform = require('./c-nv-pr-xform');
const CNvPicPrXform = require('./c-nv-pic-pr-xform');

class NvPicPrXform extends BaseXform {
constructor() {
super();

this.map = {
'xdr:cNvPr': new CNvPrXform(),
'xdr:cNvPicPr': new CNvPicPrXform(),
};
}

get tag() {
return 'xdr:nvPicPr';
}

render(xmlStream, model) {
const xform = new StaticXform({
tag: this.tag,
c: [
{
tag: 'xdr:cNvPr',
$: {id: model.index, name: `Picture ${model.index}`},
c: [
{
tag: 'a:extLst',
c: [
{
tag: 'a:ext',
$: {uri: '{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}'},
c: [
{
tag: 'a16:creationId',
$: {
'xmlns:a16': 'http://schemas.microsoft.com/office/drawing/2014/main',
id: '{00000000-0008-0000-0000-000002000000}',
},
},
],
},
],
},
],
},
{
tag: 'xdr:cNvPicPr',
c: [{tag: 'a:picLocks', $: {noChangeAspect: '1'}}],
},
],
});
xform.render(xmlStream);
xmlStream.openNode(this.tag);
this.map['xdr:cNvPr'].render(xmlStream, model);
this.map['xdr:cNvPicPr'].render(xmlStream, model);
xmlStream.closeNode();
}

parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}

switch (node.name) {
case this.tag:
this.reset();
break;
default:
this.parser = this.map[node.name];
if (this.parser) {
this.parser.parseOpen(node);
}
break;
}
return true;
}

parseText() {}

parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
this.parser = undefined;
}
return true;
}
switch (name) {
case this.tag:
this.model = this.map['xdr:cNvPr'].model;
return false;
default:
return true;
}
}
}

Expand Down
Loading