//是否自定义公式 function IsCustumFormula( fromStr) { var index=fromStr.indexOf("GETTAGSNAPSHORT("); if(index>=0) return true; index=fromStr.indexOf("GETTAGHISINTERVAL("); if(index>=0) return true; index=fromStr.indexOf("GETTIMEINTERVAL("); if(index>=0) return true; index=fromStr.indexOf("GETTIMESTASTIC("); if(index>=0) return true; return false; } function ApplyCustomFunc(spread) { var sheet = spread.getSheet(0); var getTagSnapShort = new goldenNameSpace.GetTagSnapShort(); sheet.addCustomFunction(getTagSnapShort); var getTagHisInterval = new goldenNameSpace.GetTagHisInterval(); sheet.addCustomFunction(getTagHisInterval); var getTimeInterval = new goldenNameSpace.GetTimeInterval(); sheet.addCustomFunction(getTimeInterval); var getTimeStastic= new goldenNameSpace.GetTimeStastic(); sheet.addCustomFunction(getTimeStastic); var getTagTimeStasticValue= new goldenNameSpace.GetTagTimeStasticValue(); sheet.addCustomFunction(getTagTimeStasticValue); var getTagTimeStasticValue2= new goldenNameSpace.GetTagTimeStasticValue2(); sheet.addCustomFunction(getTagTimeStasticValue2); var getTimeDisplayStr= new goldenNameSpace.GetTimeDisplayStr(); sheet.addCustomFunction(getTimeDisplayStr); } //重新激活公式 function ResetFormular(spread) { var sheet = spread.getSheet(0); //ApplyCustomFunc(spread); var colNum=sheet.getColumnCount(); var rowNum=sheet.getRowCount(); for( var i =0;i=0) { var myCellType = new MyCellType(); sheet.getCell(j,i).cellType(myCellType) } } catch(e) { }*/ } sheet.autoFitColumn(i); } } //斜线表头 function MyCellType() { } MyCellType.prototype = new GC.Spread.Sheets.CellTypes.Text(); MyCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) { //Paints a cell on the canvas. if (!ctx) { return; } ctx.save(); ctx.beginPath(); ctx.moveTo(x,y); ctx.lineTo(x+w,y+h); ctx.stroke(); ctx.font = style.font; var text = value ? value.split(" ") : ["",""]; ctx.fillText(text[0].trim() ,x + w *7/8, y + h/3); ctx.fillText(text[1].trim(),x + w /2, y + h*3/4); ctx.restore(); }; var goldenNameSpace = {}; (function () { // 自定义公式(获取统计时间段值2)---------------------------------------------------------------------- function GetTagTimeStasticValue2 () { this.name = 'GetTagTimeStasticValue2'; this.maxArgs = 4; this.minArgs = 4; GC.Spread.CalcEngine.Functions.Function.apply(this, ["GetTagTimeStasticValue2", 0, 0]); this.typeName = "goldenNameSpace.GetTagTimeStasticValue2"; } GetTagTimeStasticValue2.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction(); GetTagTimeStasticValue2.prototype.evaluateMode = function () { return 0; }; goldenNameSpace.GetTagTimeStasticValue2=GetTagTimeStasticValue2; GetTagTimeStasticValue2.prototype.evaluateAsync = function (context) { if(arguments.length>=5) { var ReportTag=arguments[1]; var ReportStartTime=arguments[2]; var ReportEndTime=arguments[3]; var ReportSelectType=arguments[4]; ReportStartTime= window[ReportStartTime];//获取控件中的变量值 if(!ReportStartTime) return ""; ReportEndTime= window[ReportEndTime];//获取控件中的变量值 if(!ReportEndTime) return ""; } else return ""; var TempStartTime=new Date(Date.parse(ReportStartTime)); var TempEndTime=new Date(Date.parse(ReportEndTime)); console.log("ask:"+ReportTag); var urlStr = "http://" + webServiceIP + ":" + webServicePort + "/api/HistorySummary?tagName="; urlStr+=ReportTag+"&beginTime="+TempStartTime.Format("yyyy-MM-dd hh:mm:ss")+"&endTime="+TempEndTime.Format("yyyy-MM-dd hh:mm:ss")+"&dataType=snap"; $.ajax({ type: 'GET', url: urlStr, dataType: 'json', cache: false, error:function(data) { context.setAsyncResult(data.responseText); }, success: function (data) { if(!data) return; for (var key in data) { console.log("answer:"+key); var tagData= data[key]; if(!tagData) continue; if(ReportSelectType=="avg") { var actVal=GetDital(tagData.CalcAvg,3); context.setAsyncResult(actVal); } else if(ReportSelectType=="max") { var actVal=GetDital(tagData.Max,3); context.setAsyncResult(actVal); } else if(ReportSelectType=="min") { var actVal=GetDital(tagData.Min,3); context.setAsyncResult(actVal); } else if(ReportSelectType=="sub") { var actVal=GetDital(tagData.Difference,3); context.setAsyncResult(actVal); } } } }); } // 自定义公式(获取统计时间段值)---------------------------------------------------------------------- function GetTagTimeStasticValue () { this.name = 'GetTagTimeStasticValue'; this.maxArgs = 4; this.minArgs = 4; GC.Spread.CalcEngine.Functions.Function.apply(this, ["GetTagTimeStasticValue", 0, 0]); this.typeName = "goldenNameSpace.GetTagTimeStasticValue"; } GetTagTimeStasticValue.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction(); GetTagTimeStasticValue.prototype.evaluateMode = function () { return 0; }; goldenNameSpace.GetTagTimeStasticValue=GetTagTimeStasticValue; GetTagTimeStasticValue.prototype.evaluateAsync = function (context) { if(arguments.length>=5) { ReportTag=arguments[1]; ReportSelectTime=arguments[2]; ReportTimeType=arguments[3]; ReportSelectType=arguments[4]; ReportSelectTime= window[ReportSelectTime];//获取控件中的变量值 if(!ReportSelectTime) return ""; } else return ""; var TempStartTime=new Date(Date.parse(ReportSelectTime)); var TempEndTime=new Date(Date.parse(ReportSelectTime)); if(ReportTimeType=='d'||ReportTimeType=='D') { TempStartTime.setHours(0); TempStartTime.setMinutes(0); TempStartTime.setSeconds(0); TempEndTime.setHours(23); TempEndTime.setMinutes(59); TempEndTime.setSeconds(59); } else if(ReportTimeType=='m'||ReportTimeType=='M') { TempStartTime.setDate(1); TempStartTime.setHours(0); TempStartTime.setMinutes(0); TempStartTime.setSeconds(0); TempEndTime.setMonth( TempEndTime.getMonth()+1); TempEndTime.setHours(23); TempEndTime.setMinutes(59); TempEndTime.setSeconds(59); } else if(ReportTimeType=='y'||ReportTimeType=='Y') { TempStartTime.setMonth(0); TempStartTime.setDate(1); TempStartTime.setHours(0); TempStartTime.setMinutes(0); TempStartTime.setSeconds(0); var year= ReportSelectTime.getYear(); TempEndTime.setYear(year+1+1900); TempEndTime.setMonth(0); TempEndTime.setDate(0); TempEndTime.setHours(23); TempEndTime.setMinutes(59); TempEndTime.setSeconds(59); } console.log("ask:"+ReportTag); var urlStr = "http://" + webServiceIP + ":" + webServicePort + "/api/HistorySummary?tagName="; urlStr+=ReportTag+"&beginTime="+TempStartTime.Format("yyyy-MM-dd hh:mm:ss")+"&endTime="+TempEndTime.Format("yyyy-MM-dd hh:mm:ss")+"&dataType=snap"; $.ajax({ type: 'GET', url: urlStr, dataType: 'json', cache: false, error:function(data) { context.setAsyncResult(data.responseText); }, success: function (data) { if(!data) return; for (var key in data) { console.log("answer:"+key); var tagData= data[key]; if(!tagData) continue; if(ReportSelectType=="avg") { var actVal=GetDital(tagData.CalcAvg,3); context.setAsyncResult(actVal); } else if(ReportSelectType=="max") { var actVal=GetDital(tagData.Max,3); context.setAsyncResult(actVal); } else if(ReportSelectType=="min") { var actVal=GetDital(tagData.Min,3); context.setAsyncResult(actVal); } else if(ReportSelectType=="sub") { var actVal=GetDital(tagData.Difference,3); context.setAsyncResult(actVal); } else if(ReportSelectType=="start") { context.setAsyncResult(tagData.StartTime); } else if(ReportSelectType=="end") { context.setAsyncResult(tagData.EndTime); } } } }); } // 自定义公式(获取统计时间段时间值)---------------------------------------------------------------------- function GetTimeDisplayStr () { this.name = 'GetTimeDisplayStr'; this.maxArgs = 2; this.minArgs = 2; GC.Spread.CalcEngine.Functions.Function.apply(this, ["GetTimeDisplayStr", 0, 0]); this.typeName = "goldenNameSpace.GetTimeDisplayStr"; } goldenNameSpace.GetTimeDisplayStr=GetTimeDisplayStr; GetTimeDisplayStr.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction(); GetTimeDisplayStr.prototype.evaluateMode = function () { return 0; }; GetTimeDisplayStr.prototype.evaluateAsync = function (context) { //context.setAsyncResult(""); //return; if(arguments.length>=2) { ReportSelectTime=arguments[1]; ReportTimeFormat=arguments[2]; ReportSelectTime= window[ReportSelectTime];//获取控件中的变量值 if(!ReportSelectTime) { context.setAsyncResult("未找到时间变量!"); return; } } else { context.setAsyncResult("参数数量不足!"); return; } ReportSelectTime=new Date(Date.parse(ReportSelectTime)); var ReportSelectTimeStr=ReportSelectTime.Format(ReportTimeFormat); context.setAsyncResult(ReportSelectTimeStr); } // 自定义公式(获取统计时间段值)---------------------------------------------------------------------- // 自定义公式(获取统计时间段时间值)---------------------------------------------------------------------- function GetTimeStastic () { this.name = 'GetTimeStastic'; this.maxArgs = 4; this.minArgs = 4; GC.Spread.CalcEngine.Functions.Function.apply(this, ["GetTimeStastic", 0, 0]); this.typeName = "goldenNameSpace.GetTimeStastic"; } goldenNameSpace.GetTimeStastic=GetTimeStastic; GetTimeStastic.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction(); GetTimeStastic.prototype.evaluateMode = function () { return 0; }; GetTimeStastic.prototype.evaluateAsync = function (context) { if(arguments.length>=5) { ReportSelectTime=arguments[1]; ReportTimeType=arguments[2]; ReportSelectType=arguments[3]; ReportTimeFormat=arguments[4]; ReportSelectTime= window[ReportSelectTime];//获取控件中的变量值 } else { context.setAsyncResult("error!"); return; } ReportSelectTime=new Date(Date.parse(ReportSelectTime)); if(ReportTimeType=='d') { if(ReportSelectType=="start") { ReportSelectTime.setHours(0); ReportSelectTime.setMinutes(0); ReportSelectTime.setSeconds(0); } else { ReportSelectTime.setHours(23); ReportSelectTime.setMinutes(59); ReportSelectTime.setSeconds(59); } } else if(ReportTimeType=='m') { if(ReportSelectType=="start") { ReportSelectTime.setSeconds(0); } else { ReportSelectTime.setSeconds(59); } } else if(ReportTimeType=='h') { if(ReportSelectType=="start") { ReportSelectTime.setMinutes(0); ReportSelectTime.setSeconds(0); } else { ReportSelectTime.setMinutes(59); ReportSelectTime.setSeconds(59); } } else if(ReportTimeType=='M') { if(ReportSelectType=="start") { ReportSelectTime.setDate(1); ReportSelectTime.setHours(0); ReportSelectTime.setMinutes(0); ReportSelectTime.setSeconds(0); } else { ReportSelectTime.setMonth( ReportSelectTime.getMonth()+1); ReportSelectTime.setDate(0); ReportSelectTime.setHours(23); ReportSelectTime.setMinutes(59); ReportSelectTime.setSeconds(59); } } else if(ReportTimeType=='y') { if(ReportSelectType=="start") { ReportSelectTime.setMonth(0); ReportSelectTime.setDate(1); ReportSelectTime.setHours(0); ReportSelectTime.setMinutes(0); ReportSelectTime.setSeconds(0); } else { var year= ReportSelectTime.getYear(); ReportSelectTime.setYear(year+1+1900); ReportSelectTime.setMonth(0); ReportSelectTime.setDate(0); ReportSelectTime.setHours(23); ReportSelectTime.setMinutes(59); ReportSelectTime.setSeconds(59); } } var ReportSelectTimeStr=ReportSelectTime.Format(ReportTimeFormat); context.setAsyncResult(ReportSelectTimeStr); } // 自定义公式(获取统计时间段时间值)---------------------------------------------------------------------- // 自定义公式(获取时间段的等间隔显示)---------------------------------------------------------------------- function GetTimeInterval () { this.name = 'GetTimeInterval'; this.maxArgs = 6; this.minArgs = 6; GC.Spread.CalcEngine.Functions.Function.apply(this, ["GetTimeInterval", 0, 0]); this.typeName = "goldenNameSpace.GetTimeInterval"; } GetTimeInterval.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction(); GetTimeInterval.prototype.defaultValue = function () {return 'loading'}; GetTimeInterval.prototype.evaluateMode = function () { return 0; }; goldenNameSpace.GetTimeInterval=GetTimeInterval; //获取历史等间隔数据 GetTimeInterval.prototype.evaluateAsync = function (context) { var args = arguments; var curCol=context.col; var curRow=context.row; if(args.length>=6) { var ReportStartTime=args[1]; var ReportEndTime=args[2]; var ReportInterval=args[3]; var ReportIntervalType=args[4]; var ReportTimeFormat=args[5]; var ReportVH=args[6]; ReportStartTime= window[ReportStartTime]; ReportEndTime= window[ReportEndTime]; //ReportInterval= window[ReportInterval]; //ReportIntervalType = window[ReportIntervalType]; } else { context.setAsyncResult(0); return; } var tempDateTime=new Date(Date.parse(ReportStartTime)); var tempEndTime=new Date(Date.parse(ReportEndTime)); var sheet =context.ctx.source; for(var i=1;i<200;i++) { if(ReportIntervalType=='s') { tempDateTime.setSeconds(tempDateTime.getSeconds()+ReportInterval); } else if(ReportIntervalType=='m') { tempDateTime.setMinutes(tempDateTime.getMinutes()+ReportInterval); } else if(ReportIntervalType=='h') { tempDateTime.setHours(tempDateTime.getHours()+ReportInterval); } else if(ReportIntervalType=='d') { tempDateTime.setDate(tempDateTime.getDate()+ReportInterval); } else if(ReportIntervalType=='M') { tempDateTime.setMonth(tempDateTime.getMonth()+ReportInterval); } else if(ReportIntervalType=='y') { tempDateTime.setYear(tempDateTime.getYear()+ReportInterval); } var tempDateStr=tempDateTime.Format(ReportTimeFormat); if(ReportVH=="v") { if(curRow+i=tempEndTime) break; } tempDateTime=new Date(Date.parse(ReportStartTime)); tempDateStr=tempDateTime.Format(ReportTimeFormat); context.setAsyncResult(tempDateStr); } // 自定义公式(获取时间段的等间隔显示)---------------------------------------------------------------------- // 自定义公式(获取获取历史等间隔数据)---------------------------------------------------------------------- function GetTagHisInterval () { this.name = 'GetTagHisInterval'; this.maxArgs = 6; this.minArgs = 6; GC.Spread.CalcEngine.Functions.Function.apply(this, ["GetTagHisInterval", 0, 0]); this.typeName = "goldenNameSpace.GetTagHisInterval"; } goldenNameSpace.GetTagHisInterval=GetTagHisInterval; GetTagHisInterval.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction(); GetTagHisInterval.prototype.defaultValue = function () {return 'loading'}; GetTagHisInterval.prototype.evaluateMode = function () { return 0; }; GetTagHisInterval.prototype.interval = function () { return 1000; }; //获取历史等间隔数据 GetTagHisInterval.prototype.evaluateAsync = function (context) { var args = arguments; var curCol=context.col; var curRow=context.row; if(args.length>=5) { var Reporttag=args[1]; var ReportStartTime=args[2]; var ReportEndTime=args[3]; var ReportInterval=args[4]; var ReportIntervalType=args[5]; var ReportVH=args[6]; ReportStartTime= window[ReportStartTime]; ReportEndTime= window[ReportEndTime]; //ReportInterval= window[ReportInterval]; ReportIntervalType= "0";// window[ReportIntervalType]; } else { context.setAsyncResult(0); return; } if(ReportIntervalType=="m")//分钟 { ReportInterval=ReportInterval*60; } else if(ReportIntervalType=="h")//小时 { ReportInterval=ReportInterval*3600; } else if(ReportIntervalType=="d")//天 { ReportInterval=ReportInterval*3600*24; } var urlStr = "http://" + webServiceIP + ":" + webServicePort + "/api/HistoryInterval?tagName="; //+ cmdStr; pageIndex=1; tagsCount=200; urlStr += createSearchStr(Reporttag,ReportStartTime, ReportEndTime, ReportInterval, ReportIntervalType, ReportInterval) + '&pageIndex=' + pageIndex + '&pageSize='+ tagsCount; // getTableHisData(shotTablePane,url,shotTableTableModel,shotTablecolumns,shotTablerows); $.ajax({ type: 'GET', url: urlStr, dataType: 'json', cache: false, success: function (data) { if(!data.Datas) { context.setAsyncResult("not find!"); return; } for (var key in data.Datas) { var tagData= data.Datas[key]; if(!tagData) continue; if(tagData.length>0) { var showCount=200; if(tagData.length1) tag=args[1]; else { context.setAsyncResult(0); return; } var str = "http://" + webServiceIP + ":" + webServicePort + "/api/Snapshot?tagName="; var para = tag; str = str + encodeURIComponent(para); $.ajax({ url: str , type: 'GET', dataType: 'json', cache: false, success: function (data) { if (data === null) return; var snapshots = eval(data); //判断快照是否为空 if (snapshots != null) { var value=0; if(snapshots.length>0) value= snapshots[0].Value; var actVal= GetDital(value,3); context.setAsyncResult(actVal); } }, complete: function (XHR, TS) { XHR = null; } } ); } })(); // 自定义公式(获取快照值)---------------------------------------------------------------------- // 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }