書式の自動設定

SpreadJS は数式結果に対して書式の自動設定をサポートしています。 数式を入力すると、SpreadJS は数式を読み取り、数式の結果に適した書式を取得して、自動的にセルに設定します。

デモはリアルタイムにコードを編集できるよう動的にコンパイルされています。 機能に素早くアクセスするには「JavaScript」タブに切り替えて、よりスムーズな体験をお楽しみください! :)
説明
app.vue
index.html

書式の自動設定には 3 つのステップがあります。

書式の取得

  • 参照されたセルから書式を取得します
  • パーセント値からパーセント形式を取得します
  • 特別な関数から書式を取得します。例:NOW/TODAY/IRR/NPV
  • 動的参照から書式を取得します。 例:=INDIRECT(“C3”)

書式の決定

以下は、いくつかの書式の決定方法のリストです。

  • 日付形式から日付形式を引いた値は空形式となります
  • 日付形式と時刻形式を足したものは日付時刻形式となります
  • 通貨形式と数値形式を乗算したものは通貨形式となります
  • 数値形式にパーセント形式を乗算したものは数値形式となります
  • 空の形式にパーセント形式を乗算したものは空の形式になります
  • SUM/MAX/MIN/AVEDEV/FLOOR/ROUND/ROUNDUP/ROUNDDOWN/INT/TRUNC関数は、第一引数の最初のセルの書式を使用します

書式の適用

セルに数式を入力すると、そのセルに明示的な書式設定がない場合、SpreadJSは上記のステップに基づいて決定した書式を適用します。


spread.options.formulaFormatHint オプションを使用することで、数式結果に基づいた書式の自動設定を有効にするかどうかを制御することができます。

    var spread = GC.Spread.Sheets.findControl(document.getElementById('ss'));
    spread.options.formulaFormatHint = false;
書式の自動設定には 3 つのステップがあります。 書式の取得 参照されたセルから書式を取得します パーセント値からパーセント形式を取得します 特別な関数から書式を取得します。例:NOW/TODAY/IRR/NPV 動的参照から書式を取得します。 例:=INDIRECT(“C3”) 書式の決定 以下は、いくつかの書式の決定方法のリストです。 日付形式から日付形式を引いた値は空形式となります 日付形式と時刻形式を足したものは日付時刻形式となります 通貨形式と数値形式を乗算したものは通貨形式となります 数値形式にパーセント形式を乗算したものは数値形式となります 空の形式にパーセント形式を乗算したものは空の形式になります SUM/MAX/MIN/AVEDEV/FLOOR/ROUND/ROUNDUP/ROUNDDOWN/INT/TRUNC関数は、第一引数の最初のセルの書式を使用します 書式の適用 セルに数式を入力すると、そのセルに明示的な書式設定がない場合、SpreadJSは上記のステップに基づいて決定した書式を適用します。 spread.options.formulaFormatHint オプションを使用することで、数式結果に基づいた書式の自動設定を有効にするかどうかを制御することができます。
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <gc-worksheet></gc-worksheet> </gc-spread-sheets> <div class="options-container"> <div class="options-row"> <p>C8:C15のセルでは、数式入力後の自動書式設定を有効にしています。</p> </div> <div class="options-row"> <input type="checkbox" id="formulaFormatHint" v-model="formulaFormatHint" /> <label for="formulaFormatHint">書式の自動設定を有効にする</label> </div> </div> </div> </template> <script setup> import { ref, watch } from "vue"; import "@mescius/spread-sheets-vue"; import GC from '@mescius/spread-sheets'; import '@mescius/spread-sheets-resources-ja'; GC.Spread.Common.CultureManager.culture("ja-jp"); const spreadRef = ref(null); const formulaFormatHint = ref(true); watch(formulaFormatHint, (value) => { let spread = spreadRef.value; formulaFormatHint.value = value; spread.options.formulaFormatHint = value; }); let initSpread = function (spread) { spreadRef.value = spread; let sheet = spread.getActiveSheet(); sheet.suspendPaint(); sheet.setColumnWidth(0, 40); sheet.setColumnWidth(1, 300); sheet.setColumnWidth(2, 150); sheet.setValue(0, 0, 'Preset:'); sheet.setValue(1, 1, 'Init a scientific format'); sheet.setFormatter(1, 2, "0.00E+00"); sheet.setValue(1, 2, 1100); sheet.setValue(0, 0, 'Preset:'); sheet.setValue(2, 1, 'Init a currency format'); sheet.setFormatter(2, 2, "$#,##0;[Red]($#,##0)"); sheet.setValue(2, 2, 1100); sheet.setValue(3, 1, 'Init a date format'); sheet.setFormatter(3, 2, "M/d/yyyy"); sheet.setValue(3, 2, new Date(2023, 2, 23)); sheet.setValue(4, 1, 'Init a number format'); sheet.setFormatter(4, 2, "[red][DBNum1][$-411]0"); sheet.setValue(4, 2, 123); sheet.setValue(6, 0, 'Format Hints:'); sheet.setValue(7, 1, 'Format hints from reference'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 7, col: 2, newValue: "=C3" }); sheet.setValue(8, 1, 'Format hints from functions'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 8, col: 2, newValue: "=TIME(12,30,0)" }); sheet.setValue(9, 1, 'Add operate of date format and time format'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 9, col: 2, newValue: "=C4+C9" }); sheet.setValue(10, 1, 'Number format plus a number'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 10, col: 2, newValue: "=C5+31" }); sheet.setValue(11, 1, 'The currency format in multiplication'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 11, col: 2, newValue: "=C3*95%" }); sheet.setValue(12, 1, 'Format hints from INDIRECT result'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 12, col: 2, newValue: '=INDIRECT("C4")+1' }); sheet.setValue(13, 1, 'Currency format multiplied by scientific format'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 13, col: 2, newValue: '=C2*C3' }); sheet.setValue(14, 1, 'Percent format multiplied by scientific format'); spread.commandManager().execute({ cmd: "editCell", // Use the command to set formula will have the format hints sheetName: "Sheet1", row: 14, col: 2, newValue: '=C2*10%' }); sheet.resumePaint(); } </script> <style scoped> #app { height: 100%; } .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; } .options-row { font-size: 14px; padding: 5px; margin-top: 10px; } input { display: inline-block; } input[type="text"] { width: 200px; } input[type="button"] { padding: 4px 6px; width: 100%; margin-bottom: 10px; } label { display: inline-block; font-size: 13px; } p { padding: 2px 10px; background-color: #F4F8EB; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .desc { padding: 2px 10px; background-color: #F4F8EB; } .loginBox { position: absolute; left: 35%; top: 30%; background-color: white; padding: 20px; border: 1px solid #c6c6c6; box-shadow: rgba(0, 0, 0, 0.4) 1px 2px 5px; z-index: 2000; height: 100px; width: 200px; } .loginBoxLabel { display: inline-block; width: 200px; text-align: center; } </style>
<!DOCTYPE html> <html lang="en" style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>SpreadJS VUE</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/vue3/node_modules/systemjs/dist/system.src.js"></script> <script src="./systemjs.config.js"></script> <script src="./compiler.js" type="module"></script> <script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script> <script> var System = SystemJS; System.import("./src/app.js"); System.import('$DEMOROOT$/ja/lib/vue3/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
(function (global) { SystemJS.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, packageConfigPaths: [ './node_modules/*/package.json', "./node_modules/@mescius/*/package.json", "./node_modules/@babel/*/package.json", "./node_modules/@vue/*/package.json" ], map: { 'vue': "npm:vue/dist/vue.esm-browser.js", 'tiny-emitter': 'npm:tiny-emitter/index.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-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js' }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);