INDIRECT 返回由文本字符串指定的引用。此函数立即对引用进行计算,并显示其内容。如果需要更改公式中对单元格的引用,而不更改公式本身。
INDIRECT 支持 A1 样式的引用、R1C1 样式的引用、定义为引用的名称或对作为文本字符串的单元格的引用。如果文本字符串不是合法的单元格引用,则 INDIRECT 返回错误值。
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet>
</gc-worksheet>
<gc-worksheet>
</gc-worksheet>
</gc-spread-sheets>
<div class="options-container">
<div class="option-row">
<label class="colorLabel">在下方输入自定义名称并设置其引用内容。然后点击“添加自定义名称”按钮将其添加到 Spread 中,以便在单元格中使用。</label>
</div>
<div class="option-row">
<label class="sizedLabel">自定义名称:</label>
<input type="text" value="name1" id="customName" />
</div>
<div class="option-row">
<label class="sizedLabel">引用位置:</label>
<input type="text" value="$A$1" id="customReference" />
</div>
<div class="option-row">
<input type="button" id="btnAddCustomName" value="添加自定义名称" title="为示例添加自定义名称"
@click="addIndirectCustomName($event)" />
</div>
</div>
</div>
</template>
<script setup>
import '@grapecity-software/spread-sheets-vue';
import { ref } from "vue";
import GC from '@grapecity-software/spread-sheets';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
const spreadRef = ref(null);
let initSpread = function (spread) {
spreadRef.value = spread;
spread.suspendPaint();
let sheet = spread.sheets[0], sheet2 = spread.sheets[1];
sheet.setArray(0, 0, [["b1", 1], ["A", 2], ["B", 3]]);
sheet2.setArray(0, 0, [["b3", 1], ["A", 3], ["B", 5]]);
sheet.setValue(3, 0, "随机数");
sheet.setFormula(3, 1, "=RANDBETWEEN(1,100)");
sheet.setValue(4, 0, "当前时间");
sheet.setFormula(4, 1, "=NOW()");
sheet.setArray(8, 0, [["公式", "说明", "结果"]]);
sheet.setColumnWidth(0, 165);
sheet.setColumnWidth(1, 285);
let sampleData = [
['=INDIRECT("A1")', 'A1单元格的值 => "b1"'],
['=INDIRECT(A1)', 'A1引用的值 => b1的值 => 1'],
['=INDIRECT("A"&(1+2))', 'A3单元格的值 => "B"'],
['=INDIRECT(A3&B2)', 'B2单元格的值 => 2'],
['=INDIRECT("Sheet2!"&A1)', 'Sheet2表中B1单元格的值'],
['=INDIRECT("Sheet2!A1")', 'Sheet2表中A1单元格的值']
];
for (let i = 0, len = sampleData.length; i < len; i++) {
let data = sampleData[i], row = 6 + i;
sheet.setArray(row, 0, [data]);
sheet.setFormula(row, 2, data[0]);
}
spread.resumePaint();
}
let addIndirectCustomName = function (e) {
let name = document.getElementById("customName").value,
ref = document.getElementById("customReference").value,
row = 15;
if (name) {
try {
let sheet = spreadRef.value.sheets[0];
sheet.addCustomName(name, ref, 0, 0);
sheet.setArray(row, 0, [[`=INDIRECT(${name})`, `${name} 是一个自定义名称,如果它定义了有效的单元格引用,则使用该值,否则返回 #REF!`]]);
sheet.setFormula(row, 2, '=INDIRECT(' + name + ')');
} catch (e) {
alert("无效的自定义名称");
}
}
}
</script>
<style scoped>
#app {
height: 100%;
}
.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;
}
.option-row {
margin-bottom: 12px;
}
input {
width: 100%;
padding: 4px 6px;
margin-bottom: 6px;
box-sizing: border-box;
}
input[type=button] {
width: auto;
}
label {
display: block;
margin-bottom: 6px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
</style>
<!DOCTYPE html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SpreadJS VUE</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/vue3/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/vue3/node_modules/systemjs/dist/system.src.js"></script>
<script src="./systemjs.config.js"></script>
<script src="https://cdn.grapecity.com.cn/online/resources/compiler-sfc.esm-browser.js" type="module" defer></script>
<script>
var System = SystemJS;
System.import("./src/app.js");
System.import('$DEMOROOT$/zh/lib/vue3/license.js');
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
(function (global) {
SystemJS.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
paths: {
// paths serve as alias
'npm:': 'node_modules/',
'cdn:': 'https://cdn.grapecity.com.cn/SpreadJS/package-contents/18.2.0/'
},
packageConfigPaths: [
'./node_modules/*/package.json',
"./node_modules/@grapecity-software/*/package.json",
"./node_modules/@babel/*/package.json",
"./node_modules/@vue/*/package.json"
],
map: {
'vue': "npm:vue/dist/vue.esm-browser.js",
'tiny-emitter': 'npm:tiny-emitter/index.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
"systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js",
'@grapecity-software/spread-sheets': 'cdn:spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'cdn:spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-vue': 'cdn:spread-sheets-vue/index.js'
},
meta: {
'*.css': { loader: 'systemjs-plugin-css' },
'*.vue': { loader: "../plugin-vue/index.js" }
}
});
})(this);