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

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

このサンプルはVueを使用しています。

<template> <div class="container-fluid"> <!-- the flexsheet --> <wj-flex-sheet :initialized="initializeFlexSheet"> <wj-sheet name="数値" :rowCount="20" :columnCount="8"></wj-sheet> <wj-sheet name="日付" :rowCount="20" :columnCount="8"></wj-sheet> </wj-flex-sheet> <wj-color-picker style="display:none;position:fixed;z-index:100" :initialized="colorPickerInit"> </wj-color-picker> <div class="well well-lg"> <wj-menu :header="'書式'" :value="format" :itemClicked="formatChanged"> <wj-menu-item :value="'0'">10進数形式</wj-menu-item> <wj-menu-item :value="'n2'">数値形式</wj-menu-item> <wj-menu-item :value="'p'">パーセンテージ形式</wj-menu-item> <wj-menu-item :value="'c2'">通貨形式</wj-menu-item> <wj-menu-separator></wj-menu-separator> <wj-menu-item :value="'d'">短い日付</wj-menu-item> <wj-menu-item :value="'D'">長い日付</wj-menu-item> <wj-menu-item :value="'f'">完全な日時(短い時刻)</wj-menu-item> <wj-menu-item :value="'F'">完全な日時(長い時刻)</wj-menu-item> </wj-menu> <div>フォント: <wj-combo-box style="width:120px" :itemsSource="fonts" :selectedIndex="fontIdx" displayMemberPath="name" selectedValuePath="value" :isEditable="false" :selectedIndexChanged="fontChanged"> </wj-combo-box> <wj-combo-box style="width:80px" :itemsSource="fontSizeList" :selectedIndex="fontSizeIdx" displayMemberPath="name" selectedValuePath="value" :isEditable="false" :selectedIndexChanged="fontSizeChanged"> </wj-combo-box> <div class="btn-group"> <button type="button" v-bind:class="['btn btn-default', { active: isBold }]" @click="applyBoldStyle()"> 太字</button> <button type="button" v-bind:class="['btn btn-default', { active: isItalic }]" @click="applyItalicStyle()"> 斜体</button> <button type="button" v-bind:class="['btn btn-default', { active: isUnderline }]" @click="applyUnderlineStyle()"> 下線</button> </div> </div> <div>色: <div class="btn-group"> <button type="button" class="btn btn-default" @click="showColorPicker($event, false)"> フォントの色</button> <button type="button" class="btn btn-default" @click="showColorPicker($event, true)"> 塗りつぶしの色</button> </div>配置: <div class="btn-group"> <button type="button" v-bind:class="['btn btn-default', { active: textAlign == 'left' }]" @click="applyCellTextAlign('left')"> 左揃え</button> <button type="button" v-bind:class="['btn btn-default', { active: textAlign == 'center' }]" @click="applyCellTextAlign('center')"> 中央揃え</button> <button type="button" v-bind:class="['btn btn-default', { active: textAlign == 'right' }]" @click="applyCellTextAlign('right')"> 右揃え</button> </div> </div> </div> </div> </template> <script setup> import { ref } from "vue"; const fonts = ref([ { 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" }, ]); const fontSizeList = ref([ { 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" }, ]); const format = ref("0"); const fontIdx = ref(0); const fontSizeIdx = ref(5); const isBold = ref(false); const isItalic = ref(false); const isUnderline = ref(false); const textAlign = ref("left"); const _updatingSelection = ref(false); const _applyFillColor = ref(false); const flex = ref({}); const colorPicker = ref({}); function initializeFlexSheet(flexref) { flexref.deferUpdate(() => { for (let sheetIdx = 0; sheetIdx < flexref.sheets.length; sheetIdx++) { flexref.selectedSheetIndex = sheetIdx; let sheetName = flexref.selectedSheet.name; for (let colIdx = 0; colIdx < flexref.columns.length; colIdx++) { for (let rowIdx = 0; rowIdx < flexref.rows.length; rowIdx++) { if (sheetName === "数値") { flexref.setCellData(rowIdx, colIdx, colIdx + rowIdx); } else { let date = new Date(2015, colIdx, rowIdx + 1); flexref.setCellData(rowIdx, colIdx, date); } } } } flexref.selectedSheetIndex = 0; setTimeout(() => _updateSelection(flex.value, flexref.selection), 100); }); flexref.selectionChanged.addHandler((sender, args) => { _updateSelection(flex.value, args.range); }); flex.value = flexref; } function fontChanged(sender) { if (sender.selectedItem && !_updatingSelection.value) { flex.value.applyCellsStyle({ fontFamily: sender.selectedItem.value }); } } function fontSizeChanged(sender) { if (sender.selectedItem && !_updatingSelection.value) { flex.value.applyCellsStyle({ fontSize: sender.selectedItem.value }); } } function colorPickerInit(colorPickerref) { // if the browser is firefox, we should bind the blur event. (TFS #124387) // if the browser is IE, we should bind the focusout event. (TFS #124500) let blurEvt = /firefox/i.test(window.navigator.userAgent) ? "blur" : "focusout"; // Hide the color picker control when it lost the focus. colorPickerref.hostElement.addEventListener(blurEvt, () => { setTimeout(() => { if (!colorPickerref.containsFocus()) { _applyFillColor.value = false; colorPickerref.hostElement.style.display = "none"; } }, 0); }); // Initialize the value changed event handler for the color picker control. colorPickerref.valueChanged.addHandler(() => { if (_applyFillColor.value) { flex.value.applyCellsStyle({ backgroundColor: colorPickerref.value }); } else { flex.value.applyCellsStyle({ color: colorPickerref.value }); } }); colorPicker.value = colorPickerref; } function formatChanged(sender) { if (sender.selectedIndex >= 0) { flex.value.applyCellsStyle({ format: sender.selectedValue }); } } // apply the text alignment for the selected cells function applyCellTextAlign(textAlignref) { textAlign.value = textAlignref; flex.value.applyCellsStyle({ textAlign: textAlign }); } // apply the bold font weight for the selected cells function applyBoldStyle() { flex.value.applyCellsStyle({ fontWeight: isBold.value ? "none" : "bold" }); isBold.value = !isBold.value; } // apply the underline text decoration for the selected cells function applyUnderlineStyle() { flex.value.applyCellsStyle({ textDecoration: isUnderline.value ? "none" : "underline" }); isUnderline.value = !isUnderline.value; } // apply the italic font style for the selected cells function applyItalicStyle() { flex.value.applyCellsStyle({ fontStyle: isItalic.value ? "none" : "italic" }); isItalic.value = !isItalic.value; } // show the color picker control. function showColorPicker(e, isFillColor) { let offset = _cumulativeOffset(e.target), he = colorPicker.value.hostElement; he.style.display = "inline"; he.style.left = offset.left + "px"; he.style.top = offset.top - he.clientHeight - 5 + "px"; he.focus(); _applyFillColor.value = isFillColor; } // Update the selection object of the scope. function _updateSelection(flexSheet, sel) { let row = flexSheet.rows[sel.row], rCnt = flexSheet.rows.length, cCnt = flexSheet.columns.length, fontIndex = 0, fontSizeIndex = 5; _updatingSelection.value = 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 = null; if (cellStyle) { fontIndex = _checkFontfamily(cellStyle.fontFamily); fontSizeIndex = _checkFontSize(cellStyle.fontSize); cellFormat = cellStyle.format; } let fmt; if (!!cellFormat) { fmt = cellFormat; } else { if (wijmo.isInt(cellContent)) { fmt = "0"; } else if (wijmo.isNumber(cellContent)) { fmt = "n2"; } else if (wijmo.isDate(cellContent)) { fmt = "d"; } } fontIdx.value = fontIndex; fontSizeIdx.value = fontSizeIndex; format.value = fmt; let state = flexSheet.getSelectionFormatState(); isBold.value = state.isBold; isItalic.value = state.isItalic; isUnderline.value = state.isUnderline; textAlign.value = state.textAlign; } _updatingSelection.value = false; } // check font family for the font name combobox of the ribbon. function _checkFontfamily(fontFamily) { let fontList = fonts.value; if (!fontFamily) { return 0; } for (let fontIndex = 0; fontIndex < fontList.length; fontIndex++) { let font = fontList[fontIndex]; if (font.name === fontFamily || font.value === fontFamily) { return fontIndex; } } return 0; } // check font size for the font size combobox of the ribbon. function _checkFontSize(fontSize) { let sizeList = fontSizeList.value; if (fontSize == null) { return 5; } for (let index = 0; index < sizeList.length; index++) { let size = sizeList[index]; if (size.value === fontSize || size.name === fontSize) { 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, }; } </script> <style> .container-fluid .wj-flexsheet { height: 400px; margin: 6px 0; } </style>
<!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="compiler.js" type="module"></script> <script src="node_modules/jszip/dist/jszip.js"></script> <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.js'); </script> </head> <body> <div id="app"> </div> </body> </html>
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' }, '*.vue': { loader: '../plugin-vue/index.js' } //'*.vue': { loader: 'systemjs-plugin-vue' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@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.style': 'npm:@mescius/wijmo.grid.style/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', "@mescius/wijmo.vue2.chart.analytics": "npm:@mescius/wijmo.vue2.chart.analytics/index.js", "@mescius/wijmo.vue2.chart.animation": "npm:@mescius/wijmo.vue2.chart.animation/index.js", "@mescius/wijmo.vue2.chart.annotation": "npm:@mescius/wijmo.vue2.chart.annotation/index.js", "@mescius/wijmo.vue2.chart.finance.analytics": "npm:@mescius/wijmo.vue2.chart.finance.analytics/index.js", "@mescius/wijmo.vue2.chart.finance": "npm:@mescius/wijmo.vue2.chart.finance/index.js", "@mescius/wijmo.vue2.chart.hierarchical": "npm:@mescius/wijmo.vue2.chart.hierarchical/index.js", "@mescius/wijmo.vue2.chart.interaction": "npm:@mescius/wijmo.vue2.chart.interaction/index.js", "@mescius/wijmo.vue2.chart.radar": "npm:@mescius/wijmo.vue2.chart.radar/index.js", '@mescius/wijmo.vue2.chart.map': 'npm:@mescius/wijmo.vue2.chart.map/index.js', "@mescius/wijmo.vue2.chart": "npm:@mescius/wijmo.vue2.chart/index.js", "@mescius/wijmo.vue2.core": "npm:@mescius/wijmo.vue2.core/index.js", "@mescius/wijmo.vue2.gauge": "npm:@mescius/wijmo.vue2.gauge/index.js", "@mescius/wijmo.vue2.grid.detail": "npm:@mescius/wijmo.vue2.grid.detail/index.js", "@mescius/wijmo.vue2.grid.filter": "npm:@mescius/wijmo.vue2.grid.filter/index.js", "@mescius/wijmo.vue2.grid.grouppanel": "npm:@mescius/wijmo.vue2.grid.grouppanel/index.js", '@mescius/wijmo.vue2.grid.search': 'npm:@mescius/wijmo.vue2.grid.search/index.js', "@mescius/wijmo.vue2.grid.multirow": "npm:@mescius/wijmo.vue2.grid.multirow/index.js", "@mescius/wijmo.vue2.grid.sheet": "npm:@mescius/wijmo.vue2.grid.sheet/index.js", '@mescius/wijmo.vue2.grid.transposed': 'npm:@mescius/wijmo.vue2.grid.transposed/index.js', '@mescius/wijmo.vue2.grid.transposedmultirow': 'npm:@mescius/wijmo.vue2.grid.transposedmultirow/index.js', "@mescius/wijmo.vue2.grid": "npm:@mescius/wijmo.vue2.grid/index.js", "@mescius/wijmo.vue2.input": "npm:@mescius/wijmo.vue2.input/index.js", "@mescius/wijmo.vue2.olap": "npm:@mescius/wijmo.vue2.olap/index.js", "@mescius/wijmo.vue2.viewer": "npm:@mescius/wijmo.vue2.viewer/index.js", "@mescius/wijmo.vue2.nav": "npm:@mescius/wijmo.vue2.nav/index.js", "@mescius/wijmo.vue2.base": "npm:@mescius/wijmo.vue2.base/index.js", '@mescius/wijmo.vue2.barcode.common': 'npm:@mescius/wijmo.vue2.barcode.common/index.js', '@mescius/wijmo.vue2.barcode.composite': 'npm:@mescius/wijmo.vue2.barcode.composite/index.js', '@mescius/wijmo.vue2.barcode.specialized': 'npm:@mescius/wijmo.vue2.barcode.specialized/index.js', 'bootstrap.css': 'npm:bootstrap/dist/css/bootstrap.min.css', 'jszip': 'npm:jszip/dist/jszip.js', 'css': 'npm:systemjs-plugin-css/css.js', 'vue': 'npm:vue/dist/vue.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', '@vue/compiler-dom':'npm:@vue/compiler-dom/dist/compiler-dom.global.prod.js', '@vue/runtime-dom':'npm:@vue/runtime-dom/dist/runtime-dom.global.prod.js', '@vue/shared':'npm:@vue/shared/dist/shared.cjs.js', 'vue-loader': 'npm:systemjs-vue-browser/index.js', //'systemjs-plugin-vue': 'npm:systemjs-plugin-vue/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' }, wijmo: { defaultExtension: 'js', } } }); })(this);