Overview

SpreadJS supports the name concept from Excel, in which you can define a name for a cell range, function, constant, or table.

Description
app.component.ts
index.html
app.component.html
styles.css
Copy to CodeMine

Use the addCustomName method to add a name to a workbook or worksheet:

    sheet.addCustomName('customName1','=SUM(A1,C3)', 1, 1, 'comment', false);

Use the removeCustomName method to remove a specified name from a workbook or worksheet:

    sheet.removeCustomName('customName1');

Use the clearCustomNames method to remove all names from a workbook or worksheet:

    sheet.clearCustomNames();

The custom NameInfo class includes the name, expression, row, column, comment and isReadOnly properties, of a workbook or worksheet:

    var nameInfo = sheet.getCustomName('customName1');
    // to get custom name
    var name = nameInfo.getName()
    // to get custom comment
    var comment = nameInfo.getComment();
    // to get custom readonly status
    var isReadOnly = nameInfo.isReadOnly();
    // to set custom readonly status
    nameInfo.isReadOnly(true);
    // to get custom expression
    var expression = nameInfo.getExpression();
    // to get expression string
    var expStr = GC.Spread.Sheets.CalcEngine.expressionToFormula(sheet, expression, 0, 0);

SpreadJS supports updating the custom NameInfo. Includes:

  • Renaming custom names and updating all referenced formulas
  • Changing the formula of the custom name
  • Updating comments of the custom name
spread.addCustomName("aaa","$A$1", 0, 0);
spread.sheets[0].setFormula(1, 1, "=aaa+1");
spread.getCustomName("aaa").set("bbb", "$A$2", 0, 0, "the configure");
Use the addCustomName method to add a name to a workbook or worksheet: Use the removeCustomName method to remove a specified name from a workbook or worksheet: Use the clearCustomNames method to remove all names from a workbook or worksheet: The custom NameInfo class includes the name, expression, row, column, comment and isReadOnly properties, of a workbook or worksheet: SpreadJS supports updating the custom NameInfo. Includes: Renaming custom names and updating all referenced formulas Changing the formula of the custom name Updating comments of the custom name
import { Component, NgModule, enableProdMode } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { SpreadSheetsModule } from '@mescius/spread-sheets-angular'; import GC from '@mescius/spread-sheets'; import './styles.css'; function setValueById(id: string, value: string) { (document.getElementById(id) as HTMLInputElement).value = value; } function getValueById(id: string) { return (document.getElementById(id) as HTMLInputElement).value; } function setCheckedById(id: string, value: boolean) { (document.getElementById(id) as HTMLInputElement).checked = value; } function getCheckedById(id: string) { return (document.getElementById(id) as HTMLInputElement).checked; } @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { spread: GC.Spread.Sheets.Workbook; hostStyle = { width: 'calc(100% - 280px)', height: '100%', overflow: 'hidden', float: 'left' }; constructor() { } initSpread($event: any) { this.spread = $event.spread; let spread = this.spread; spread.suspendPaint(); let sheet = spread.sheets[0]; 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(); } printNames(sheet: GC.Spread.Sheets.Worksheet) { let namesInfo = sheet.getCustomNames(); let str = "name\t\treference\n"; namesInfo.forEach(function (nameInfo) { let name = nameInfo.getName(); let reference = GC.Spread.Sheets.CalcEngine.rangeToFormula(nameInfo.getExpression().getRange()); str += name + " " + reference + '\n'; }); setValueById('allNameInfo', str); } onAddCustomName($event: any) { let sheet = this.spread.sheets[0]; let name = getValueById('setname'); if (name === '') { alert("name should not be empty"); return; } let comment = getValueById('setComment'); let isReadOnly = getCheckedById('setIsReadOnly'); let 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); } onCellClick(sender: any, args: any) { let sheet = args.sheet; let selection = sheet.getSelections()[0]; setValueById('setname', ''); setValueById('setComment', ''); setCheckedById('setIsReadOnly', false); setValueById('reference', GC.Spread.Sheets.CalcEngine.rangeToFormula(selection)); let customNames = sheet.getCustomNames(); for (let i = 0; i < customNames.length; i++) { let customName = customNames[i]; let range = customName.getExpression().getRange(); if (range.row === selection.row && range.col === selection.col && range.rowCount === selection.rowCount && range.colCount === selection.colCount) { let name = customName.getName(); let comment = customName.getComment(); let isReadOnly = customName.isReadOnly(); setValueById('setname', name); setValueById('setComment', comment); setCheckedById('setIsReadOnly', !!isReadOnly); break; } } } } @NgModule({ imports: [BrowserModule, SpreadSheetsModule], declarations: [AppComponent], exports: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {} enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule);
<!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$/en/angular/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- Polyfills --> <script src="$DEMOROOT$/en/angular/node_modules/core-js/client/shim.min.js"></script> <script src="$DEMOROOT$/en/angular/node_modules/zone.js/fesm2015/zone.min.js"></script> <!-- SystemJS --> <script src="$DEMOROOT$/en/angular/node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.import('@angular/compiler'); System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('$DEMOROOT$/en/lib/angular/license.ts'); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html>
<div class="sample-tutorial"> <gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)"> <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>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .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: 'ts', typescriptOptions: { tsconfig: true }, meta: { 'typescript': { "exports": "ts" }, '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'core-js': 'npm:core-js/client/shim.min.js', 'zone': 'npm:zone.js/fesm2015/zone.min.js', 'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js', '@angular/core': 'npm:@angular/core/fesm2022', '@angular/common': 'npm:@angular/common/fesm2022/common.mjs', '@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs', '@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs', '@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs', '@angular/router': 'npm:@angular/router/fesm2022/router.mjs', '@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs', 'jszip': 'npm:jszip/dist/jszip.min.js', 'typescript': 'npm:typescript/lib/typescript.js', 'ts': './plugin.js', 'tslib':'npm:tslib/tslib.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', '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-angular': 'npm:@mescius/spread-sheets-angular/fesm2020/mescius-spread-sheets-angular.mjs', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'ts' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, "node_modules/@angular": { defaultExtension: 'mjs' }, "@mescius/spread-sheets-angular": { defaultExtension: 'mjs' }, '@angular/core': { defaultExtension: 'mjs', main: 'core.mjs' } } }); })(this);