概要と基本的な使い方

SpreadJSでは、Excelの名前の概念がサポートされます。これにより、セル範囲、関数、定数、またはテーブルに対して任意の名前を定義できます。

ワークブックまたはワークシートに名前を追加するには、addCustomNameメソッドを使用します。 ワークブックまたはワークシートから指定の名前を削除するには、removeCustomNameメソッドを使用します。 ワークブックまたはワークシートからすべての名前を削除するには、clearCustomNamesメソッドを使用します。 カスタム名を表すNameInfoクラスには、ワークブックまたはワークシートに属する名前、式、行、列、およびコメントプロパティが含まれます。
window.onload = initFunction; function initFunction() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 1}); spread.suspendPaint(); var sheet = spread.getActiveSheet(); 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)"); printNames(sheet); document.getElementById('setNameInfo').onclick = function () { var sheet = spread.getActiveSheet(); var name = document.getElementById('setname').value; if (name === '') { alert("name should not be empty"); return; } var comment = document.getElementById('setComment').value; var formula = GC.Spread.Sheets.CalcEngine.rangeToFormula(sheet.getSelections()[0], 0, 0, GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allAbsolute); sheet.addCustomName(name, formula, sheet.getActiveRowIndex(), sheet.getActiveColumnIndex(), comment); printNames(sheet); }; function printNames(sheet) { var namesInfo = sheet.getCustomNames(); var str = "name\t\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; } sheet.bind(GC.Spread.Sheets.Events.CellClick, function (sender, args) { var sheet = args.sheet; var selection = sheet.getSelections()[0]; document.getElementById('setname').value = ''; document.getElementById('setComment').value = ''; 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(); document.getElementById('setname').value = name; document.getElementById('setComment').value = comment; break; } } }); spread.resumePaint(); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="ja-jp" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <strong class="sp-demo-HeaderTitle">設定:</strong> <P style="padding:2px 10px">範囲を選択して、カスタム名を追加します。</P> <div id="settingsDiv" style="margin-top: 10px"> <div class="option-row"> <label for="setName">名前:</label> <input type="text" id="setname" placeholder="名前は空欄にできません"/> </div> <div class="option-row"> <label for="setComment">コメント:</label> <input type="text" id="setComment"/> </div> <div class="option-row"> <label for="reference">参照先:</label> <input type="text" id="reference"/> </div> <div class="option-row"> <input type="button" id="setNameInfo" value="カスタム名を追加" style="width:125px; margin-left: 10px"/> </div> <div class="option-row"> <label for="reference">すべてのカスタム名:</label> <textarea rows="5", cols="30" id="allNameInfo"></textarea> </div> </div> </div> </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; } label, input { padding: 4px 6px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }