スプレッドシート:セル書式

FlexSheetでは、applyCellsStyleメソッドを使用して各セルに書式を設定できます。書式には、セル値のデータ形式(日付/数値形式)、フォントスタイル、塗りつぶしの色、および水平方向の配置が含まれます。

import 'bootstrap.css'; import '@mescius/wijmo.styles/wijmo.css'; import './styles.css'; // import * as wijmo from '@mescius/wijmo'; import * as input from '@mescius/wijmo.input'; import * as sheet from '@mescius/wijmo.grid.sheet'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { let fonts = [ { name: 'Arial', value: 'Arial, Helvetica, sans-serif' }, { name: 'Arial Black', value: '"Arial Black", Gadget, sans-serif' }, { name: 'Comic Sans MS', value: '"Comic Sans MS", cursive, sans-serif' }, { name: 'Courier New', value: '"Courier New", Courier, monospace' }, { name: 'Georgia', value: 'Georgia, serif' }, { name: 'Impact', value: 'Impact, Charcoal, sans-serif' }, { name: 'Lucida Console', value: '"Lucida Console", Monaco, monospace' }, { name: 'Lucida Sans Unicode', value: '"Lucida Sans Unicode", "Lucida Grande", sans-serif' }, { name: 'Palatino Linotype', value: '"Palatino Linotype", "Book Antiqua", Palatino, serif' }, { name: 'Tahoma', value: 'Tahoma, Geneva, sans-serif' }, { name: 'Segoe UI', value: '"Segoe UI", "Roboto", sans-serif' }, { name: 'Times New Roman', value: '"Times New Roman", Times, serif' }, { name: 'Trebuchet MS', value: '"Trebuchet MS", Helvetica, sans-serif' }, { name: 'Verdana', value: 'Verdana, Geneva, sans-serif' } ]; // let fontSizeList = [ { name: '8', value: '8px' }, { name: '9', value: '9px' }, { name: '10', value: '10px' }, { name: '11', value: '11px' }, { name: '12', value: '12px' }, { name: '14', value: '14px' }, { name: '16', value: '16px' }, { name: '18', value: '18px' }, { name: '20', value: '20px' }, { name: '22', value: '22px' }, { name: '24', value: '24px' } ]; // let updatingSelection = false; let applyFillColor = false; let selectionFormatState = {}; let flexSheet = new sheet.FlexSheet('#formatSheet'); let inputColor = new input.ColorPicker('#inputColor'); // let formatMenu = new input.Menu('#formatMenu', { selectedIndex: 0, selectedIndexChanged: (sender) => { if (sender.selectedIndex >= 0) { updateMenuHeader(sender, '書式'); if (!updatingSelection) { flexSheet.applyCellsStyle({ format: sender.selectedValue }); } } } }); updateMenuHeader(formatMenu, 'Format'); // let cboFontName = new input.ComboBox('#fonts', { itemsSource: fonts, displayMemberPath: 'name', selectedValuePath: 'value', isEditable: false }); // let cboFontSize = new input.ComboBox('#fontSize', { itemsSource: fontSizeList, selectedIndex: 5, displayMemberPath: 'name', selectedValuePath: 'value', isEditable: false }); // flexSheet.addUnboundSheet('数値', 20, 8); flexSheet.addUnboundSheet('日付', 20, 8); flexSheet.deferUpdate(() => { for (let sheetIdx = flexSheet.sheets.length - 1; sheetIdx >= 0; sheetIdx--) { flexSheet.selectedSheetIndex = sheetIdx; let sheetName = flexSheet.selectedSheet.name; // for (let colIdx = 0; colIdx < flexSheet.columns.length; colIdx++) { for (let rowIdx = 0; rowIdx < flexSheet.rows.length; rowIdx++) { if (sheetName === '数値') { flexSheet.setCellData(rowIdx, colIdx, colIdx + rowIdx); } else { let date = new Date(2018, colIdx, rowIdx + 1); flexSheet.setCellData(rowIdx, colIdx, date); } } } } flexSheet.selectedSheetIndex = 0; setTimeout(() => updateSelection(flexSheet.selection), 100); }); // flexSheet.selectionChanged.addHandler((sender, args) => { updateSelection(args.range); }); // // Hide the color picker control when it lost the focus. let blurEvt = /firefox/i.test(window.navigator.userAgent) ? 'blur' : 'focusout'; inputColor.hostElement.addEventListener(blurEvt, () => { setTimeout(() => { if (!inputColor.containsFocus()) { applyFillColor = false; inputColor.hostElement.style.display = 'none'; } }, 0); }); // // Initialize the value changed event handler for the color picker control. inputColor.valueChanged.addHandler(() => { if (applyFillColor) { flexSheet.applyCellsStyle({ backgroundColor: inputColor.value }); } else { flexSheet.applyCellsStyle({ color: inputColor.value }); } }); // cboFontName.selectedIndexChanged.addHandler(() => { // apply the font family for the selected cells if (!updatingSelection) { flexSheet.applyCellsStyle({ fontFamily: cboFontName.selectedItem.value }); } }); // cboFontSize.selectedIndexChanged.addHandler(() => { // apply the font size for the selected cells if (!updatingSelection) { flexSheet.applyCellsStyle({ fontSize: cboFontSize.selectedItem.value }); } }); // onClick('applyCellTextLeftAlign', () => applyCellTextAlign('left')); onClick('applyCellTextCenterAlign', () => applyCellTextAlign('center')); onClick('applyCellTextRightAlign', () => applyCellTextAlign('right')); // onClick('applyBoldStyle', e => { flexSheet.applyCellsStyle({ fontWeight: selectionFormatState.isBold ? 'none' : 'bold' }); selectionFormatState.isBold = !selectionFormatState.isBold; // if (selectionFormatState.isBold) { wijmo.addClass(e.target, 'active'); } else { wijmo.removeClass(e.target, 'active'); } }); // onClick('applyUnderlineStyle', e => { flexSheet.applyCellsStyle({ textDecoration: selectionFormatState.isUnderline ? 'none' : 'underline' }); selectionFormatState.isUnderline = !selectionFormatState.isUnderline; // if (selectionFormatState.isUnderline) { wijmo.addClass(e.target, 'active'); } else { wijmo.removeClass(e.target, 'active'); } }); // onClick('applyItalicStyle', e => { flexSheet.applyCellsStyle({ fontStyle: selectionFormatState.isItalic ? 'none' : 'italic' }); selectionFormatState.isItalic = !selectionFormatState.isItalic; // if (selectionFormatState.isItalic) { wijmo.addClass(e.target, 'active'); } else { wijmo.removeClass(e.target, 'active'); } }); // onClick('foreColor', (e) => showColorPicker(e, false)); onClick('fillColor', (e) => showColorPicker(e, true)); // // apply the text alignment for the selected cells function applyCellTextAlign(textAlign) { flexSheet.applyCellsStyle({ textAlign: textAlign }); selectionFormatState.textAlign = textAlign; updateTextAlignState(textAlign); } // // show the color picker control. function showColorPicker(e, isFillColor) { let offset = cumulativeOffset(e.target), he = inputColor.hostElement; // he.style.display = 'inline'; he.style.left = offset.left + 'px'; he.style.top = (offset.top - he.clientHeight - 5) + 'px'; he.focus(); // applyFillColor = isFillColor; } // // Update the selection object of the scope. function updateSelection(sel) { let rCnt = flexSheet.rows.length, cCnt = flexSheet.columns.length; // updatingSelection = true; // if (sel.row > -1 && sel.col > -1 && rCnt > 0 && cCnt > 0 && sel.col < cCnt && sel.col2 < cCnt && sel.row < rCnt && sel.row2 < rCnt) { let cellContent = flexSheet.getCellData(sel.row, sel.col, false), cellStyle = flexSheet.selectedSheet.getCellStyle(sel.row, sel.col), cellFormat = ''; // if (cellStyle) { cboFontName.selectedIndex = checkFontfamily(cellStyle.fontFamily); cboFontSize.selectedIndex = checkFontSize(cellStyle.fontSize); cellFormat = cellStyle.format; } else { cboFontName.selectedIndex = 0; cboFontSize.selectedIndex = 5; } // if (!cellFormat) { if (wijmo.isInt(cellContent)) { cellFormat = '0'; } else if (wijmo.isNumber(cellContent)) { cellFormat = 'n2'; } else if (wijmo.isDate(cellContent)) { cellFormat = 'd'; } } // formatMenu.selectedValue = cellFormat; // selectionFormatState = flexSheet.getSelectionFormatState(); updateStyleButtonState(); } // updatingSelection = false; } // function updateStyleButtonState() { let boldBtn = document.querySelector('#applyBoldStyle'), underlineBtn = document.querySelector('#applyUnderlineStyle'), italicBtn = document.querySelector('#applyItalicStyle'); // if (selectionFormatState.isBold) { wijmo.addClass(boldBtn, 'active'); } else { wijmo.removeClass(boldBtn, 'active'); } // if (selectionFormatState.isUnderline) { wijmo.addClass(underlineBtn, 'active'); } else { wijmo.removeClass(underlineBtn, 'active'); } // if (selectionFormatState.isItalic) { wijmo.addClass(italicBtn, 'active'); } else { wijmo.removeClass(italicBtn, 'active'); } // updateTextAlignState(selectionFormatState.textAlign); } // function updateTextAlignState(textAlign) { let leftBtn = document.querySelector('#applyCellTextLeftAlign'), centerBtn = document.querySelector('#applyCellTextCenterAlign'), rightBtn = document.querySelector('#applyCellTextRightAlign'); // wijmo.removeClass(leftBtn, 'active'); wijmo.removeClass(centerBtn, 'active'); wijmo.removeClass(rightBtn, 'active'); // switch (textAlign) { case 'left': wijmo.addClass(leftBtn, 'active'); break; case 'center': wijmo.addClass(centerBtn, 'active'); break; case 'right': wijmo.addClass(rightBtn, 'active'); break; } } // // check font family for the font name combobox of the ribbon. function checkFontfamily(value) { if (!value) { return 0; } // for (let fontIndex = 0; fontIndex < fonts.length; fontIndex++) { let font = fonts[fontIndex]; if (font.name === value || font.value === value) { return fontIndex; } } // return 0; } // // check font size for the font size combobox of the ribbon. function checkFontSize(value) { if (value == null) { return 5; } // for (let index = 0; index < fontSizeList.length; index++) { let size = fontSizeList[index]; if (size.value === value || size.name === value) { return index; } } // return 5; } // // Get the absolute position of the dom element. function cumulativeOffset(element) { let top = 0, left = 0, scrollTop = 0, scrollLeft = 0; // do { top += element.offsetTop || 0; left += element.offsetLeft || 0; scrollTop += element.scrollTop || 0; scrollLeft += element.scrollLeft || 0; element = element.offsetParent; } while (element && !(element instanceof HTMLBodyElement)); // scrollTop += document.body.scrollTop || document.documentElement.scrollTop; scrollLeft += document.body.scrollLeft || document.documentElement.scrollLeft; // return { top: top - scrollTop, left: left - scrollLeft }; } // function onClick(id, fn) { document.querySelector('#' + id).addEventListener('click', fn); } // function updateMenuHeader(menu, header) { menu.header = header + ': <b>' + menu.text + '</b>'; } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>MESCIUS Wijmo FlexSheet Formatting Cells</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.21.5/system.src.js" integrity="sha512-skZbMyvYdNoZfLmiGn5ii6KmklM82rYX2uWctBhzaXPxJgiv4XBwJnFGr5k8s+6tE1pcR1nuTKghozJHyzMcoA==" crossorigin="anonymous"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <div id="formatSheet"></div> <div id="inputColor" style="display:none;position:fixed;z-index:100"></div> <div class="well well-lg"> <div> <select id="formatMenu"> <option value="0">10進数形式</option> <option value="n2">数値形式</option> <option value="p2">パーセンテージ形式</option> <option value="c2">通貨形式</option> <option></option> <option value="d">短い日付</option> <option value="D">長い日付</option> <option value="f">完全な日時(短い時刻)</option> <option value="F">完全な日時(長い時刻)</option> </select> </div> <div> フォント: <div id="fonts" style="width: 120px"></div> <div id="fontSize" style="width: 80px"></div> <div class="btn-group"> <button id="applyBoldStyle" type="button" class="btn btn-default">太字</button> <button id="applyItalicStyle" type="button" class="btn btn-default">斜体</button> <button id="applyUnderlineStyle" type="button" class="btn btn-default">下線</button> </div> </div> <div> 色: <div class="btn-group"> <button id="foreColor" type="button" class="btn btn-default">フォントの色</button> <button id="fillColor" type="button" class="btn btn-default">塗りつぶしの色</button> </div> 配置: <div class="btn-group"> <button id="applyCellTextLeftAlign" type="button" class="btn btn-default">左揃え</button> <button id="applyCellTextCenterAlign" type="button" class="btn btn-default">中央揃え</button> <button id="applyCellTextRightAlign" type="button" class="btn btn-default">右揃え</button> </div> </div> </div> </div> </body> </html>
.wj-flexsheet { height: 400px; margin: 6px 0; }
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'jszip': 'npm:jszip/dist/jszip.js', '@mescius/wijmo': 'npm:@mescius/wijmo/index.js', '@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js', '@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles', '@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures', '@mescius/wijmo.chart': 'npm:@mescius/wijmo.chart/index.js', '@mescius/wijmo.chart.analytics': 'npm:@mescius/wijmo.chart.analytics/index.js', '@mescius/wijmo.chart.animation': 'npm:@mescius/wijmo.chart.animation/index.js', '@mescius/wijmo.chart.annotation': 'npm:@mescius/wijmo.chart.annotation/index.js', '@mescius/wijmo.chart.finance': 'npm:@mescius/wijmo.chart.finance/index.js', '@mescius/wijmo.chart.finance.analytics': 'npm:@mescius/wijmo.chart.finance.analytics/index.js', '@mescius/wijmo.chart.hierarchical': 'npm:@mescius/wijmo.chart.hierarchical/index.js', '@mescius/wijmo.chart.interaction': 'npm:@mescius/wijmo.chart.interaction/index.js', '@mescius/wijmo.chart.radar': 'npm:@mescius/wijmo.chart.radar/index.js', '@mescius/wijmo.chart.render': 'npm:@mescius/wijmo.chart.render/index.js', '@mescius/wijmo.chart.webgl': 'npm:@mescius/wijmo.chart.webgl/index.js', '@mescius/wijmo.chart.map': 'npm:@mescius/wijmo.chart.map/index.js', '@mescius/wijmo.gauge': 'npm:@mescius/wijmo.gauge/index.js', '@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js', '@mescius/wijmo.grid.detail': 'npm:@mescius/wijmo.grid.detail/index.js', '@mescius/wijmo.grid.filter': 'npm:@mescius/wijmo.grid.filter/index.js', '@mescius/wijmo.grid.search': 'npm:@mescius/wijmo.grid.search/index.js', '@mescius/wijmo.grid.grouppanel': 'npm:@mescius/wijmo.grid.grouppanel/index.js', '@mescius/wijmo.grid.multirow': 'npm:@mescius/wijmo.grid.multirow/index.js', '@mescius/wijmo.grid.transposed': 'npm:@mescius/wijmo.grid.transposed/index.js', '@mescius/wijmo.grid.transposedmultirow': 'npm:@mescius/wijmo.grid.transposedmultirow/index.js', '@mescius/wijmo.grid.pdf': 'npm:@mescius/wijmo.grid.pdf/index.js', '@mescius/wijmo.grid.sheet': 'npm:@mescius/wijmo.grid.sheet/index.js', '@mescius/wijmo.grid.xlsx': 'npm:@mescius/wijmo.grid.xlsx/index.js', '@mescius/wijmo.grid.selector': 'npm:@mescius/wijmo.grid.selector/index.js', '@mescius/wijmo.grid.cellmaker': 'npm:@mescius/wijmo.grid.cellmaker/index.js', '@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js', '@mescius/wijmo.odata': 'npm:@mescius/wijmo.odata/index.js', '@mescius/wijmo.olap': 'npm:@mescius/wijmo.olap/index.js', '@mescius/wijmo.rest': 'npm:@mescius/wijmo.rest/index.js', '@mescius/wijmo.pdf': 'npm:@mescius/wijmo.pdf/index.js', '@mescius/wijmo.pdf.security': 'npm:@mescius/wijmo.pdf.security/index.js', '@mescius/wijmo.viewer': 'npm:@mescius/wijmo.viewer/index.js', '@mescius/wijmo.xlsx': 'npm:@mescius/wijmo.xlsx/index.js', '@mescius/wijmo.undo': 'npm:@mescius/wijmo.undo/index.js', '@mescius/wijmo.interop.grid': 'npm:@mescius/wijmo.interop.grid/index.js', '@mescius/wijmo.touch': 'npm:@mescius/wijmo.touch/index.js', '@mescius/wijmo.cloud': 'npm:@mescius/wijmo.cloud/index.js', '@mescius/wijmo.barcode': 'npm:@mescius/wijmo.barcode/index.js', '@mescius/wijmo.barcode.common': 'npm:@mescius/wijmo.barcode.common/index.js', '@mescius/wijmo.barcode.composite': 'npm:@mescius/wijmo.barcode.composite/index.js', '@mescius/wijmo.barcode.specialized': 'npm:@mescius/wijmo.barcode.specialized/index.js', 'jszip': 'npm:jszip/dist/jszip.js', 'bootstrap.css': 'npm:bootstrap/dist/css/bootstrap.min.css', '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' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, } }); })(this);