条件格式和数据验证

TableSheet 支持在视图列级别配置条件格式和数据验证,可以根据数据值动态改变单元格样式,并对用户输入进行验证。通过条件格式可以快速突出显示重要数据,通过数据验证可以确保数据的准确性。

概述 本 Demo 展示了如何在 TableSheet 视图中应用条件格式、数据验证和列样式。Demo 中为 Unit Price(单价)列配置了三种特性:条件格式规则(单价 >= 50 时显示特殊样式)、数据验证器(验证输入值 < 50)和货币格式样式;同时还演示了在另一张 TableSheet 视图中为销售数据配置迷你图条件格式。 实现思路 初始化 DataManager 并添加远程数据表,配置数据源 URL 创建 TableSheet 并设置基本选项 定义条件格式规则,使用公式规则判断单价是否 >= 50 定义数据验证器,使用公式验证输入值是否 < 50 定义列样式,设置货币格式 创建视图时在列配置中应用条件格式、验证器和样式 获取数据后将视图绑定到 TableSheet 代码解析 定义条件格式规则 这段代码定义了一个公式规则条件格式。ruleType 指定为 "formulaRule" 表示使用公式规则。formula 属性使用 "@>=50" 判断当前单元格值是否 >= 50,其中 @ 表示当前单元格的值。当条件满足时,应用 style 中定义的样式,包括粗体字体、橙色背景和文字颜色。 定义数据验证器 这段代码定义了一个公式类型的数据验证器。type 设置为 "formula" 表示使用公式验证。formula 属性使用 "@<50" 验证输入值是否 < 50。inputTitle 和 inputMessage 用于在用户编辑单元格时显示提示信息。highlightStyle 配置验证失败时的视觉提示,包括图标类型、颜色和位置。 创建视图并应用配置 在创建视图时,通过 conditionalFormats 属性应用条件格式规则数组,通过 validator 属性设置数据验证器,通过 style 属性配置列样式。Unit Price 列同时应用了这三种配置,使数据展示更加直观。最后一列使用自定义格式字符串显示勾选标记。 运行效果 Unit Price 列中,单价 >= 50 的单元格显示为粗体橙色文字和浅橙色背景 当用户尝试在 Unit Price 列输入数据时,会看到提示信息“数据验证:请输入小于 50 的数字。” 如果输入值 >= 50,单元格右侧会显示一个橙色警告图标 所有价格都以 "$ 0.00" 格式显示 Discontinued 列根据值显示绿色勾选或红色叉号标记 API 参考 addTable 方法 name:表的名称 dataSourceOption.remote.read.url:数据源的读取 URL addView 方法 name:视图名称 columns:列配置数组,可包含以下属性: value:绑定字段名 caption:列标题 width:列宽 conditionalFormats:条件格式规则数组 validator:数据验证器配置 style:列样式配置 formulaRule 配置 ruleType:规则类型,设为 "formulaRule" formula:公式表达式,使用 @ 引用当前单元格值 style:条件满足时应用的样式 formula 类型验证器配置 type:验证器类型,设为 "formula" formula:验证公式 inputTitle:输入提示标题 inputMessage:输入提示信息 highlightStyle:验证失败时的视觉提示样式
/*REPLACE_MARKER*/ /*DO NOT DELETE THESE COMMENTS*/ window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 0 }); initSpread(spread); }; function initSpread(spread) { spread.suspendPaint(); spread.options.autoFitType = GC.Spread.Sheets.AutoFitType.cellWithHeader; spread.options.highlightInvalidData = true; initProductSheet(spread); initSparklineRuleSheet(spread); spread.setActiveSheetIndex(0); spread.resumePaint(); } function initProductSheet(spread) { //init a data manager var baseApiUrl = getBaseApiUrl(); var dataManager = spread.dataManager(); //add product table var productTable = dataManager.addTable("productTable", { remote: { read: { url: baseApiUrl + "/Product" } } }); //init a table sheet var sheet = spread.addSheetTab(0, "条件格式", GC.Spread.Sheets.SheetType.tableSheet); sheet.options.allowAddNew = false; //hide new row sheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader); //bind a view to the table sheet var numericStyle = {}; numericStyle.formatter = "$ 0.00"; var formulaRule = { ruleType: "formulaRule", formula: "@>=50", style: { font:"bold 12pt Calibri", backColor: "#F7D3BA", foreColor :"#F09478" } }; var positiveNumberValidator = { type: "formula", formula: '@<50', inputTitle: '数据验证:', inputMessage: '请输入小于 50 的数字。', highlightStyle: { type: 'icon', color: "#F09478", position: 'outsideRight', } }; var myView = productTable.addView("myView", [ { value: "Id", caption: "编号", width: 64}, { value: "ProductName", caption: "名称", width: 250 }, { value: "QuantityPerUnit", caption: "每单位数量", width: 140}, { value: "UnitPrice", caption: "单价", width: 140, conditionalFormats: [formulaRule], validator: positiveNumberValidator, style: numericStyle }, { value: "UnitsInStock", caption: "库存量", width: 140}, { value: "UnitsOnOrder", caption: "在订量", width: 140}, { value: "Discontinued", caption: "已停产", width: 120, style: { formatter:"[green]✔;;[red]✘", hAlign: GC.Spread.Sheets.HorizontalAlign.center }} ]); myView.fetch().then(function () { sheet.setDataView(myView); }); } function initSparklineRuleSheet(spread) { var dataManager = spread.dataManager(); // Regional sales data with a mix of above/below target results var salesTable = dataManager.addTable("salesTable", { data: [ { region: "北美", target: 850, actual: 920, forecast: 880, actual1: 920, actual2: 920 }, { region: "欧洲", target: 720, actual: 680, forecast: 700, actual1: 680, actual2: 680 }, { region: "亚太", target: 650, actual: 710, forecast: 670, actual1: 710, actual2: 710 }, { region: "拉丁美洲", target: 380, actual: 350, forecast: 360, actual1: 350, actual2: 350 }, { region: "中东", target: 280, actual: 310, forecast: 290, actual1: 310, actual2: 310 }, { region: "非洲", target: 180, actual: 165, forecast: 170, actual1: 165, actual2: 165 }, { region: "大洋洲", target: 150, actual: 160, forecast: 155, actual1: 160, actual2: 160 }, { region: "南亚", target: 220, actual: 195, forecast: 210, actual1: 195, actual2: 195 }, ] }); // Create a table sheet for sparkline rules var sheet = spread.addSheetTab(1, "迷你图规则", GC.Spread.Sheets.SheetType.tableSheet); sheet.options.allowAddNew = false; sheet.setDefaultRowHeight(40, GC.Spread.Sheets.SheetArea.colHeader); sheet.setDefaultRowHeight(40); var bulletRule = { ruleType: "sparklineRule", sparklineType: "BULLETSPARKLINE", sparklineOptions: { measure: '@', // current cell value = actual revenue target: 500, // company-wide benchmark target ($500K) maxi: 1000, // maximum scale ($1000K) good: 700, // "exceeding expectations" threshold bad: 250, // "needs improvement" threshold forecast: 600, // company average forecast colorScheme: '#2F5496' }, showSparklineOnly: true }; var lollipopRule = { ruleType: "sparklineRule", sparklineType: "LOLLIPOPVARISPARKLINE", sparklineOptions: { plannedValue: '[target]', actualValue: '$CF_RANGE$', index: '@', // 0-based index of current cell within rule range reference: 0, // reference line at 0% variance mini: -0.3, // minimum variance scale (-30%) maxi: 0.3, // maximum variance scale (+30%) tickUnit: 0.1, // tick marks every 10% legend: true, // show legend labels colorPositive: '#70AD47', // green for above target colorNegative: '#ED7D31', // orange for below target lollipopHeaderColor: '#2F5496' // dot color }, showSparklineOnly: true }; var numericStyle = { formatter: "$#,##0" }; var salesView = salesTable.addView("salesView", [ { value: "region", caption: "区域", width: 130 }, { value: "target", caption: "目标值(千美元)", width: 100, style: numericStyle }, { value: "actual", caption: "实际值(千美元)", width: 100, style: numericStyle }, { value: "forecast", caption: "预测值(千美元)", width: 100, style: numericStyle }, { value: "actual1", caption: "绩效(子弹图)", width: 220, conditionalFormats: [bulletRule] }, { value: "actual2", caption: "差异(棒棒糖图)", width: 220, conditionalFormats: [lollipopRule] } ]); salesView.fetch().then(function () { sheet.setDataView(salesView); }); } function getBaseApiUrl() { return window.location.href.match(/http.+spreadjs\/SpreadJSTutorial\//)[0] + 'server/api'; }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <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"> <!-- Promise Polyfill for IE, https://www.npmjs.com/package/promise-polyfill --> <script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script> <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-tablesheet/dist/gc.spread.sheets.tablesheet.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 id="optionContainer" class="optionContainer"> </div> </div> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .sample-spreadsheets { width: 100%; height: 100%; overflow: hidden; float: left; }