在格式提示中,有三个步骤。
获取格式
从引用的单元格中获取格式
从百分比值中获取百分比格式
从特殊函数中获取格式,例如:NOW/TODAY/IRR/NPV
从动态引用中获取格式,例如:=INDIRECT("C3")
格式计算
以下是一些格式计算策略的列表:
日期格式减去日期格式将是空格式
日期格式加上时间格式将是日期时间格式
货币格式乘以数字格式将是货币格式
数字格式乘以百分比格式将是数字格式
空格式加上百分比格式将是空格式
SUM/MAX/MIN/AVEDEV/LOOR/ROUND/ROUNDUP/ROUNDDOWN/INT/TRUNC函数将在第一个参数的第一个单元格使用格式。
适用格式
当输入公式到单元格时,如果单元格没有明确设置格式,SpreadJS将应用格式提示到该单元格。
你可以使用spread.options.formulaFormatHint选项来控制是否启用格式提示。
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {
sheetCount: 2
});
initSpread(spread);
};
function initSpread(spread) {
initWorkbook(spread);
initEvents(spread);
}
function initWorkbook(spread) {
var 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();
}
function initEvents(spread) {
document.getElementById('formulaFormatHint').onchange = function (e) {
var formulaFormatHint = e.target.checked;
spread.options.formulaFormatHint = formulaFormatHint;
};
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="spreadjs culture" content="zh-cn" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js"
type="text/javascript"></script>
<script
src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.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">
<p>Cells C8:C15 are auto formatted (using format hints) after the formulas were entered.</p>
<div class="sp-demo-childBlock">
<div id="settingsDiv">
<div class="option-row">
<input type="checkbox" id="formulaFormatHint" checked="checked" />
<label for="formulaFormatHint">Enable format hints</label>
</div>
</div>
</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;
margin-top: 10px;
}
label {
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
}
p{
padding:2px 10px;
background-color:#F4F8EB;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}