本帖最后由 IT-Weaver 于 2022-1-12 09:30 编辑
之前在论坛下载的demo中,一般都是以顺序形式写的代码,如:
- var asyncSum = function () {
- };
- asyncSum.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction("ASUM", 1, 255);
- asyncSum.prototype.defaultValue = function () {
- return "Loading...";
- };
- asyncSum.prototype.evaluateAsync = function (context) {
- // use setTimeout to simulate server side evaluation
- // in read world it maybe an ajax post to server for evaluation
- var args = arguments;
- setTimeout(function () {
- var result = 0;
- for (var i = 1; i < args.length; i++) {
- result += args[i];
- }
- result *= 2;
- context.setAsyncResult(result);
- }, 2000);
- };
- var GetNumberFromServer = function () {
- };
- GetNumberFromServer.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction("GETNUMBERFROMSERVER", 1, 2);
- GetNumberFromServer.prototype.evaluate = function (context, arg1, arg2) {
- setTimeout(function () {
- var value = Math.random() + 1;
- context.setAsyncResult(value);
- }, 500);
- };
- GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("GETNUMBERFROMSERVER", new GetNumberFromServer());
- var GetTimeFromServer = function () {
- };
- GetTimeFromServer.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction("GETTIMEFROMSERVER");
- GetTimeFromServer.prototype.evaluate = function (context) {
- setTimeout(function () {
- var date = new Date();
- context.setAsyncResult(date);
- }, 500);
- };
- GetTimeFromServer.prototype.evaluateMode = function () {
- return 2;
- };
- GetTimeFromServer.prototype.interval = function () {
- return 1000;
- };
- GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("GETTIMEFROMSERVER", new GetTimeFromServer());
- var sheet = spread.sheets[0];
- sheet.suspendPaint();
- sheet.options.allowCellOverflow = true;
- sheet.setArray(0, 0, [[5, 15]]);
- sheet.addCustomFunction(new asyncSum());
- sheet.setText(1, 1, 'ASUM(A1,B1)');
- for(i=0;i<3001;i++){
- sheet.setFormula(i, 7, "ASUM(A1,B1)");
- }
- sheet.setText(2, 1, 'SUM(A1,B1)');
- sheet.setFormula(1, 2, "ASUM(A1,B1)");
- sheet.getCell(1, 2).foreColor("green");
- sheet.setFormula(2, 2, "SUM(A1,B1)");
复制代码
但是在我们实际需求中,一般这些动态类会以引用形式进行编写,如:
- import { asyncCustomFormulaV12 } from '../js/report/customFormula.js'
- let customFunction = new asyncCustomFormulaV12(customFormula.fName);
- activeSheet.addCustomFunction(customFunction);
复制代码 那么现在有这样一个问题,当我引用时,我就无法在
- asyncCustomFormulaV12.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction();
复制代码 继承原型链时,动态传递name,minArgs,maxArgs等参数,请问这些情景下,应该如何操作,才可以让动态公式生效,在网上搜了好多相关原型链继承的方式,都尝试,依然在引用时报“无效的自定义函数”,但是如官方demo写在页面里,就可以绑定。
|