为了快速访问另一个文件或网页上的相关信息,你可以在单元格中插入超链接。
你可以通过setHyperlink方法来设置超链接,例如:
同时,你可以使用getHyperlink方法来获取超链接,例如:
HyperLink包含如下属性:
属性
数据类型
描述
url
字符串
表示超链接指向的位置。SpreadJS的超链接支持许多协议,例如:http / https / ftp / file / mailto...。此外,还支持以“ sjs://”开头的url,它引用工作表位置。
tooltip
字符串
表示超链接的提示消息,当鼠标悬停在带有超链接的单元格上时显示。
linkColor
字符串
表示访问链接之前的前景色。默认值为#0066cc。
visitedLinkColor
字符串
表示访问链接后的前景色。默认值为#3399ff。
target
枚举
表示用户打开超链接文档的方式。默认值为0 / *空白* /。
drawUnderline
布尔
表示是否绘制超链接的下划线。默认为true。
command
字符串 | 函数
表示单击超链接而执行的行为
如果你输入一个链接到单元格中,我们支持为你自动创建超链接。
在spread选项中有一个allowAutoCreateHyperlink选项。
默认情况下,我们支持自动创建超链接。
如果你不喜欢这个, 你可以设置 allowAutoCreateHyperlink为false.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import './styles.css';
import { AppFunc } from './app-func';
import { App } from './app-class';
// 1. Functional Component sample
ReactDOM.render(<AppFunc />, document.getElementById('app'));
// 2. Class Component sample
// ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import GC from '@grapecity-software/spread-sheets';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import { SpreadSheets, Worksheet, Column } from '@grapecity-software/spread-sheets-react';
import './styles.css';
const Component = React.Component;
const GCsheets = GC.Spread.Sheets;
export function AppFunc() {
let spread = null;
let sheet = null;
let autoGenerateColumns = true;
let initSpread = function (value) {
spread = value;
spread.suspendPaint();
sheet = spread.getActiveSheet();
sheet.setText(1, 1, 'SpreadJS Demo Link');
sheet.setText(2, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForUrl = {};
hyperlinkForUrl.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
sheet.setHyperlink(2, 1, hyperlinkForUrl);
sheet.setText(5, 1, 'Link with tooltip');
sheet.setText(6, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForTooltip = {};
hyperlinkForTooltip.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkForTooltip.tooltip = 'GrapeCity SpreadJS Demos';
sheet.setHyperlink(6, 1, hyperlinkForTooltip);
sheet.setText(9, 1, 'Change link foreColor');
sheet.setText(10, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForForeColor = {};
hyperlinkForForeColor.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkForForeColor.tooltip = 'GrapeCity SpreadJS Demos';
hyperlinkForForeColor.linkColor = 'red';
sheet.setHyperlink(10, 1, hyperlinkForForeColor);
sheet.setText(13, 1, 'Change link visited color');
sheet.setText(14, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForVisitedColor = {};
hyperlinkForVisitedColor.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkForVisitedColor.tooltip = 'GrapeCity SpreadJS Demos';
hyperlinkForVisitedColor.visitedLinkColor = 'green';
sheet.setHyperlink(14, 1, hyperlinkForVisitedColor);
sheet.setText(17, 1, 'Link without underline');
sheet.setText(18, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkWithoutUnderline = {};
hyperlinkWithoutUnderline.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkWithoutUnderline.tooltip = 'GrapeCity SpreadJS Demos';
hyperlinkWithoutUnderline.drawUnderline = false;
sheet.setHyperlink(18, 1, hyperlinkWithoutUnderline);
spread.resumePaint();
}
let setHyperlinkButtonClicked = function (e) {
spread.suspendPaint();
let hyperlink = {};
hyperlink.url = document.getElementById('hyperlinkURL').value;
hyperlink.tooltip = document.getElementById('tooltip').value;
hyperlink.linkColor = document.getElementById('linkColor').value;
if (hyperlink.linkColor === "") {
hyperlink.linkColor = undefined;
}
hyperlink.visitedLinkColor = document.getElementById('visitedLinkColor').value;
if (hyperlink.visitedLinkColor === "") {
hyperlink.visitedLinkColor = undefined;
}
hyperlink.target = document.getElementById('hyperlinkTarget').value;
hyperlink.drawUnderline = document.getElementById('drawUnderline').checked;
let row = sheet.getActiveRowIndex();
let col = sheet.getActiveColumnIndex();
sheet.setHyperlink(row, col, hyperlink);
sheet.setValue(row, col, hyperlink.tooltip);
spread.resumePaint();
}
let allowAutoCreateHyperlink = function (e) {
spread.suspendPaint();
spread.options.allowAutoCreateHyperlink = e.target.checked;
spread.resumePaint();
}
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => initSpread(spread)}>
<Worksheet autoGenerateColumns={autoGenerateColumns}>
</Worksheet>
</SpreadSheets>
</div>
<Panel
allowAutoCreateHyperlink={(e) => { allowAutoCreateHyperlink(e) }}
setHyperlinkButtonClicked={(e) => { setHyperlinkButtonClicked(e) }}
/>
</div>
);
}
class Panel extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div class="options-container">
<strong class="sp-demo-HeaderTitle">Settings:</strong>
<p id="settingString">Set the hyperlink data for a cell:</p>
<fieldset>
<div id="settingsDiv">
<div class="option-row">
<label for="hyperlinkURL">URL:</label>
<input type="text" id="hyperlinkURL" placeholder="https://www.grapecity.com/" />
</div>
<div class="option-row">
<label for="tooltip">Tooltip:</label>
<input type="text" id="tooltip" placeholder="GrapeCity" />
</div>
<div class="option-row">
<label for="linkColor">Link Color:</label>
<input type="text" id="linkColor" placeholder="#0066cc" />
</div>
<div class="option-row">
<label for="visitedLinkColor">Visited Link Color:</label>
<input type="text" id="visitedLinkColor" placeholder="#3399ff" />
</div>
<div class="option-row">
<label for="hyperlinkTarget">Target:</label>
<select id="hyperlinkTarget"
name="hyperlinkTarget">
<option value="0">blank</option>
<option value="1">self</option>
<option value="2">parent</option>
<option value="3">top</option>
</select>
</div>
<div class="option-row">
<label for="drawUnderline">Draw Underline:</label>
<input type="checkbox" id="drawUnderline" />
</div>
<div class="option-row">
<button id="setHyperlinkButton" onClick={(e) => { this.props.setHyperlinkButtonClicked(e) }}>Set Hyperlink</button>
</div>
</div>
</fieldset>
<div id="allowAutoCreateHyperlinkDiv">
<div class="option-row">
<label for="allowAutoCreateHyperlink">Allow auto create hyperlink:</label>
<input id="allowAutoCreateHyperlink" type="checkbox" onChange={(e) => { this.props.allowAutoCreateHyperlink(e) }} />
</div>
</div>
</div>
)
}
}
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import GC from '@grapecity-software/spread-sheets';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import { SpreadSheets, Worksheet, Column } from '@grapecity-software/spread-sheets-react';
import './styles.css';
const Component = React.Component;
const GCsheets = GC.Spread.Sheets;
export class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.autoGenerateColumns = true;
}
render() {
return(
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread=>this.initSpread(spread)}>
<Worksheet autoGenerateColumns={this.autoGenerateColumns}>
</Worksheet>
</SpreadSheets>
</div>
<Panel
allowAutoCreateHyperlink={(e)=>{this.allowAutoCreateHyperlink(e)}}
setHyperlinkButtonClicked={(e)=>{this.setHyperlinkButtonClicked(e)}}
/>
</div>
);
}
initSpread(spread) {
this.spread = spread;
spread.suspendPaint();
let sheet = spread.getActiveSheet();
this.sheet = sheet;
sheet.setText(1, 1, 'SpreadJS Demo Link');
sheet.setText(2, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForUrl = {};
hyperlinkForUrl.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
sheet.setHyperlink(2, 1, hyperlinkForUrl);
sheet.setText(5, 1, 'Link with tooltip');
sheet.setText(6, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForTooltip = {};
hyperlinkForTooltip.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkForTooltip.tooltip = 'GrapeCity SpreadJS Demos';
sheet.setHyperlink(6, 1, hyperlinkForTooltip);
sheet.setText(9, 1, 'Change link foreColor');
sheet.setText(10, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForForeColor = {};
hyperlinkForForeColor.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkForForeColor.tooltip = 'GrapeCity SpreadJS Demos';
hyperlinkForForeColor.linkColor = 'red';
sheet.setHyperlink(10, 1, hyperlinkForForeColor);
sheet.setText(13, 1, 'Change link visited color');
sheet.setText(14, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkForVisitedColor = {};
hyperlinkForVisitedColor.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkForVisitedColor.tooltip = 'GrapeCity SpreadJS Demos';
hyperlinkForVisitedColor.visitedLinkColor = 'green';
sheet.setHyperlink(14, 1, hyperlinkForVisitedColor);
sheet.setText(17, 1, 'Link without underline');
sheet.setText(18, 1, 'https://www.grapecity.com.cn/developer/spreadjs/demo');
let hyperlinkWithoutUnderline = {};
hyperlinkWithoutUnderline.url = 'https://www.grapecity.com.cn/developer/spreadjs/demo';
hyperlinkWithoutUnderline.tooltip = 'GrapeCity SpreadJS Demos';
hyperlinkWithoutUnderline.drawUnderline = false;
sheet.setHyperlink(18, 1, hyperlinkWithoutUnderline);
spread.resumePaint();
}
setHyperlinkButtonClicked(e) {
this.spread.suspendPaint();
let hyperlink = {};
let sheet = this.sheet;
hyperlink.url = document.getElementById('hyperlinkURL').value;
hyperlink.tooltip = document.getElementById('tooltip').value;
hyperlink.linkColor = document.getElementById('linkColor').value;
if (hyperlink.linkColor === "") {
hyperlink.linkColor = undefined;
}
hyperlink.visitedLinkColor = document.getElementById('visitedLinkColor').value;
if (hyperlink.visitedLinkColor === "") {
hyperlink.visitedLinkColor = undefined;
}
hyperlink.target = document.getElementById('hyperlinkTarget').value;
hyperlink.drawUnderline = document.getElementById('drawUnderline').checked;
let row = sheet.getActiveRowIndex();
let col = sheet.getActiveColumnIndex();
sheet.setHyperlink(row, col, hyperlink);
sheet.setValue(row, col, hyperlink.tooltip);
this.spread.resumePaint();
}
allowAutoCreateHyperlink(e) {
this.spread.suspendPaint();
this.spread.options.allowAutoCreateHyperlink = e.target.checked;
this.spread.resumePaint();
}
componentDidMount() {
let allowAutoCreateHyperlinkElement = document.getElementById('allowAutoCreateHyperlink');
allowAutoCreateHyperlinkElement.checked = true;
}
}
class Panel extends Component{
constructor(props){
super(props);
}
render(){
return(
<div class="options-container">
<strong class="sp-demo-HeaderTitle">Settings:</strong>
<p id="settingString">Set the hyperlink data for a cell:</p>
<fieldset>
<div id="settingsDiv">
<div class="option-row">
<label for="hyperlinkURL">URL:</label>
<input type="text" id="hyperlinkURL" placeholder="https://www.grapecity.com/" />
</div>
<div class="option-row">
<label for="tooltip">Tooltip:</label>
<input type="text" id="tooltip" placeholder="GrapeCity" />
</div>
<div class="option-row">
<label for="linkColor">Link Color:</label>
<input type="text" id="linkColor" placeholder="#0066cc" />
</div>
<div class="option-row">
<label for="visitedLinkColor">Visited Link Color:</label>
<input type="text" id="visitedLinkColor" placeholder="#3399ff" />
</div>
<div class="option-row">
<label for="hyperlinkTarget">Target:</label>
<select id="hyperlinkTarget"
name="hyperlinkTarget">
<option value="0">blank</option>
<option value="1">self</option>
<option value="2">parent</option>
<option value="3">top</option>
</select>
</div>
<div class="option-row">
<label for="drawUnderline">Draw Underline:</label>
<input type="checkbox" id="drawUnderline" />
</div>
<div class="option-row">
<button id="setHyperlinkButton" onClick={(e)=>{this.props.setHyperlinkButtonClicked(e)}}>Set Hyperlink</button>
</div>
</div>
</fieldset>
<div id="allowAutoCreateHyperlinkDiv">
<div class="option-row">
<label for="allowAutoCreateHyperlink">Allow auto create hyperlink:</label>
<input id="allowAutoCreateHyperlink" type="checkbox" onChange={(e)=>{this.props.allowAutoCreateHyperlink(e)}} />
</div>
</div>
</div>
)
}
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/react/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/zh/lib/react/license.js').then(function() {
System.import('./src/app');
});
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.options-container legend {
text-align: center;
}
.option-row {
font-size: 14px;
padding: 5px;
}
input {
display:block;
width: 100%;
margin: 8px 0;
box-sizing: border-box;
}
label, input {
padding: 4px 6px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#app {
height: 100%;
}
#drawUnderline {
display: inline-block;
width: 30px;
}
#drawUnderlineLabel {
display: inline-block;
}
#allowAutoCreateHyperlink {
display: inline-block;
width: 30px;
}
#setHyperlinkButton {
font-weight: bold;
background-color: #ecf3ff;
width: 200px;
height: 35px;
border-radius: 4px;
border-color: #0b93d5;
border-width: thin;
}
#settingsDiv {
margin-top: "10px"
}
#settingString {
padding:"2px 10px
}
#allowAutoCreateHyperlinkDiv {
margin-top: "10px"
}
(function(global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true,
react: true
},
meta: {
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-react': 'npm:@grapecity-software/spread-sheets-react/index.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js',
'react': 'npm:react/umd/react.production.min.js',
'react-dom': 'npm:react-dom/umd/react-dom.production.min.js',
'css': 'npm:systemjs-plugin-css/css.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'jsx'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);