書式の自動設定には 3 つのステップがあります。
書式の取得
参照されたセルから書式を取得します
パーセント値からパーセント形式を取得します
特別な関数から書式を取得します。例:NOW/TODAY/IRR/NPV
動的参照から書式を取得します。 例:=INDIRECT(“C3”)
書式の決定
以下は、いくつかの書式の決定方法のリストです。
日付形式から日付形式を引いた値は空形式となります
日付形式と時刻形式を足したものは日付時刻形式となります
通貨形式と数値形式を乗算したものは通貨形式となります
数値形式にパーセント形式を乗算したものは数値形式となります
空の形式にパーセント形式を乗算したものは空の形式になります
SUM/MAX/MIN/AVEDEV/FLOOR/ROUND/ROUNDUP/ROUNDDOWN/INT/TRUNC関数は、第一引数の最初のセルの書式を使用します
書式の適用
セルに数式を入力すると、そのセルに明示的な書式設定がない場合、SpreadJSは上記のステップに基づいて決定した書式を適用します。
spread.options.formulaFormatHint オプションを使用することで、数式結果に基づいた書式の自動設定を有効にするかどうかを制御することができます。
import { Component, NgModule, enableProdMode } from '@angular/core';
import { bootstrapApplication, BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { SpreadSheetsModule } from '@mescius/spread-sheets-angular';
import GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets-resources-ja';
GC.Spread.Common.CultureManager.culture("ja-jp");
import './styles.css';
const spreadNS = GC.Spread.Sheets, SheetArea = spreadNS.SheetArea;
@Component({
standalone: true,
imports: [SpreadSheetsModule, BrowserModule, FormsModule],
selector: 'app-component',
templateUrl: 'src/app.component.html'
})
export class AppComponent {
spread: GC.Spread.Sheets.Workbook;
hostStyle = {
width: 'calc(100% - 280px)',
height: '100%',
overflow: 'hidden',
float: 'left'
};
constructor() {
}
initSpread($event: any) {
let spread = this.spread = $event.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();
}
formulaFormatHint($event: any) {
let spread = this.spread, formulaFormatHint = $event.target.checked;
spread.options.formulaFormatHint = formulaFormatHint;
}
}
enableProdMode();
bootstrapApplication(AppComponent);
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/angular/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script>
<!-- Polyfills -->
<script src="$DEMOROOT$/ja/angular/node_modules/core-js/client/shim.min.js"></script>
<script src="$DEMOROOT$/ja/angular/node_modules/zone.js/fesm2015/zone.min.js"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/ja/angular/node_modules/systemjs/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
// workaround to load 'rxjs/operators' from the rxjs bundle
System.import('rxjs').then(function (m) {
System.import('@angular/compiler');
System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators));
System.import('$DEMOROOT$/ja/lib/angular/license.ts');
System.import('./src/app.component');
});
</script>
</head>
<body>
<app-component></app-component>
</body>
</html>
<div class="sample-tutorial">
<gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)">
<gc-worksheet>
</gc-worksheet>
</gc-spread-sheets>
<div class="options-container">
<div class="option-row">
<p>C8:C15のセルでは、数式入力後の自動書式設定を有効にしています。</p>
</div>
<div class="option-row">
<input type = "checkbox" id="formulaFormatHint" checked (change)="formulaFormatHint($event)"/>
<label for="formulaFormatHint">書式の自動設定を有効にする</label>
</div>
</div>
</div>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
margin-top: 10px;
}
input {
display: inline-block;
}
input[type="text"] {
width: 200px;
}
label {
margin-bottom: 6px;
}
p{
padding:2px 10px;
background-color:#F4F8EB;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(function (global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
},
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'core-js': 'npm:core-js/client/shim.min.js',
'zone': 'npm:zone.js/fesm2015/zone.min.js',
'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js',
'@angular/core': 'npm:@angular/core/fesm2022',
'@angular/common': 'npm:@angular/common/fesm2022/common.mjs',
'@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs',
'@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs',
'@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs',
'@angular/router': 'npm:@angular/router/fesm2022/router.mjs',
'@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs',
'jszip': 'npm:jszip/dist/jszip.min.js',
'typescript': 'npm:typescript/lib/typescript.js',
'ts': './plugin.js',
'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',
'tslib':'npm:tslib/tslib.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-angular': 'npm:@mescius/spread-sheets-angular/fesm2020/mescius-spread-sheets-angular.mjs',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'ts',
meta: {
"*.component.ts": {
loader: "system.component-loader.js"
}
}
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
"node_modules/@angular": {
defaultExtension: 'mjs'
},
"@mescius/spread-sheets-angular": {
defaultExtension: 'mjs'
},
'@angular/core': {
defaultExtension: 'mjs',
main: 'core.mjs'
}
}
});
})(this);