📄 model.js
字号:
// Copyright (c) iRows.com. All rights reserved. Do not copy.function Model(){var M=new Object()M.rows=0M.cols=0M.GE=[]M.GF=[]M.G8=[]M.G9=[]M.images=[]M.parser=new FormulaParser(M)M.lastModified=0M.G4Cells=nullM.ownerId=""M.GA=[]M.GB=[]M.GC=0M.GD=0M.put=function(v){return G2put(M,v)}M.putRow=function(r){return G2putRow(M,r)}M.putCol=function(c){return G2putCol(M,c)}M.remove=function(r,c){return G2remove(M,r,c)}M.setColWidth=function(c,w){G2setColWidth(M,c,w)}M.getColWidth=function(c,defw){return G2getColWidth(M,c,defw)}M.getColsWidth=function(c,defw,bordw){return G2getColsWidth(M,c,defw,bordw)}M.getCell=function(r,c){return G2getCell(M,r,c)}M.getCells=function(fr,fc,tr,tc){return G2getCells(M,fr,fc,tr,tc)}M.getCellAttrs=function(r,c){return G2getCellAttrs(M,r,c)}M.getRow=function(r){return G2getRow(M,r)}M.getCol=function(c){return G2getCol(M,c)}M.cellId=function(cell){return G2cellId(cell)}M.copy=function(sfr,sfc,str,stc,dfr,dfc,dtr,dtc){G2copy(M,sfr,sfc,str,stc,dfr,dfc,dtr,dtc)}M.move=function(sfr,sfc,str,stc,dstr,dstc){G2move(M,sfr,sfc,str,stc,dstr-sfr,dstc-sfc)}M.setOwner=function(o){M.ownerId=o}M.read=function(xmlText){G2read(M,xmlText)}M.init=function(cmds){G2init(M,cmds)}M.toXml=function(){return G2toXml(M)}M.getLastModified=function(){return M.lastModified}M.resetLastModified=function(){M.lastModified=0}M.setModified=function(){M.lastModified=new Date().getTime()}M.delCols=function(fc,tc){G2delCols(M,fc,tc)}M.delRows=function(fr,tr){G2delRows(M,fr,tr)}M.addCols=function(bc,n){G2addCols(M,bc,n)}M.addRows=function(br,n){G2addRows(M,br,n)}M.putImage=function(v,reload){return G2putImage(M,v,reload)}M.removeImage=function(id){G2removeImage(M,id)}M.getImages=function(){return G2getImages(M)}M.getImage=function(ind){return G2getImage(M,ind)}M.getImageByImgId=function(id){return G2getImageByImgId(M,id)}M.sort=function(fr,fc,tr,tc,f1,o1,f2,o2,f3,o3){G2sort(M,fr,fc,tr,tc,f1,o1,f2,o2,f3,o3)}M.getDyn=function(id){return G2getDyn(M,id)}M.G3=function(){var r=M.G4CellsM.G4Cells=nullreturn r}return M}function G2put(M,v){var res={errors:[]}var key='R'+v.row+'C'+v.colM.GE[key]=vM.rows=Math.max(M.rows,v.row)M.cols=Math.max(M.cols,v.col)var ev=G2evaluate(M,v)for(var i=0;i<ev.errors.length;i++){ev.errors[i].row=v.rowev.errors[i].col=v.col}res.errors=ev.errorsv.type=ev.typev.value=ev.valuev.pattern=ev.patternv.formattedValue=ev.formattedValuev.formattedColor=ev.formattedColorv.formulaErrorType=ev.formulaErrorTypev.requires=ev.requiresv.requiresDyns=ev.requiresDynsv.execCode=ev.execCodeif(v.text!=null&&v.text.length>1&&v.text.charAt(0)=='='){M.GF[key]=vvar affected=[]G2addAffected(M,v,affected)if(affected[key]){v.type='text'v.formattedValue='# reference #'v.formulaErrorType='reference'v.requires=[]v.execCode=nullres.errors[res.errors.length]={row:v.row,col:v.col,type:'reference',message:'Circular reference is not allowed',start:0,length:v.text.length}}}else{if(M.GF[key])M.GF[key]=null}var affected=G2affected(M,v)if(affected.length>0){res.affected=affectedfor(var a=0;a<affected.length;a++){var ac=affected[a]var ev=G2evaluate(M,ac)for(var i=0;i<ev.errors.length;i++){ev.errors[i].row=ac.rowev.errors[i].col=ac.colres.errors[res.errors.length]=ev.errors[i]}ac.type=ev.typeac.value=ev.valueac.pattern=ev.patternac.formattedValue=ev.formattedValueac.formattedColor=ev.formattedColorac.formulaErrorType=ev.formulaErrorTypeac.requires=ev.requiresac.requiresDyns=ev.requiresDyns}}G2refreshCharts(M)M.setModified()return res}function G2putRow(M,r){M.G8[r.row]=rM.rows=Math.max(M.rows,r.row)M.setModified()}function G2putCol(M,c){M.G9[c.col]=cM.cols=Math.max(M.cols,c.col)M.setModified()}function G2remove(M,r,c){var res={errors:[]}var key='R'+r+'C'+cvar v=M.GE[key]if(v){M.GE[key]=nullif(M.GF[key])M.GF[key]=nullvar affected=G2affected(M,v)if(affected.length>0){res.affected=affectedfor(var a=0;a<affected.length;a++){var ac=affected[a]var ev=G2evaluate(M,ac)for(var i=0;i<ev.errors.length;i++){ev.errors[i].row=ac.rowev.errors[i].col=ac.colres.errors[res.errors.length]=ev.errors[i]}ac.type=ev.typeac.value=ev.valueac.pattern=ev.patternac.formattedValue=ev.formattedValueac.formattedColor=ev.formattedColorac.formulaErrorType=ev.formulaErrorTypeac.requires=ev.requiresac.requiresDyns=ev.requiresDyns}}G2setRowsCols(M)G2refreshCharts(M)M.setModified()}return res}function G2setColWidth(M,c,w){var r=M.G9[c]if(r)r.width=welseG2putCol(M,{col:c,width:w})M.setModified()}function G2getColWidth(M,c,defw){var w=defwvar r=M.G9[c]if(r&&r.width)w=r.widthreturn w}function G2getColsWidth(M,c,defw,bordw){var res=0for(var i=1;i<=c;i++){var w=G2getColWidth(M,i,defw)res+=w+bordw}return res}function G2getCell(M,r,c){var key='R'+r+'C'+creturn M.GE[key]}function G2getCellAttrs(M,r,c){var key='R'+r+'C'+cvar cell=M.GE[key]var res={}if(cell){for(var p in cell)res[p]=cell[p]}else{res.row=rres.col=c}var col=G2getCol(M,c)if(col){if(!res.color)res.color=col.colorif(!res.bgcolor)res.bgcolor=col.bgcolorif(!res.align)res.align=col.alignif(!res.pattern)res.pattern=col.patternif(!res.style)res.style=col.style}var row=G2getRow(M,r)if(row){if(!res.color)res.color=row.colorif(!res.bgcolor)res.bgcolor=row.bgcolorif(!res.align)res.align=row.alignif(!res.pattern)res.pattern=row.patternif(!res.style)res.style=row.style}return res}function G2getCells(M,fr,fc,tr,tc){var cells=[]for(var p in M.GE){var cell=M.GE[p]if(cell){if(cell.row>=fr&&cell.row<=tr&&cell.col>=fc&&cell.col<=tc)cells[cells.length]=cell}}return cells}function G2getRow(M,r){return M.G8[r]}function G2getCol(M,c){return M.G9[c]}function G2evaluate(M,v){var text=v.textvar res=new Object()res.type='text'res.value=textres.errors=[]var pvalif(text==null||text.length==0)res.type='null'elseif(text.length>1&&text.charAt(0)=='='){var execCode=v.execCodevar fres=M.parser.evaluate(text.substring(1),execCode)res.errors=fres.errorsif(execCode==null){res.requires=fres.requiresres.execCode=fres.execCode}else{res.requires=v.requiresres.execCode=execCode}res.requiresDyns=fres.requiresDynsif(fres.errors.length>0){res.type='text'res.formulaErrorType=fres.errors[0].typeres.formattedValue=fres.errors[0].type=='value'?'# value #':'# error #'}else{res.type=fres.typeres.value=fres.valueres.formulaErrorType=nullres.pattern=v.patternvar pattern=res.patternif(!pattern)pattern=res.pattern=fres.patternif(!pattern&&M.G9[v.col])pattern=M.G9[v.col].patternif(!pattern&&M.G8[v.row])pattern=M.G8[v.row].patternif(res.type=='number'&&pattern)G2formatNumber(res.value,pattern,res)}}elseif((pval=G3(text))){res.type='number'res.value=pval.valueres.pattern=v.patternvar pattern=res.patternif(!pattern)pattern=res.pattern=pval.patternif(!pattern&&M.G9[v.col])pattern=M.G9[v.col].patternif(!pattern&&M.G8[v.row])pattern=M.G8[v.row].patternif(pattern)G2formatNumber(res.value,pattern,res)}elseif(G2testBoolean(text)){res.value=text.toLowerCase()res.type='boolean'}return res}function G2testBoolean(text){return(text.toLowerCase()=='true'||text.toLowerCase()=='false')}function G2cellId(cell){var colIdvar cind=cell.col-1var COL_IDS="ABCDEFGHIJKLMNOPQRSTUVWXYZ"if(cind<COL_IDS.length)colId=COL_IDS.charAt(cind)else{var l1=Math.floor(cind / COL_IDS.length)-1var l2=cind % COL_IDS.lengthcolId=COL_IDS.charAt(l1)+COL_IDS.charAt(l2)}return colId+cell.row}function G2affected(M,v){var affected=[]G2addAffected(M,v,affected)var nextOrder=1var rescan=truewhile(rescan){rescan=falsefor(var i in affected){if(!affected[i].order){var allPredDone=truefor(var p=0;allPredDone&&p<affected[i].pred.length;p++){var pid=affected[i].pred[p]if(affected[pid]&&!affected[pid].order)allPredDone=false}if(allPredDone){affected[i].order=nextOrder++rescan=true}}}}var sorted=[]for(var i in affected){var a=affected[i]sorted[a.order-1]=a.cell}return sorted}function G2addAffected(M,v,affList){for(var fk in M.GF){var f=M.GF[fk]if(f&&f.requires){var isDep=falsefor(var r=0;!isDep&&r<f.requires.length;r++){var req=f.requires[r]if(req.row)isDep=(v.row==req.row&&v.col==req.col)elseisDep=(v.row>=req.startRow&&v.col>=req.startCol&&v.row<=req.endRow&&v.col<=req.endCol)}if(isDep){var aid="R"+f.row+"C"+f.colvar vid="R"+v.row+"C"+v.colif(affList[aid]){affList[aid].pred[affList[aid].pred.length]=vid}else{affList[aid]={cell:f,pred:[vid]}G2addAffected(M,f,affList)}}}}}function G2getAttributes(node,target){target.color=node.getAttribute("color")target.bgcolor=node.getAttribute("bgcolor")target.align=node.getAttribute("align")target.style=node.getAttribute("style")var p=node.getAttribute("pattern")if(p)target.pattern=unescape(p)}function G2read(M,xmlText){if(xmlText.length==0)returnvar xmlDocif(window.ActiveXObject){xmlDoc=new ActiveXObject("Microsoft.XMLDOM")xmlDoc.async="false"xmlDoc.loadXML(xmlText)}elseif(document.implementation&&document.implementation.createDocument){xmlDoc=(new DOMParser()).parseFromString(xmlText,"text/xml")}if(xmlDoc){M.rows=0M.cols=0M.GE=[]M.GF=[]M.G8=[]M.G9=[]M.images=[]var rows=xmlDoc.getElementsByTagName("row")for(var i=0;i<rows.length;i++){var cn=rows[i]var r=parseInt(cn.getAttribute("number"))var v={row:r}G2getAttributes(cn,v)G2putRow(M,v)}var cols=xmlDoc.getElementsByTagName("column")for(var i=0;i<cols.length;i++){var cn=cols[i]var c=parseInt(cn.getAttribute("number"))var w=nullif(cn.getAttribute("width"))w=parseInt(cn.getAttribute("width"))if(w!=null&&(isNaN(w)||w<20))w=20var v={col:c,width:w}G2getAttributes(cn,v)G2putCol(M,v)}var cells=xmlDoc.getElementsByTagName("cell")for(var i=0;i<cells.length;i++){var cn=cells[i]var r=parseInt(cn.getAttribute("row"))var c=parseInt(cn.getAttribute("column"))var text=unescape(cn.getAttribute("text"))var v={row:r,col:c,text:text}G2getAttributes(cn,v)G2put(M,v)}var images=xmlDoc.getElementsByTagName("image")for(var i=0;i<images.length;i++){var cn=images[i]var r=parseInt(cn.getAttribute("row"))var c=parseInt(cn.getAttribute("column"))var offX=parseInt(cn.getAttribute("offsetX"))var offY=parseInt(cn.getAttribute("offsetY"))var imo={row:r,col:c,offX:offX,offY:offY}if(cn.getAttribute("chart")){imo.chart=cn.getAttribute("chart").toLowerCase()var w=nullif(cn.getAttribute("width"))w=parseInt(cn.getAttribute("width"))var h=nullif(cn.getAttribute("height"))h=parseInt(cn.getAttribute("height"))if(w!=null&&!isNaN(w)&&w>=60)imo.width=wif(h!=null&&!isNaN(h)&&h>=60)imo.height=himo.fromRow=parseInt(cn.getAttribute("fromRow"))imo.fromCol=parseInt(cn.getAttribute("fromCol"))imo.toRow=parseInt(cn.getAttribute("toRow"))imo.toCol=parseInt(cn.getAttribute("toCol"))imo.incols=cn.getAttribute("incols")=="y"imo.legend=cn.getAttribute("legend")=="y"imo.d3=cn.getAttribute("d3")=="y"if(cn.getAttribute("title"))imo.title=unescape(cn.getAttribute("title"))if(cn.getAttribute("titlex"))imo.titlex=unescape(cn.getAttribute("titlex"))if(cn.getAttribute("titley"))imo.titley=unescape(cn.getAttribute("titley"))if(cn.getAttribute("stacked"))imo.stacked=true}elseimo.url=unescape(cn.getAttribute("url"))G2putImage(M,imo,true)}}M.setModified()}function Mu(t){return unescape(t)}function Mc(M,c){M.G9[c.col]=c}function Mr(M,r){M.G8[r.row]=r}function Mv(M,v){var key='R'+v.row+'C'+v.colM.GE[key]=vif(v.text!=null&&v.text.length>1&&v.text.charAt(0)=='=')M.GF[key]=vif(v.sdyns){v.requiresDyns=[]for(var i=0;i<v.sdyns.length;i++)v.requiresDyns[v.sdyns[i]]=1v.sdyns=null}}function Mi(M,img){G2putImage(M,img,true)}function Md(M,id,v){M.GA[id]=v}function G2init(M,cmds){if(cmds.length>0)eval(cmds)G2setRowsCols(M)}function G2attributesToXml(e){var t=""if(e.color)t+="color='"+e.color+"' "if(e.bgcolor)t+="bgcolor='"+e.bgcolor+"' "if(e.align)t+="align='"+e.align+"' "if(e.style)t+="style='"+e.style+"' "if(e.pattern)t+="pattern='"+escape(e.pattern)+"' "return t}function G2toXml(M){var xml=""xml+="<sheet>"xml+="<rows>"for(var i=0;i<M.G8.length;i++){var row=M.G8[i]if(row){var attrs=G2attributesToXml(row)if(attrs!="")xml+="<row number='"+row.row+"' "+attrs+"/>"}}xml+="</rows>"xml+="<columns>"for(var i=0;i<M.G9.length;i++){var col=M.G9[i]if(col){var attrs=G2attributesToXml(col)if(attrs!=""||col.width){xml+="<column number='"+col.col+"' "if(col.width)xml+="width='"+col.width+"' "xml+=attrsxml+="/>"}}}xml+="</columns>"xml+="<cells>"for(var p in M.GE){var cell=M.GE[p]if(cell!=null&&cell.text==""&&!cell.color&&!cell.bgcolor&&!cell.align&&!cell.style&&!cell.pattern)cell=nullif(cell){xml+="<cell row='"+cell.row+"' column='"+cell.col+"' text='"+escape(cell.text)+"' "xml+=G2attributesToXml(cell)xml+="/>"}}xml+="</cells>"if(M.images.length>0){xml+="<images>"for(var i=0;i<M.images.length;i++){var imo=M.images[i]if(imo){xml+="<image row='"+imo.row+"' column='"+imo.col+"' offsetX='"+imo.offX+"' offsetY='"+imo.offY+"' "if(imo.chart){xml+="chart='"+imo.chart+"' fromRow='"+imo.fromRow+"' toRow='"+imo.toRow+"' fromCol='"+imo.fromCol+"' toCol='"+imo.toCol+"' incols='"+(imo.incols?"y":"n")+"' "if(imo.width)xml+="width='"+imo.width+"' "if(imo.height)xml+="height='"+imo.height+"' "if(imo.title)xml+="title='"+escape(imo.title)+"' "xml+="legend='"+(imo.legend?"y":"n")+"' "xml+="d3='"+(imo.d3?"y":"n")+"' "if(imo.titlex)xml+="titlex='"+escape(imo.titlex)+"' "if(imo.titley)xml+="titley='"+escape(imo.titley)+"' "if(imo.stacked)xml+="stacked='y' "}else{xml+="url='"+escape(imo.url)+"' "}xml+="/>"}}xml+="</images>"}xml+="</sheet>"return xml}var sh_pref_format_sep=','var sh_pref_format_decpoint='.'var sh_pref_format_currency='$'function G2formatNumber(value,pattern,res){if(!pattern)returnif(pattern=='n:hex'){res.formattedValue=G2hex(value)return}if(pattern=='n:bin'){res.formattedValue=G2bin(value)return}if(pattern.length!=7||pattern.substring(0,2)!='n:')returnvar useSep=pattern.charAt(2)=='y'var decimalDigits=parseInt(pattern.substring(3,5),10)var symbol=pattern.charAt(5)var negativeFormat=pattern.charAt(6)if(symbol=='%')value *=100var factor=Math.pow(10,decimalDigits)value=Math.round(value * factor)/ factor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -