基本应用

Spread.Sheets支持类似Excel的命名概念, 你可以给一个单元格,区域,函数,常量,表格定义一个命名信息。

用 addCustomName 方法去给工作簿或者工作表增加一个新的命名信息。 用 removeCustomName 方法去给工作簿或者工作表移除一个特定的命名信息。 用 clearCustomNames 方法去移除工作簿或者工作表的所有的名称。 一个普通的命名信息包含名称、描述、行、列、备注和只读属性,用 getCustomName 方法通过一个名称去获取一个指定的命名信息。
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <gc-worksheet> </gc-worksheet> </gc-spread-sheets> <div class="options-container"> <strong class="sp-demo-HeaderTitle">Settings:</strong> <p style="padding:2px 10px">Select a range to add custom name.</p> <div id="settingsDiv" style="margin-top: 10px"> <div class="option-row"> <label for="setName">Name:</label> <input type="text" id="setname" placeholder="Name should not be empty" /> </div> <div class="option-row"> <label for="setComment">Comment:</label> <input type="text" id="setComment" /> </div> <div class="option-row"> <label for="setIsReadOnly">IsReadOnly:</label> <input type="checkbox" id="setIsReadOnly" /> </div> <div class="option-row"> <label for="reference">Reference:</label> <input type="text" id="reference" /> </div> <div class="option-row"> <input type="button" id="setNameInfo" value="Add Custom Name" style="width:125px; margin-left: 10px" @click="onAddCustomName($event)" /> </div> <div class="option-row"> <label for="reference">All CustomNames:</label> <textarea rows="5" cols="30" id="allNameInfo"></textarea> </div> </div> </div> </div> </template> <script> import Vue from "vue"; import '@grapecity-software/spread-sheets-resources-zh'; GC.Spread.Common.CultureManager.culture("zh-cn"); import '@grapecity-software/spread-sheets-vue' import GC from '@grapecity-software/spread-sheets'; import './styles.css'; let App = Vue.extend({ name: "app", methods: { initSpread: function (spread) { this.spread = spread; var sheet = spread.getSheet(0); spread.suspendPaint(); sheet.setColumnWidth(1, 100); sheet.setValue(2, 1, "Sample scope:"); sheet.setValue(2, 2, 1); sheet.setValue(2, 3, 2); sheet.setValue(3, 2, 3); sheet.setValue(3, 3, 4); // add custom name sheet.addCustomName("customName1", "=$B$3", 0, 0, "this is a customName!"); sheet.setFormula(5, 1, "=customName1"); sheet.addCustomName("customName2", "=$C$3:$D$4", 0, 0, "this is another customName!"); sheet.setFormula(5, 2, "=sum(customName2)"); this.printNames(sheet); sheet.bind(GC.Spread.Sheets.Events.CellClick, this.onCellClick); spread.resumePaint(); }, onAddCustomName(e) { var sheet = this.spread.sheets[0]; var name = document.getElementById('setname').value; if (name === '') { alert("name should not be empty"); return; } var comment = document.getElementById('setComment').value; var isReadOnly = document.getElementById('setIsReadOnly').checked; var formula = GC.Spread.Sheets.CalcEngine.rangeToFormula(sheet.getSelections()[0], 0, 0, GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allAbsolute); sheet.addCustomName(name, formula, 0, 0, comment, isReadOnly); this.printNames(sheet); }, printNames(sheet) { var namesInfo = sheet.getCustomNames(); var str = "name\t\treference\n"; namesInfo.forEach(function (nameInfo) { var name = nameInfo.getName(); var reference = GC.Spread.Sheets.CalcEngine.rangeToFormula(nameInfo.getExpression().getRange()); str += name + " " + reference + '\n'; }); document.getElementById('allNameInfo').value = str; }, onCellClick(sender, args) { var sheet = args.sheet; var selection = sheet.getSelections()[0]; document.getElementById('setname').value = ''; document.getElementById('setComment').value = ''; document.getElementById('setIsReadOnly').checked = false; document.getElementById('reference').value = GC.Spread.Sheets.CalcEngine.rangeToFormula(selection); var customNames = sheet.getCustomNames(); for (var i = 0; i < customNames.length; i++) { var customName = customNames[i]; var range = customName.getExpression().getRange(); if (range.row === selection.row && range.col === selection.col && range.rowCount === selection.rowCount && range.colCount === selection.colCount) { var name = customName.getName(); var comment = customName.getComment(); var isReadOnly = customName.isReadOnly(); document.getElementById('setname').value = name; document.getElementById('setComment').value = comment; document.getElementById('setIsReadOnly').checked = !!isReadOnly; break; } } } } }); new Vue({ render: h => h(App) }).$mount('#app'); </script>
<!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/vue/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- SystemJS --> <script src="$DEMOROOT$/zh/vue/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); System.import('$DEMOROOT$/zh/lib/vue/license.js'); </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; } .option-row { font-size: 14px; padding: 5px; } input { display:block; width: 100%; margin: 8px 0; box-sizing: border-box; } input[type=checkbox] { display: inline-block; width: initial; margin: 0; vertical-align: middle; } label, input { padding: 4px 6px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
(function(global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' }, '*.vue': { loader: 'vue-loader' } }, 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-vue': 'npm:@grapecity-software/spread-sheets-vue/index.js', '@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js', 'jszip': 'npm:jszip/dist/jszip.js', 'css': 'npm:systemjs-plugin-css/css.js', 'vue': 'npm:vue/dist/vue.min.js', 'vue-loader': 'npm:systemjs-vue-browser/index.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' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' } } }); })(this);