123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880 |
- /**
- * Created by Tony on 2017/4/28.
- */
- var sheetCommonObj = {
- // CSL.2017.06.05
- // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
- createSpread: function(container, SheetCount){
- var me = this;
- var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: SheetCount });
- spreadBook.options.tabStripVisible = false;
- spreadBook.options.showHorizontalScrollbar = true;
- spreadBook.options.showVerticalScrollbar = true;
- spreadBook.options.allowCopyPasteExcelStyle = false;
- spreadBook.options.allowUserDragDrop = true;
- spreadBook.options.allowContextMenu = false;
- spreadBook.options.allowUserEditFormula = false;
- spreadBook.options.showDragFillSmartTag = false;
- return spreadBook;
- },
- initSheet: function(sheet, setting, rowCount) {
- var me = this;
- var spreadNS = GC.Spread.Sheets;
- sheet.suspendPaint();
- sheet.suspendEvent();
- if(setting.frozenCols) sheet.frozenColumnCount(setting.frozenCols);//冻结列
- sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
- sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
- if (setting && setting.view && setting.view.colHeaderHeight) {
- sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
- };
- if (setting && setting.view && setting.view.rowHeaderWidth) {
- sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
- };
- sheet.options.colHeaderAutoTextIndex = 1;
- sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
- sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
- sheet.options.protectionOptions = {
- allowResizeColumns: true
- };
- sheet.showRowOutline(false);
- sheet.options.allowCellOverflow = false;
- me.buildHeader(sheet, setting);
- if (rowCount > 0)
- sheet.setRowCount(rowCount);
- else
- sheet.setRowCount(1);
- sheet.resumeEvent();
- sheet.resumePaint();
- },
- // buildSheet 在一个Spread、一个Sheet的情况下使用。
- buildSheet: function(container, setting, rowCount) {
- var me = this;
- var spreadBook = me.createSpread(container, { sheetCount: 1 });
- var sheet = spreadBook.getSheet(0);
- me.initSheet(sheet, setting, rowCount);
- return spreadBook;
- },
- buildHeader: function(sheet, setting){
- var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
- for (var i = 0; i < setting.header.length; i++) {
- sheet.setValue(0, i, setting.header[i].headerName, ch);
- sheet.getCell(0, i, ch).wordWrap(true);
- sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
- sheet.setColumnVisible(i,setting.header[i].visible === false ? false:true);
- }
- },
- cleanSheet: function(sheet, setting, rowCount) {
- sheet.suspendPaint();
- sheet.suspendEvent();
- sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
- if (rowCount > 0) sheet.setRowCount(rowCount);
- sheet.clearSelection();
- sheet.resumeEvent();
- sheet.resumePaint();
- },
- cleanData: function (sheet, setting, rowCount) {
- sheet.suspendPaint();
- sheet.suspendEvent();
- sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
- if (rowCount > 0) sheet.setRowCount(rowCount);
- sheet.resumeEvent();
- sheet.resumePaint();
- },
- setAreaAlign: function(area, hAlign, vAlign){
- if (!(hAlign) || hAlign === "left") {
- area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
- } else if (hAlign === "right") {
- area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
- } else if (hAlign === "center") {
- area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
- } else {
- area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
- }
- if (!(vAlign) || vAlign === "center") {
- area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
- } else if (vAlign === "top") {
- area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
- } else if (vAlign === "bottom") {
- area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
- } else {
- area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
- }
- },
- showData: function(sheet, setting, data,distTypeTree) {
- var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
- sheet.suspendPaint();
- sheet.suspendEvent();
- //sheet.addRows(row, 1);
- sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
- if(sheet.getRowCount()<data.length){
- data.length<30? sheet.setRowCount(30):sheet.setRowCount(data.length);
- }else if(sheet.getRowCount()==0){
- sheet.setRowCount(30);
- }
- for (var col = 0; col < setting.header.length; col++) {
- var hAlign = "left", vAlign = "center";
- if (setting.header[col].hAlign) {
- hAlign = setting.header[col].hAlign;
- } else if (setting.header[col].dataType !== "String"){
- hAlign = "right";
- }
- vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
- me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
- if (setting.header[col].formatter) {
- sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
- }
- if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
- var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
- sheet.deleteColumns(col,1);
- sheet.addColumns(col, 1);
- sheet.setValue(0, col, setting.header[col].headerName, header);
- sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
- }
- if(setting.header[col].visible === false){
- sheet.setColumnVisible(col,false);
- }
- sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
- }
- for (var row = 0; row < data.length; row++) {
- //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
- this.showRowData(sheet,setting,row,data,distTypeTree);
- if(setting.getStyle && setting.getStyle(data[row])){
- sheet.setStyle(row, -1, setting.getStyle(data[row]));
- }
- }
- this.lockCells(sheet,setting);
- sheet.resumeEvent();
- sheet.resumePaint();
- //me.shieldAllCells(sheet);
- },
- showRowData:function (sheet,setting,row,data,distTypeTree=null) {
- let ch = GC.Spread.Sheets.SheetArea.viewport;
- for (var col = 0; col < setting.header.length; col++) {
- //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
- var val = data[row][setting.header[col].dataCode];
- if(val&&setting.header[col].dataType === "Number"){
- if(setting.header[col].hasOwnProperty('tofix')){
- val =scMathUtil.roundToString(val,setting.header[col].tofix);
- }
- else if(setting.header[col].hasOwnProperty('decimalField')){
- var decimal = getDecimal(setting.header[col].decimalField);
- val =scMathUtil.roundToString(val,decimal);
- sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
- }else {
- val =val+'';
- }
- }
- if(val!=null&&setting.header[col].cellType === "checkBox"){
- this.setCheckBoxCell(row,col,sheet,val)
- }
- if(setting.header[col].cellType === "comboBox"){
- this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType);
- }
- if(setting.header[col].cellType === "selectButton"){
- this.setSelectButton(row,col,sheet,setting.header[col]);
- }
- if(setting.header[col].cellType === "replaceButton"){
- this.setReplaceButton(row,col,sheet,setting.header[col]);
- }
- if(setting.header[col].cellType === "tipsCell"){
- this.setTipsCell(row,col,sheet,setting.header[col]);
- }
- if(setting.owner==='gljTree'){
- if(setting.header[col].cellType === "checkBox"){
- val==1?val:0;
- this.setCheckBoxCell(row,col,sheet,val);
- }
- if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
- let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
- val=distTypeVal;
- }
- }
- if(setting.header[col].getText){
- val = setting.getText[setting.header[col].getText](data[row],val)
- }
- sheet.setValue(row, col, val, ch);
- }
- this.setRowStyle(row,sheet,data[row].bgColour);
- if(setting.autoFit==true){
- sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
- sheet.autoFitRow(row);
- }
- },
- //todo
- analyzePasteData: function(setting, pastedInfo) {
- var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
- for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
- if (pastedInfo.pasteData.text[i] === "\n") {
- propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
- preStrIdx = i + 1;
- rst.push(itemObj);
- if (i < pastedInfo.pasteData.text.length - 1) {
- itemObj = {};
- }
- } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
- itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
- propId++;
- preStrIdx = i + 1;
- //if the last copied-cell were empty, should check whether the end of text
- if (i == pastedInfo.pasteData.text.length - 1) {
- itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
- rst.push(itemObj);
- }
- } else if (i == pastedInfo.pasteData.text.length - 1) {
- itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
- rst.push(itemObj);
- }
- }
- return rst;
- },
- combineRowData: function(sheet, setting, row) {
- var rst = {};
- for (var col = 0; col < setting.header.length; col++) {
- rst[setting.header[col].dataCode] = sheet.getValue(row, col);
- }
- return rst;
- },
- shieldAllCells: function(sheet) {
- sheet.options.isProtected = true;
- },
- unShieldAllCells: function(sheet) {
- sheet.options.isProtected = false;
- },
- lockCells: function(sheet, setting){
- if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
- sheet.options.isProtected = true;
- sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
- for (var i = 0; i < setting.view.lockColumns.length; i++) {
- sheet.getRange(-1,setting.view.lockColumns[i], -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
- }
- }
- },
- setCheckBoxCell(row,col,sheet,val){
- var c = new GC.Spread.Sheets.CellTypes.CheckBox();
- c.isThreeState(false);
- sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
- sheet.getCell(row, col).value(val);
- sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
- },
- setComboBox(row,col,sheet,options,editorValueType){
- //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
- let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
- if(options){
- dynamicCombo.itemHeight(options.length).items(options);
- if(editorValueType==true){
- dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
- }
- }
- sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
- },
- setRowStyle(row,sheet,bgColour) {
- if(bgColour){
- let style = new GC.Spread.Sheets.Style();
- style.backColor = bgColour;
- style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
- style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
- style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
- style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
- sheet.setStyle(row, -1, style);
- }
- },
- getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
- let me = this;
- function CustomerCoeCellType() {
- this.isEscKey=false;
- this.displayText='';
- }
- CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
- CustomerCoeCellType.prototype.createEditorElement = function (context) {
- console.log("create editor")
- let element = document.createElement("div");//这里创建的,会自动销毁
- return element
- };
- CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
- if (editorContext) {
- $editor = $(editorContext);
- $editor.css("position", "fixed");
- $editor.css("background", "white");
- $editor.css("width", cellRect.width);
- $editor.attr("gcUIElement", "gcEditingInput");
- if(htmlGenerator) htmlGenerator(context,cellRect,$editor);
- }
- }
- CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
- };
- CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
- console.log("set editor value");
- this.displayText = value;
- };
- CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
- console.log("get value");
- if(this.isEscKey !=true&& updateCallback){
- updateCallback();
- }
- this.isEscKey = false;
- return this.displayText;
- };
- CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
- console.log(" update editor");
- if( setEditorValue){//不是esc时才更新
- setEditorValue(context);
- }
- };
- CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
- //cell type handle tab key by itself
- this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
- return false;
- };
- return new CustomerCoeCellType();
- },
- scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
- let topRow = sheet.getViewportTopRow(1);
- if(row == topRow) return;//已经是最顶行了
- let length = options&&options.length>0?options.length:1;
- let height = cxt.canvas.height;
- let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
- let endY = startY + length * cellRect.height;//下拉框的结束显示位置
- if(endY <= height) return; //如果没有超出显示位置,直接返回
- let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
- let showRow = topRow + overRow > row?row:topRow + overRow;
- sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
- },
- setSelectButton(row,col,sheet,header){
- let getSelectButton = function (cellWidth=100) {
- function moreButton() {
- }
- moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
- moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
- GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
- let buttonW = cellWidth/5;
- let endX = x+w-2;
- if(value){
- let textWidth = ctx.measureText(value).width;
- let spaceWidth = cellWidth - buttonW;
- let textEndX = x+2+textWidth;
- if(spaceWidth<textWidth){
- for(let i = value.length-1;i>1;i--){
- let newValue = value.substr(0,i);
- let newTestWidth = ctx.measureText(newValue).width;
- if(spaceWidth>newTestWidth){
- value = newValue;
- textEndX = x+2+newTestWidth;
- break;
- }
- }
- }
- ctx.fillText(value,textEndX,y+h-5);
- }
- //画三个点
- ctx.save();
- ctx.beginPath();
- ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
- ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
- ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
- ctx.fillStyle="black";//填充颜色,默认是黑色
- ctx.fill();//画实心圆
- ctx.closePath();
- ctx.restore();
- };
- moreButton.prototype.processMouseLeave= function (hitinfo) {
- let newCell = new selectButton();
- hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
- hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
- };
- function selectButton() {
- }
- selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
- selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
- GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
- };
- selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
- return {
- x: x,
- y: y,
- row: context.row,
- col: context.col,
- cellStyle: cellStyle,
- cellRect: cellRect,
- sheetArea: context.sheetArea
- };
- };
- selectButton.prototype.processMouseDown = function (hitinfo){
- if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
- let b1 = new moreButton();
- b1.marginLeft(cellWidth*4/5);
- hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
- hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
- }
- };
- return new selectButton();
- };
- sheet.setCellType(row, col,getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
- },
- setReplaceButton(row,col,sheet){
- let replaceButton = function(){
- };
- replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
- replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
- GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this,arguments);
- if(value){
- ctx.save();
- ctx.fillStyle = "white";
- let fh = options.fontInfo&&options.fontInfo.fontSize?options.fontInfo.fontSize:h-6;
- ctx.fillText(value,x+(w+ctx.measureText(value).width)/2,y+(h+fh)/2-2);
- ctx.restore();
- }
- };
- let cellType = new replaceButton();
- cellType.buttonBackColor("#07A0FF");
- sheet.setCellType(row, col,cellType,GC.Spread.Sheets.SheetArea.viewport);
- },
- setTipsCell(row,col,sheet,header){
- let TipCellType = function () {};
- TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
- TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
- return {
- x: x,
- y: y,
- row: context.row,
- col: context.col,
- cellStyle: cellStyle,
- cellRect: cellRect,
- sheet: context.sheet,
- sheetArea: context.sheetArea
- };
- };
- TipCellType.prototype.processMouseEnter = function (hitinfo) {
- let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
- let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
- let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
- let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
- zoom = hitinfo.sheet.zoom();
- let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
- let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
- let setting = {};
- if(textLength <= cellWidth){
- return;
- }
- if(sheet && sheet.getParent().qo){
- setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
- }
- TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
- };
- TipCellType.prototype.processMouseLeave = function (hitinfo) {
- TREE_SHEET_HELPER.tipDiv = 'hide';
- if (TREE_SHEET_HELPER._toolTipElement) {
- $(TREE_SHEET_HELPER._toolTipElement).hide();
- TREE_SHEET_HELPER._toolTipElement = null;
- };
- TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
- };
- sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
- },
- chkIfEmpty: function(rObj, setting) {
- var rst = true;
- if (rObj) {
- for (var i = 0; i < setting.header.length; i++) {
- if (rObj[setting.header[i].dataCode]) {
- rst = false;
- break;
- }
- }
- }
- return rst;
- },
- //add by zhong 2017-10-10
- //动态下拉框,配合EnterCell, args.sheet.repaint();
- getDynamicCombo: function (forLocked) {
- let ComboCellForActiveCell = function () { };
- ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
- ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
- let sheet = options.sheet;
- if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
- GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
- } else {
- GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
- }
- };
- ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
- let sheet = options.sheet;
- if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
- return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
- } else {
- return {
- x: x,
- y: y,
- row: options.row,
- col: options.col,
- cellStyle: cellStyle,
- cellRect: cellRect,
- sheetArea: options.sheetArea
- };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
- }
- };
- return new ComboCellForActiveCell();
- },
- getTipsCombo:function (forLocked,tips,setting,node) {
- let getTipsCombo = function () {
- this.clickCom=false;
- };
- getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
- if(tips && tips !=""){
- getTipsCombo.prototype.processMouseEnter = function(hitinfo){
- if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
- this.clickCom = false;
- return;
- }
- let text = typeof tips == 'function'?tips(node):tips;
- TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,text);
- };
- getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
- TREE_SHEET_HELPER.hideTipsDiv();
- };
- getTipsCombo.prototype.processMouseDown = function (hitinfo){
- if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
- TREE_SHEET_HELPER.hideTipsDiv();
- this.clickCom = true;
- }
- GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
- };
- getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
- TREE_SHEET_HELPER.hideTipsDiv();
- GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
- };
- }
- return new getTipsCombo();
- },
- setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
- let me = this;
- sheet.suspendPaint();
- let combo = me.getDynamicCombo();
- for(let i = 0, len = rowCount; i < len; i++){
- if(itemsHeight) {
- combo.itemHeight(itemsHeight);
- combo._maxDropDownItems = itemsHeight + 5;
- }
- if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
- else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
- else combo.items(items);
- sheet.getCell(beginRow + i, col).cellType(combo);
- }
- sheet.resumePaint();
- },
- setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
- sheet.suspendPaint();
- let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
- for(let i = 0, len = rowCount; i < len; i++){
- if(itemsHeight) combo.itemHeight(itemsHeight);
- if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
- else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
- else combo.items(items);
- sheet.getCell(beginRow + i, col).cellType(combo);
- }
- sheet.resumePaint();
- },
- //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
- copyTextToClipboard: function(text) {
- let textArea = document.createElement("textarea");
- textArea.style.position = 'fixed';
- textArea.style.top = 0;
- textArea.style.left = 0;
- textArea.style.width = '2em';
- textArea.style.height = '2em';
- textArea.style.padding = 0;
- textArea.style.border = 'none';
- textArea.style.outline = 'none';
- textArea.style.boxShadow = 'none';
- textArea.style.background = 'transparent';
- textArea.value = text;
- document.body.appendChild(textArea);
- textArea.select();
- try {
- let successful = document.execCommand('copy');
- let msg = successful ? 'successful' : 'unsuccessful';
- console.log('Copying text command was ' + msg);
- } catch (err) {
- console.log('Oops, unable to copy');
- }
- document.body.removeChild(textArea);
- },
- //获取选中区域的表格类型数据(可粘贴到excel)
- getTableData: function (sheet, colSettings = null) {
- let rst = '';
- let sel = sheet.getSelections()[0];
- let pasteText = [];
- for(let row = sel.row; row < sel.row + sel.rowCount; row++){
- if(!sheet.getCell(row, -1).visible())
- continue;
- let rowText = [];
- for(let j = 0; j < sel.colCount; j++){
- let col = sel.col + j;
- if(!sheet.getCell(-1, col).visible())
- continue;
- if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
- rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
- else
- rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
- }
- pasteText.push(rowText.join('\t'));
- }
- return pasteText.join('\n');
- },
- transferToTreeSetting:function(setting,treeSetting,treeCol){
- for(let h of setting.header){
- treeSetting.cols.push(getSettingCol(h))
- }
- for(let l of setting.view.lockColumns){
- treeSetting.cols[l].readOnly = true;
- }
- return treeSetting;
- function getSettingCol(header) {
- let aMap ={left:0,center:1,right:2};
- let hAlign = header.hAlign?aMap[header.hAlign]:0;
- let col = {
- "width":header.headerWidth?header.headerWidth:100,
- "head":{
- "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
- "spanCols":header.spanCols?header.spanCols:[1],
- "spanRows":header.spanRows?header.spanRows:[1],
- "vAlign":[1],
- "hAlign":[1],
- "font":["Arial"]
- },
- "data": {
- "field": header.dataCode,
- "vAlign": 1,
- "hAlign": hAlign,
- "font": "Arial"
- }
- };
- if(header.showHint == true){
- col.showHint = true;
- }
- if(header.cellType){
- col.data.cellType = getCellType(header);
- }
- if(header.decimalField){//设置formatter
- let decimal = getDecimal(header.decimalField);
- col.formatter = getFormatter(decimal);
- }
- if(header.getText && treeCol){
- col.data.getText = treeCol.getEvent(header.getText);
- }
- /*col.readOnly = function (node) {
- if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
- return true;
- }
- return false;
- };*/
- return col;
- }
- function getCellType(header) {
- return function () {
- if(header.cellType === "checkBox"){
- return new GC.Spread.Sheets.CellTypes.CheckBox();
- }
- if(header.cellType === "comboBox"){
- let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
- if(header.options){
- dynamicCombo.itemHeight(header.options.length).items(header.options);
- if(header.editorValueType==true){
- dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
- }
- }
- return dynamicCombo
- }
- }
- }
- },
- //注册自定义回车键事件
- bindEnterKey: function (workBook, operation) {
- workBook.commandManager().register('myEnter', operation);
- workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
- workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
- },
- //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
- bindEscKey: function (workBook, sheets) {
- function isDef(v){
- return typeof v !== 'undefined' && v !== null;
- }
- workBook.commandManager().register('myEsc', function () {
- let activeSheet = workBook.getActiveSheet();
- let hasTheSheet = false;
- for(let sheetObj of sheets){
- let sheet = sheetObj.sheet;
- if(sheet === activeSheet){
- hasTheSheet = true;
- let editStarting = sheetObj.editStarting;
- let editEnded = sheetObj.editEnded;
- if(editStarting){
- sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
- }
- if(editEnded){
- sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
- }
- let row = sheet.getActiveRowIndex();
- let col = sheet.getActiveColumnIndex();
- let orgV = sheet.getValue(row, col);
- let orgText = sheet.getText(row, col);
- if(!isDef(orgV)){
- orgV = '';
- }
- if(sheet.isEditing()){
- sheet.endEdit();
- sheet.setValue(row, col, orgV);
- }
- if(editStarting){
- sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
- }
- if(editEnded){
- sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
- }
- }
- }
- //容错处理,以防没把所有工作簿的表格信息传入参数
- if(!hasTheSheet){
- if(activeSheet.isEditing()){
- activeSheet.endEdit();
- }
- }
- });
- workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
- workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
- },
- //设置默认样式
- spreadDefaultStyle: function (workBook) {
- let defaultStyle = new GC.Spread.Sheets.Style();
- defaultStyle.font = '14px Calibri';
- let sheetCount = workBook.getSheetCount();
- for(let i = 0; i < sheetCount; i++){
- let sheet = workBook.getSheet(i);
- sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
- sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
- sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
- }
- },
- //动态根据工作簿宽度和各列宽度比例设置宽度
- setColumnWidthByRate: function (workBookWidth, workBook, headers){
- if(workBook){
- const sheet = workBook.getActiveSheet();
- sheet.suspendEvent();
- sheet.suspendPaint();
- for(let col = 0; col < headers.length; col++){
- if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
- sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
- }
- else {
- if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
- sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
- }
- }
- }
- sheet.resumeEvent();
- sheet.resumePaint();
- }
- },
- drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
- ctx.save();
- ctx.strokeStyle = "gray";
- ctx.translate(0.5, 0.5);
- ctx.beginPath();
- let rectX = x + margin;
- let rectY = y + Math.round(h / 2) - rectH / 2;
- ctx.moveTo(rectX, rectY);
- ctx.lineTo(rectX, rectY + rectH);
- ctx.lineTo(rectX + rectW, rectY + rectH);
- ctx.lineTo(rectX + rectW, rectY);
- ctx.lineTo(rectX, rectY);
- ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
- ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
- ctx.stroke();
- ctx.restore();
- },
- drawLine : function (ctx, x1, y1, x2, y2, color) {
- let l_color = color?color:"gray";
- ctx.save();
- ctx.translate(0.5, 0.5);
- ctx.beginPath();
- ctx.moveTo(x1, y1);
- ctx.lineTo(x2, y2);
- ctx.strokeStyle = l_color;
- ctx.stroke();
- ctx.restore();
- },
- drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
- ctx.save();
- ctx.strokeStyle = "#000000";
- ctx.translate(0.5, 0.5);
- ctx.beginPath();
- ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
- ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
- let rectY = y + Math.round(h / 2) - rectH / 2;
- if (collapsed) {
- ctx.moveTo(x + margin + rectW / 2, rectY + 2);
- ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
- }
- ctx.stroke();
- ctx.restore();
- },
- drowTriangle:function(ctx,x,y){//画向下三角形
- ctx.save();
- ctx.fillStyle = "black";
- ctx.beginPath();
- ctx.moveTo(x, y);
- ctx.lineTo(x-3, y -6);
- ctx.lineTo(x+3, y -6);
- ctx.fill();
- ctx.restore();
- },
- drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
- let t_step = step?step:6;
- offset += t_step;
- ctx.save();
- ctx.strokeStyle = "gray";
- ctx.translate(0.5, 0.5);
- ctx.beginPath();
- ctx.moveTo(x + offset, y);
- ctx.lineTo(x + offset, y + Math.round(h / 2));
- offset += 9;
- ctx.lineTo(x + offset, y + Math.round(h / 2));
- if (hasNext) {
- ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
- ctx.lineTo(x + offset - 9, y + h);
- }
- ctx.stroke();
- ctx.restore();
- return offset;
- }
- }
|