自定义函数

尽管FlexSheet中提供的功能应涵盖绝大多数使用场景,但仍可能存在某些用户可能需要其他功能的情况。

FlexSheet提供了两种方法,允许您添加自己的自定义函数:addFunctionunknownFunction

addFunction 方法将自定义函数添加到内置函数列表中。

addFunction方法通常是向FlexSheet计算引擎添加自定义函数的最佳方法。 但是,有些情况下功能名称可变或未知。 例如,命名范围或值字典。

在这些情况下,您可以使用 unknownFunction 事件动态查找函数的值。 当FlexSheet检测到未知函数名称时,它会引发unknownFunction事件并提供包含函数名称和参数的参数。 然后事件处理程序计算结果并返回值。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as wjFlexSheet from '@grapecity/wijmo.grid.sheet'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { let customFuncSheet = new wjFlexSheet.FlexSheet('#customFuncSheet'); customFuncSheet.addUnboundSheet('Custom Function', 20, 10); customFuncSheet.addUnboundSheet('Data', 20, 10); customFuncSheet.addFunction('customSumProduct', (...params) => { let result = 0, range1 = params[0], range2 = params[1]; if (range1.length > 0 && range1.length === range2.length && range1[0].length === range2[0].length) { for (let i = 0; i < range1.length; i++) { for (let j = 0; j < range1[0].length; j++) { result += range1[i][j] * range2[i][j]; } } } return result; }, 'Custom SumProduct Function', 2, 2); customFuncSheet.unknownFunction.addHandler((sender, e) => { let result = ''; if (e.params) { for (let i = 0; i < e.params.length; i++) { result += e.params[i]; } } e.value = result; }); customFuncSheet.deferUpdate(() => { for (let i = customFuncSheet.sheets.length - 1; i >= 0; i--) { customFuncSheet.sheets.selectedIndex = i; switch (customFuncSheet.sheets[i].name) { case 'Custom Function': customFuncSheet.setCellData(0, 0, '=customSumProduct(Data!A1:B5, Data!B1:C5)'); customFuncSheet.setCellData(1, 0, '=customFunc(1, "B", 3)'); break; case 'Data': for (let ri = 0; ri < customFuncSheet.rows.length; ri++) { for (let ci = 0; ci < 3; ci++) { customFuncSheet.setCellData(ri, ci, ri + ci); } } break; } } }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexSheet Custom Function</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <div id="customFuncSheet"></div> </div> </body> </html> .wj-flexsheet { height: 400px; margin: 6px 0; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { WjGridSheetModule } from '@grapecity/wijmo.angular2.grid.sheet'; import * as wjcSheet from '@grapecity/wijmo.grid.sheet'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { initializeFlexSheet(flex: wjcSheet.FlexSheet) { flex.addFunction('customSumProduct', (...params: any[][][]) => { let result = 0, range1 = params[0], range2 = params[1]; if (range1.length > 0 && range1.length === range2.length && range1[0].length === range2[0].length) { for (let i = 0; i < range1.length; i++) { for (let j = 0; j < range1[0].length; j++) { result += range1[i][j] * range2[i][j]; } } } return result; }, 'Custom SumProduct Function', 2, 2); flex.unknownFunction.addHandler((sender, e: wjcSheet.UnknownFunctionEventArgs) => { let result = ''; if (e.params) { for (let i = 0; i < e.params.length; i++) { result += e.params[i]; } } e.value = result; }); flex.deferUpdate(() => { for (let i = flex.sheets.length - 1; i >= 0; i--) { flex.sheets.selectedIndex = i; switch (flex.sheets[i].name) { case 'Custom Function': flex.setCellData(0, 0, '=customSumProduct(Data!A1:B5, Data!B1:C5)'); flex.setCellData(1, 0, '=customFunc(1, "B", 3)'); break; case 'Data': for (let ri = 0; ri < flex.rows.length; ri++) { for (let ci = 0; ci < 3; ci++) { flex.setCellData(ri, ci, ri + ci); } } break; } } }); } } @NgModule({ imports: [WjGridSheetModule, BrowserModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexSheet Custom Function</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="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.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <!-- the flexsheet --> <wj-flex-sheet #flex (initialized)="initializeFlexSheet(flex)"> <wj-sheet [name]="'Custom Function'" [rowCount]="20" [columnCount]="10"></wj-sheet> <wj-sheet [name]="'Data'" [rowCount]="20" [columnCount]="10"></wj-sheet> </wj-flex-sheet> </div> .wj-flexsheet { height: 400px; margin: 6px 0; } <template> <div class="container-fluid"> <!-- the flexsheet --> <wj-flex-sheet :initialized="initializeFlexSheet"> <wj-sheet :name="'Custom Function'" :rowCount="20" :columnCount="10"></wj-sheet> <wj-sheet :name="'Data'" :rowCount="20" :columnCount="10"></wj-sheet> </wj-flex-sheet> </div> </template> <script> import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import Vue from "vue"; import "@grapecity/wijmo.vue2.core"; import "@grapecity/wijmo.vue2.input"; import "@grapecity/wijmo.vue2.grid.sheet"; let App = Vue.extend({ name: "app", methods: { initializeFlexSheet: function(flex) { flex.addFunction( "customSumProduct", (...params) => { let result = 0, range1 = params[0], range2 = params[1]; if ( range1.length > 0 && range1.length === range2.length && range1[0].length === range2[0].length ) { for (let i = 0; i < range1.length; i++) { for (let j = 0; j < range1[0].length; j++) { result += range1[i][j] * range2[i][j]; } } } return result; }, "Custom SumProduct Function", 2, 2 ); flex.unknownFunction.addHandler((sender, e) => { let result = ""; if (e.params) { for (let i = 0; i < e.params.length; i++) { result += e.params[i]; } } e.value = result; }); flex.deferUpdate(() => { for (let i = flex.sheets.length - 1; i >= 0; i--) { flex.sheets.selectedIndex = i; switch (flex.sheets[i].name) { case "Custom Function": flex.setCellData( 0, 0, "=customSumProduct(Data!A1:B5, Data!B1:C5)" ); flex.setCellData(1, 0, '=customFunc(1, "B", 3)'); break; case "Data": for (let ri = 0; ri < flex.rows.length; ri++) { for (let ci = 0; ci < 3; ci++) { flex.setCellData(ri, ci, ri + ci); } } break; } } }); } } }); new Vue({ render: h => h(App) }).$mount("#app"); </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>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/jszip/dist/jszip.js"></script> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> import './app.css'; import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjGridSheet from "@grapecity/wijmo.react.grid.sheet"; class App extends React.Component { constructor(props) { super(props); } initializeFlexSheet(flex) { flex.addFunction("customSumProduct", (...params) => { let result = 0, range1 = params[0], range2 = params[1]; if (range1.length > 0 && range1.length === range2.length && range1[0].length === range2[0].length) { for (let i = 0; i < range1.length; i++) { for (let j = 0; j < range1[0].length; j++) { result += range1[i][j] * range2[i][j]; } } } return result; }, "Custom SumProduct Function", 2, 2); flex.unknownFunction.addHandler((sender, e) => { let result = ""; if (e.params) { for (let i = 0; i < e.params.length; i++) { result += e.params[i]; } } e.value = result; }); flex.deferUpdate(() => { for (let i = flex.sheets.length - 1; i >= 0; i--) { flex.sheets.selectedIndex = i; switch (flex.sheets[i].name) { case "Custom Function": flex.setCellData(0, 0, "=customSumProduct(Data!A1:B5, Data!B1:C5)"); flex.setCellData(1, 0, '=customFunc(1, "B", 3)'); break; case "Data": for (let ri = 0; ri < flex.rows.length; ri++) { for (let ci = 0; ci < 3; ci++) { flex.setCellData(ri, ci, ri + ci); } } break; } } }); } render() { return (<div className="container-fluid"> <wjGridSheet.FlexSheet initialized={this.initializeFlexSheet.bind(this)}> <wjGridSheet.Sheet name="Custom Function'" rowCount={20} columnCount={10}></wjGridSheet.Sheet> <wjGridSheet.Sheet name="Data" rowCount={20} columnCount={10}></wjGridSheet.Sheet> </wjGridSheet.FlexSheet> </div>); } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AutoComplete</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> .container-fluid .wj-flexsheet { height: 400px; margin: 6px 0; }