Skip to content

fix issue 1676 #1701

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 13 commits into from
Nov 21, 2021
29 changes: 24 additions & 5 deletions lib/xlsx/xform/sheet/hyperlink-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ class HyperlinkXform extends BaseXform {
}

render(xmlStream, model) {
xmlStream.leafNode('hyperlink', {
ref: model.address,
'r:id': model.rId,
tooltip: model.tooltip,
});
if (this.isInternalLink(model)) {
xmlStream.leafNode('hyperlink', {
ref: model.address,
'r:id': model.rId,
tooltip: model.tooltip,
location: model.target,
});
} else {
xmlStream.leafNode('hyperlink', {
ref: model.address,
'r:id': model.rId,
tooltip: model.tooltip,
});
}
}

parseOpen(node) {
Expand All @@ -20,6 +29,11 @@ class HyperlinkXform extends BaseXform {
rId: node.attributes['r:id'],
tooltip: node.attributes.tooltip,
};

// This is an internal link
if (node.attributes.location) {
this.model.target = node.attributes.location;
}
return true;
}
return false;
Expand All @@ -30,6 +44,11 @@ class HyperlinkXform extends BaseXform {
parseClose() {
return false;
}

isInternalLink(model) {
// @example: Sheet2!D3, return true
return model.target && /^[^!]+![a-zA-Z]+[\d]+$/.test(model.target);
}
}

module.exports = HyperlinkXform;
12 changes: 12 additions & 0 deletions spec/unit/xlsx/xform/sheet/hyperlink-xform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ const expectations = [
xml: '<hyperlink ref="B6" r:id="rId1"/>',
tests: ['render', 'renderIn', 'parse'],
},
{
title: 'Internal Link',
create() {
return new HyperlinkXform();
},
preparedModel: {address: 'B6', rId: 'rId1', target: 'sheet1!B2'},
get parsedModel() {
return this.preparedModel;
},
xml: '<hyperlink ref="B6" r:id="rId1" location="sheet1!B2"/>',
tests: ['render', 'renderIn', 'parse'],
},
];

describe('HyperlinkXform', () => {
Expand Down