概要と基本的な使い方

データをフィルタリングすると、指定した条件に適合する行だけを表示したり、表示させたくない行を非表示にしたりできます。この機能は、大量のデータが含まれているシートにおいて、特定の条件を満たすデータだけに注目したい場合に便利です。

<p>シートにフィルタを設定するには、まず行フィルタを作成します。次に、<strong>rowFilter</strong>メソッドを使用して、この行フィルタを設定します。</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> range = <span class="hljs-keyword">new</span> GC.Spread.Sheets.Range(<span class="hljs-number">-1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">2</span>); <span class="hljs-keyword">var</span> rowFilter = <span class="hljs-keyword">new</span> GC.Spread.Sheets.Filter.HideRowFilter(range); sheet.rowFilter(rowFilter); rowFilter.filterButtonVisible(<span class="hljs-literal">false</span>); <span class="hljs-keyword">var</span> condition = <span class="hljs-keyword">new</span> GC.Spread.Sheets.ConditionalFormatting.Condition(GC.Spread.Sheets.ConditionalFormatting.ConditionType.textCondition, {<span class="hljs-attr">compareType</span>: GC.Spread.Sheets.ConditionalFormatting.TextCompareType.contains,<span class="hljs-attr">expected</span>: <span class="hljs-string">'*e*'</span>}); rowFilter.addFilterItem(<span class="hljs-number">1</span>, condition); rowFilter.filter(<span class="hljs-number">1</span>); </code></pre> <p>条件を作成してから、<strong>addFilterItem</strong>メソッドを使用して、既存の行フィルタに指定のフィルタを追加することもできます。次に、例を示します。</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> formulaCondition = <span class="hljs-keyword">new</span> GC.Spread.Sheets.ConditionalFormatting.Condition(GC.Spread.Sheets.ConditionalFormatting.ConditionType.formulaCondition, {<span class="hljs-attr">customValueType</span>: GC.Spread.Sheets.ConditionalFormatting.CustomValueType.empty,<span class="hljs-attr">formula</span>: <span class="hljs-literal">null</span>}); rowFilter.addFilterItem(<span class="hljs-number">1</span>, formulaCondition); rowFilter.filter(<span class="hljs-number">1</span>); </code></pre>
var spreadNS = GC.Spread.Sheets; window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); initSpread(spread); }; function initSpread(spread) { var sheet = spread.getSheet(0); sheet.suspendPaint(); sheet.setRowCount(50); sheet.setColumnCount(8); var rc = sheet.getRowCount(); var cc = sheet.getColumnCount(); for (var r = 0; r < rc; r++) { for (var c = 0; c < cc; c++) { if (c == 0) { sheet.setValue(r, c, "Value is Number"); } else { sheet.setValue(r, c, r + c); } } } sheet.setValue(0, 0, "Conditions", spreadNS.SheetArea.colHeader); sheet.setValue(0, 1, "Cell Value", spreadNS.SheetArea.colHeader); sheet.setValue(1, 0, "Text contains e"); sheet.setValue(10, 0, "Text doesn't contains e"); sheet.setValue(21, 0, "Text contains e"); sheet.setValue(1, 1, "begin"); sheet.setValue(10, 1, "during"); sheet.setValue(21, 1, "end"); sheet.setValue(2, 0, "Background is Cyan"); sheet.setValue(6, 0, "Background is Purple"); sheet.setValue(12, 0, "Background is Cyan"); sheet.getCell(2, 1).backColor("Cyan"); sheet.getCell(6, 1).backColor("Purple"); sheet.getCell(12, 1).backColor("Cyan"); sheet.setValue(3, 0, "Value is Thursday"); sheet.setValue(8, 0, "Value is Friday"); sheet.setValue(14, 0, "Value is Thursday"); sheet.setValue(3, 1, new Date(2011, 5, 30)); sheet.setValue(8, 1, new Date(2011, 6, 1)); sheet.setValue(14, 1, new Date(2011, 5, 30)); sheet.setValue(4, 0, "Value is null"); sheet.setValue(9, 0, "Value is null"); sheet.setValue(18, 0, "Value is null"); sheet.setValue(4, 1, null); sheet.setValue(9, 1, null); sheet.setValue(18, 1, null); sheet.setColumnWidth(0, 150); sheet.setColumnWidth(1, 150); var filter = new spreadNS.Filter.HideRowFilter(new spreadNS.Range(-1, 0, -1, 2)); sheet.rowFilter(filter); filter.filterButtonVisible(false); sheet.resumePaint(); _getElementById("text_conditon").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var filter = sheet.rowFilter(); if (filter) { filter.removeFilterItems(1); if (this.checked) { var condition = new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.textCondition, { compareType: spreadNS.ConditionalFormatting.TextCompareType.contains, expected: "*e*" }); filter.addFilterItem(1, condition); } filter.filter(1); sheet.invalidateLayout(); sheet.repaint(); } }); _getElementById("number_conditon").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var filter = sheet.rowFilter(); if (filter) { filter.removeFilterItems(1); if (this.checked) { var condition = new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.numberCondition, { compareType: spreadNS.ConditionalFormatting.GeneralComparisonOperators.lessThan, expected: 20 }); filter.addFilterItem(1, condition); } filter.filter(1); sheet.invalidateLayout(); sheet.repaint(); } }); _getElementById("date_conditon").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var filter = sheet.rowFilter(); if (filter) { filter.removeFilterItems(1); if (this.checked) { var dateExCondition = spreadNS.ConditionalFormatting.Condition.fromWeek(4); filter.addFilterItem(1, dateExCondition); } filter.filter(1); sheet.invalidateLayout(); sheet.repaint(); } }); _getElementById("style_conditon").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var filter = sheet.rowFilter(); if (filter) { filter.removeFilterItems(1); if (this.checked) { var condition = new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.colorCondition, { compareType: spreadNS.ConditionalFormatting.ColorCompareType.backgroundColor, expected: "Cyan" }); filter.addFilterItem(1, condition); } filter.filter(1); sheet.invalidateLayout(); sheet.repaint(); } }); _getElementById("custom_conditon").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var filter = sheet.rowFilter(); if (filter) { filter.removeFilterItems(1); if (this.checked) { var formulaCondition = new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.formulaCondition, { customValueType: spreadNS.ConditionalFormatting.CustomValueType.empty, formula: null }); filter.addFilterItem(1, formulaCondition); } filter.filter(1); sheet.invalidateLayout(); sheet.repaint(); } }); } function _getElementById(id){ return document.getElementById(id); }
<!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="$DEMOROOT$/spread/source/data/outlineColumn.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"> <label >以下の条件付き書式ルールは、列2に適用されます。</label> <hr> <div class="option-row"> <input type="checkbox" id="text_conditon" /> <label for="text_conditon" style="display: inline-block; width: 210px;">テキストの条件(「e」を含む)</label> </div> <div class="option-row"> <input type="checkbox" id="number_conditon" /> <label for="number_conditon">数値の条件(20未満)</label> </div> <div class="option-row"> <input type="checkbox" id="date_conditon" /> <label for="date_conditon" id="label_date_conditon" style="display: inline-block; width: 210px;">日付の条件(木曜)</label> </div> <div class="option-row"> <input type="checkbox" id="style_conditon" /> <label for="style_conditon">スタイルの条件(背景色がシアン)</label> </div> <div class="option-row"> <input type="checkbox" id="custom_conditon" /> <label for="custom_conditon" id="label_custom_conditon">カスタマイズの条件(値がない)</label> </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; margin-top: 3px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }