sheet_common.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var sheetCommonObj = {
  5. // CSL.2017.06.05
  6. // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
  7. createSpread: function(container, SheetCount){
  8. var me = this;
  9. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: SheetCount });
  10. spreadBook.options.tabStripVisible = false;
  11. spreadBook.options.showHorizontalScrollbar = true;
  12. spreadBook.options.showVerticalScrollbar = true;
  13. spreadBook.options.allowCopyPasteExcelStyle = false;
  14. spreadBook.options.allowUserDragDrop = true;
  15. spreadBook.options.allowContextMenu = false;
  16. spreadBook.options.allowUserEditFormula = false;
  17. spreadBook.options.showDragFillSmartTag = false;
  18. return spreadBook;
  19. },
  20. initSheet: function(sheet, setting, rowCount) {
  21. var me = this;
  22. var spreadNS = GC.Spread.Sheets;
  23. if(setting.headRows == 2){
  24. return me.buildSpanHeader(sheet, setting, rowCount);//初始化合并表格列头
  25. }
  26. sheet.suspendPaint();
  27. sheet.suspendEvent();
  28. if(setting.frozenCols) sheet.frozenColumnCount(setting.frozenCols);//冻结列s
  29. sheet.setRowCount(setting.headRows ? setting.headRows : 1, spreadNS.SheetArea.colHeader);
  30. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  31. if (setting && setting.view && setting.view.colHeaderHeight) {
  32. sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
  33. };
  34. if (setting && setting.view && setting.view.rowHeaderWidth) {
  35. sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
  36. };
  37. if (setting.emptyRowHeader) {
  38. sheet.setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  39. }
  40. sheet.options.colHeaderAutoTextIndex = 1;
  41. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  42. //复制粘贴的时候只粘贴值,忽略公式等
  43. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  44. sheet.options.protectionOptions = {
  45. allowResizeColumns: true//列宽可拖动
  46. };
  47. sheet.showRowOutline(false);
  48. sheet.options.allowCellOverflow = false;
  49. me.buildHeader(sheet, setting);
  50. sheet.resumeEvent();
  51. sheet.resumePaint();
  52. },
  53. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  54. buildSheet: function(container, setting, rowCount) {
  55. var me = this;
  56. var spreadBook = me.createSpread(container, { sheetCount: 1 });
  57. var sheet = spreadBook.getSheet(0);
  58. me.initSheet(sheet, setting, rowCount);
  59. return spreadBook;
  60. },
  61. buildSpanHeader:function (sheet, setting, rowCount) {
  62. let temSetting = {
  63. "emptyRows":0,
  64. "headRows":2,
  65. "headRowHeight":[21],
  66. "defaultRowHeight": 21,
  67. "treeCol": 0,
  68. "cols":[]
  69. };
  70. let spanSetting = sheetCommonObj.transferToTreeSetting(setting,temSetting);
  71. TREE_SHEET_HELPER.loadSheetHeader(spanSetting,sheet,rowCount);
  72. sheet.options.protectionOptions = {
  73. allowResizeColumns: true//列宽可拖动
  74. };
  75. },
  76. buildHeader: function(sheet, setting){
  77. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  78. for (var i = 0; i < setting.header.length; i++) {
  79. sheet.setValue(0, i, setting.header[i].headerName, ch);
  80. sheet.getCell(0, i, ch).wordWrap(true);
  81. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  82. sheet.setColumnVisible(i,setting.header[i].visible === false ? false:true);
  83. }
  84. },
  85. getHeadersFromTreeSetting: function (treeSetting) {
  86. const vAlignMap = ['top', 'center', 'bottom'];
  87. const hAlignMap = ['left', 'center', 'right'];
  88. return treeSetting.cols.map(item => {
  89. const vAlign = vAlignMap[item.data.vAlign];
  90. const hAlign = hAlignMap[item.data.hAlign];
  91. return {
  92. name: item.head.titleNames[0],
  93. dataCode: item.data.field,
  94. width: item.width,
  95. vAlign,
  96. hAlign,
  97. };
  98. });
  99. },
  100. setHeader: function(sheet, headers) {
  101. sheet.suspendPaint();
  102. sheet.suspendEvent();
  103. sheet.setColumnCount(headers.length);
  104. sheet.setRowHeight(0, 30, GC.Spread.Sheets.SheetArea.colHeader);
  105. headers.forEach((header, index) => {
  106. sheet.setValue(0, index, header.name, GC.Spread.Sheets.SheetArea.colHeader);
  107. sheet.setColumnWidth(index, header.width, GC.Spread.Sheets.SheetArea.colHeader);
  108. if (header.formatter) {
  109. sheet.setFormatter(-1, index, header.formatter);
  110. }
  111. sheet.getRange(-1, index, -1, 1).hAlign(GC.Spread.Sheets.HorizontalAlign[header.hAlign]);
  112. sheet.getRange(-1, index, -1, 1).vAlign(GC.Spread.Sheets.VerticalAlign[header.vAlign]);
  113. });
  114. sheet.resumeEvent();
  115. sheet.resumePaint();
  116. },
  117. cleanSheet: function(sheet, setting, rowCount) {
  118. sheet.suspendPaint();
  119. sheet.suspendEvent();
  120. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  121. if (rowCount > 0) sheet.setRowCount(rowCount);
  122. sheet.clearSelection();
  123. sheet.resumeEvent();
  124. sheet.resumePaint();
  125. },
  126. cleanData: function (sheet, setting, rowCount) {
  127. sheet.suspendPaint();
  128. sheet.suspendEvent();
  129. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  130. if (rowCount > 0) sheet.setRowCount(rowCount);
  131. sheet.resumeEvent();
  132. sheet.resumePaint();
  133. },
  134. setAreaAlign: function(area, hAlign, vAlign){
  135. if (!(hAlign) || hAlign === "left") {
  136. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  137. } else if (hAlign === "right") {
  138. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  139. } else if (hAlign === "center") {
  140. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  141. } else {
  142. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  143. }
  144. if (!(vAlign) || vAlign === "center") {
  145. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  146. } else if (vAlign === "top") {
  147. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  148. } else if (vAlign === "bottom") {
  149. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  150. } else {
  151. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  152. }
  153. },
  154. showTreeData:function (sheet,setting,data) {
  155. let ch = GC.Spread.Sheets.SheetArea.viewport;
  156. let parentMap=_.groupBy(data, 'ParentID');
  157. let visibleMap = {};
  158. let styleRow=[];
  159. sheet.suspendPaint();
  160. sheet.suspendEvent();
  161. sheet.setRowCount(data.length);
  162. for (let col = 0; col < setting.header.length; col++) {
  163. let hAlign = "left", vAlign = "center";
  164. if (setting.header[col].hAlign) {
  165. hAlign = setting.header[col].hAlign;
  166. } else if (setting.header[col].dataType !== "String"){
  167. hAlign = "right";
  168. }
  169. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  170. sheetCommonObj.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  171. if (setting.header[col].formatter) {
  172. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  173. }
  174. if(setting.header[col].cellType === "comboBox"){
  175. this.setComboBox(-1,col,sheet,setting.header[col].options,setting.header[col].editorValueType,setting.header[col].editable,setting.header[col].maxDropDownItems);
  176. }
  177. for (let row = 0; row < data.length; row++) {
  178. if(data[row].cellType === 'comboBox'){
  179. let options = data[row].options ? data[row].options.split("@") : [];
  180. this.setComboBox(row,col,sheet,options);
  181. }else if(data[row].cellType === 'String'){//默认设置字符输入,避免出现输入10:01变成日期的情况
  182. sheet.setFormatter(row, col,"@", GC.Spread.Sheets.SheetArea.viewport);
  183. }
  184. let val = data[row][setting.header[col].dataCode];
  185. if(val&&setting.header[col].dataType === "Number"){
  186. if(setting.header[col].hasOwnProperty('decimalField')){
  187. let decimal = getDecimal(setting.header[col].decimalField);
  188. val =scMathUtil.roundToString(val,decimal);
  189. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  190. }else {
  191. val =scMathUtil.roundToString(val,2);
  192. }
  193. }
  194. if(val!=null && setting.header[col].cellType == "checkBox"){
  195. this.setCheckBoxCell(row,col,sheet,val);
  196. }
  197. sheet.setValue(row, col, val, ch);
  198. if(col==0){
  199. let treeType = sheetCommonObj.getTreeNodeCellType(data,row,parentMap);
  200. sheet.getCell(row, 0).cellType(treeType);
  201. visibleMap[data[row].ID] = treeType.collapsed;
  202. this.setRowVisible(data,row,visibleMap,sheet);
  203. }
  204. if(data[row].bgColour) styleRow.push(row)
  205. }
  206. }
  207. for(let r of styleRow){
  208. this.setRowStyle(r,sheet,data[r].bgColour);
  209. }
  210. this.lockCells(sheet,setting);
  211. sheet.resumeEvent();
  212. sheet.resumePaint();
  213. },
  214. setRowVisible:function (data,row,visibleMap,sheet) {
  215. sheet.getRange(row , -1, 1, -1).visible(getVisible(data[row].ParentID));//显示或隐藏
  216. function getVisible(ParentID) {
  217. if(visibleMap[ParentID]) return false //如果父节点是缩起的,那就隐藏本身。
  218. if(visibleMap[ParentID] == false){//如果父节点不是缩起的,要再往父节点找看
  219. let pnode = _.find(data,{'ID':ParentID});
  220. if(pnode) return getVisible(pnode.ParentID);//如果有父节点,递归调用
  221. return true;//没有,返回显示
  222. }
  223. }
  224. },
  225. setSheetBySetting: function (sheet, setting) {
  226. function setCellType(range, cellType) {
  227. let type;
  228. if (cellType === 'checkBox') {
  229. type = new GC.Spread.Sheets.CellTypes.CheckBox();
  230. }
  231. range.cellType(type);
  232. }
  233. setting.header.forEach((item, index) => {
  234. const range = sheet.getRange(-1, index, -1, 1);
  235. this.setAreaAlign(range, item.hAlign, item.vAlign);
  236. if (item.cellType) {
  237. console.log(item);
  238. setCellType(range, item.cellType);
  239. }
  240. if (item.formatter) {
  241. range.formatter(item.formatter)
  242. }
  243. });
  244. },
  245. appendData: function (sheet, index, rowIndex, setting, data) {
  246. /*const viewport = GC.Spread.Sheets.SheetArea.viewport;
  247. const rowHeader = GC.Spread.Sheets.SheetArea.rowHeader;*/
  248. if (!index) {
  249. sheet.setRowCount(0);
  250. }
  251. sheet.suspendPaint();
  252. sheet.suspendEvent();
  253. // 追加空行
  254. sheet.addRows(index, data.length);
  255. // 显示数据
  256. data.forEach((item, rIdx) => {
  257. const row = index + rIdx;
  258. setting.header.forEach((hItem, cIdx) => {
  259. const dataCode = hItem.dataCode;
  260. const val = gljUtil.isDef(item[dataCode]) ? item[dataCode] : '';
  261. sheet.setValue(row, cIdx, val);
  262. });
  263. });
  264. sheet.resumeEvent();
  265. sheet.resumePaint();
  266. },
  267. showData: function(sheet, setting, data,distTypeTree,callback) {
  268. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  269. sheet.suspendPaint();
  270. sheet.suspendEvent();
  271. //sheet.addRows(row, 1);
  272. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  273. if(sheet.getRowCount()<data.length){
  274. data.length<30? sheet.setRowCount(30):sheet.setRowCount(data.length);
  275. }else if(sheet.getRowCount()==0){
  276. sheet.setRowCount(30);
  277. }
  278. for (var col = 0; col < setting.header.length; col++) {
  279. var hAlign = "left", vAlign = "center";
  280. if (setting.header[col].hAlign) {
  281. hAlign = setting.header[col].hAlign;
  282. } else if (setting.header[col].dataType !== "String"){
  283. hAlign = "right";
  284. }
  285. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  286. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  287. if (setting.header[col].formatter) {
  288. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  289. }
  290. /* 直接在showrowdata时当val为null时返回一个text类型的单元格
  291. if(setting.headRows != 2 && setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset 2019 - 11- 11 加入了多行列头的判断情况,排除多行列头时清空操作,先试运行看是否有问题
  292. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  293. sheet.deleteColumns(col,1);
  294. sheet.addColumns(col, 1);
  295. sheet.setValue(0, col, setting.header[col].headerName, header);
  296. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  297. } */
  298. if(setting.header[col].visible!==null&&setting.header[col].visible!==undefined){
  299. sheet.setColumnVisible(col,setting.header[col].visible);
  300. }
  301. sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  302. }
  303. for (var row = 0; row < data.length; row++) {
  304. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  305. this.showRowData(sheet,setting,row,data,distTypeTree);
  306. }
  307. if(setting.emptyRowHeader){
  308. let rowCount = sheet.getRowCount();
  309. for (let row = 0; row < rowCount; row++) {
  310. sheet.setValue(row, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  311. }
  312. }
  313. this.lockCells(sheet,setting);
  314. if(callback) callback();
  315. sheet.resumeEvent();
  316. sheet.resumePaint();
  317. //me.shieldAllCells(sheet);
  318. },
  319. showRowData:function (sheet,setting,row,data,distTypeTree=null) {
  320. let ch = GC.Spread.Sheets.SheetArea.viewport;
  321. if(data[row].cellType == "date"){//日期格式比较特殊,要判断数据里的属性,为data类型,同时读取列
  322. this.setDatePickerCellType(row,data[row].dateCol,sheet)
  323. }
  324. if(data[row].cellType === 'comboBox'){//对于选项在数据里下拉框类型,同时读取列
  325. let options = data[row].options ? data[row].options.split("@") : [];
  326. this.setComboBox(row,data[row].dateCol,sheet,options);
  327. }
  328. this.setRowStyle(row, sheet, data[row].bgColour);
  329. for (var col = 0; col < setting.header.length; col++) {
  330. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  331. var val = _.get(data[row],setting.header[col].dataCode);
  332. let style = null;
  333. if(data[row].foreColor && data[row].styleCol == col){
  334. style = {foreColor: data[row].foreColor};
  335. sheet.setStyle(row, col, {foreColor: data[row].foreColor});
  336. }
  337. if(val&&setting.header[col].dataType === "Number"){
  338. if(setting.header[col].hasOwnProperty('tofix')){
  339. val =scMathUtil.roundToString(val,setting.header[col].tofix);
  340. }
  341. else if(setting.header[col].hasOwnProperty('decimalField')){
  342. var decimal = getDecimal(setting.header[col].decimalField);
  343. val =scMathUtil.roundToString(val,decimal);
  344. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  345. }else {
  346. val =val+'';
  347. }
  348. }
  349. if(setting.header[col].cellType === "checkBox"){
  350. this.setCheckBoxCell(row,col,sheet,val)
  351. }
  352. if(setting.header[col].cellType === "comboBox"){
  353. this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType,setting.header[col].editable,setting.header[col].maxDropDownItems);
  354. }
  355. if(setting.header[col].cellType === "selectButton"){
  356. this.setSelectButton(row,col,sheet,setting.header[col]);
  357. }
  358. if(setting.header[col].cellType === "replaceButton"){
  359. this.setReplaceButton(row,col,sheet,setting.header[col]);
  360. }
  361. if(setting.header[col].cellType === "cusButton"){
  362. this.setCusButton(row,col,sheet,setting,style);
  363. }
  364. if(setting.header[col].cellType === "tipsCell"){
  365. this.setTipsCell(row,col,sheet,setting.header[col]);
  366. }
  367. if(setting.owner==='gljTree'){
  368. if(setting.header[col].cellType === "checkBox"){
  369. val = val==1?val:0;
  370. this.setCheckBoxCell(row,col,sheet,val);
  371. }
  372. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  373. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  374. val=distTypeVal;
  375. }
  376. }
  377. if(setting.header[col].getText){
  378. val = setting.getText[setting.header[col].getText](data[row],val)
  379. }
  380. sheet.setValue(row, col, val, ch);
  381. if(setting.getStyle && setting.getStyle(data[row],row,null,setting.header[col].dataCode)){
  382. let cstyle = setting.getStyle(data[row],row,null,setting.header[col].dataCode);
  383. if(cstyle){
  384. let oldStyle = sheet.getStyle(row, col);
  385. sheet.setStyle(row, col,{...oldStyle,...cstyle});
  386. }
  387. }
  388. }
  389. if(setting.autoFit==true){//设置自动行高
  390. if(setting.fitRow && setting.fitRow.length > 0){//如果有设置特定的某些列才需要自动行高就按设置的来,没有设置就默认所有列
  391. for(let dataCode of setting.fitRow){
  392. let col = _.findIndex(setting.header,{dataCode:dataCode})
  393. sheet.getCell(row,col).wordWrap(true);
  394. }
  395. }else {
  396. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  397. }
  398. sheet.autoFitRow(row);
  399. }
  400. if(setting.getStyle && setting.getStyle(data[row],row,sheet.getActiveRowIndex())){
  401. sheet.setStyle(row, -1, setting.getStyle(data[row],row,sheet.getActiveRowIndex()));
  402. }
  403. },
  404. checkData : function(col,setting, value) {
  405. let result = true;
  406. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  407. if (validator === null) {
  408. return result;
  409. }
  410. switch (validator) {
  411. case 'number':
  412. let regular = /^\d+(\.\d+)?$/;
  413. result = regular.test(value);
  414. break;
  415. case 'boolean':
  416. let booleanValue = [true, false];
  417. result = booleanValue.indexOf(value) >= 0;
  418. break;
  419. }
  420. return result;
  421. },
  422. //todo
  423. analyzePasteData: function(setting, pastedInfo) {
  424. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  425. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  426. if (pastedInfo.pasteData.text[i] === "\n") {
  427. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  428. preStrIdx = i + 1;
  429. rst.push(itemObj);
  430. if (i < pastedInfo.pasteData.text.length - 1) {
  431. itemObj = {};
  432. }
  433. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  434. if (setting.header[propId]) {
  435. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  436. }
  437. propId++;
  438. preStrIdx = i + 1;
  439. //if the last copied-cell were empty, should check whether the end of text
  440. if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  441. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  442. rst.push(itemObj);
  443. }
  444. } else if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  445. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  446. rst.push(itemObj);
  447. }
  448. }
  449. return rst;
  450. },
  451. combineRowData: function(sheet, setting, row) {
  452. var rst = {};
  453. for (var col = 0; col < setting.header.length; col++) {
  454. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  455. }
  456. return rst;
  457. },
  458. shieldAllCells: function(sheet) {
  459. sheet.options.isProtected = true;
  460. },
  461. unShieldAllCells: function(sheet) {
  462. sheet.options.isProtected = false;
  463. },
  464. lockCells: function(sheet, setting){
  465. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  466. sheet.options.isProtected = true;
  467. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  468. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  469. let col = setting.view.lockColumns[i];
  470. if(_.isString(col)){//如果是dataCode 进行转换
  471. col = _.findIndex(setting.header,{dataCode:col})
  472. }
  473. sheet.getRange(-1,col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  474. }
  475. }
  476. if (setting && Array.isArray(setting.view.lockRows) && setting.view.lockRows.length) {
  477. if (!setting.view.lockColumns || !setting.view.lockColumns.length) {
  478. sheet.options.isProtected = true;
  479. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  480. }
  481. setting.view.lockRows.forEach(row => {
  482. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  483. });
  484. }
  485. },
  486. setCheckBoxCell(row,col,sheet,val){
  487. var c = val==null?new GC.Spread.Sheets.CellTypes.Text():this.getCheckBox();
  488. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  489. sheet.getCell(row, col).value(val);
  490. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  491. },
  492. getCheckBox(threeState = false){
  493. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  494. c.isThreeState(threeState);
  495. return c
  496. },
  497. // 无法勾选的复选框
  498. getReadOnlyCheckBox (threeState = false) {
  499. function ReadOnlyCheckBox() {}
  500. ReadOnlyCheckBox.prototype = this.getCheckBox(threeState);
  501. ReadOnlyCheckBox.prototype.processMouseUp = function () {
  502. return;
  503. };
  504. return new ReadOnlyCheckBox();
  505. },
  506. setComboBox(row,col,sheet,options,editorValueType,editable,maxDropDownItems){
  507. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  508. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  509. if(options){
  510. dynamicCombo.items(options);
  511. if(maxDropDownItems) dynamicCombo.maxDropDownItems(maxDropDownItems);
  512. if(editable == true) dynamicCombo.editable(true);//可编辑
  513. if(editorValueType==true){
  514. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  515. }
  516. }
  517. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  518. },
  519. setRowStyle(row,sheet,bgColour) {
  520. if(bgColour){
  521. let style = new GC.Spread.Sheets.Style();
  522. style.backColor = bgColour;
  523. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  524. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  525. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  526. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  527. sheet.setStyle(row, -1, style);
  528. }
  529. },
  530. getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
  531. let me = this;
  532. function CustomerCoeCellType() {
  533. this.isEscKey=false;
  534. this.displayText='';
  535. }
  536. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  537. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  538. console.log("create editor")
  539. let element = document.createElement("div");//这里创建的,会自动销毁
  540. return element
  541. };
  542. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  543. if (editorContext) {
  544. $editor = $(editorContext);
  545. $editor.css("position", "fixed");
  546. $editor.css("background", "white");
  547. //$editor.css("width", cellRect.width); 2018-11-15 改成固定列宽
  548. $editor.css("width", 160);
  549. $editor.attr("gcUIElement", "gcEditingInput");
  550. if(htmlGenerator) htmlGenerator(context,cellRect,$editor);
  551. }
  552. }
  553. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  554. };
  555. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  556. console.log("set editor value");
  557. this.displayText = value;
  558. };
  559. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  560. console.log("get value");
  561. if(this.isEscKey !=true&& updateCallback){
  562. updateCallback();
  563. }
  564. this.isEscKey = false;
  565. return this.displayText;
  566. };
  567. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  568. console.log(" update editor");
  569. if( setEditorValue){//不是esc时才更新
  570. setEditorValue(context);
  571. }
  572. };
  573. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  574. //cell type handle tab key by itself
  575. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  576. return false;
  577. };
  578. return new CustomerCoeCellType();
  579. },
  580. scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
  581. let topRow = sheet.getViewportTopRow(1);
  582. if(row == topRow) return;//已经是最顶行了
  583. let length = options&&options.length>0?options.length:1;
  584. let height = cxt.canvas.height;
  585. let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
  586. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  587. if(endY <= height) return; //如果没有超出显示位置,直接返回
  588. let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
  589. let showRow = topRow + overRow > row?row:topRow + overRow;
  590. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  591. },
  592. setSelectButton(row,col,sheet,header){
  593. /* let getSelectButton = function (cellWidth=100) {
  594. function moreButton() {
  595. }
  596. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  597. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  598. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  599. let buttonW = cellWidth/5;
  600. let endX = x+w-2;
  601. if(value){
  602. let textWidth = ctx.measureText(value).width;
  603. let spaceWidth = cellWidth - buttonW;
  604. let textEndX = x+2+textWidth;
  605. if(spaceWidth<textWidth){
  606. for(let i = value.length-1;i>1;i--){
  607. let newValue = value.substr(0,i);
  608. let newTestWidth = ctx.measureText(newValue).width;
  609. if(spaceWidth>newTestWidth){
  610. value = newValue;
  611. textEndX = x+2+newTestWidth;
  612. break;
  613. }
  614. }
  615. }
  616. ctx.fillText(value,textEndX,y+h-5);
  617. }
  618. //画三个点
  619. ctx.save();
  620. ctx.beginPath();
  621. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  622. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  623. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  624. ctx.fillStyle="black";//填充颜色,默认是黑色
  625. ctx.fill();//画实心圆
  626. ctx.closePath();
  627. ctx.restore();
  628. };
  629. moreButton.prototype.processMouseLeave= function (hitinfo) {
  630. let newCell = new selectButton();
  631. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  632. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  633. };
  634. function selectButton() {
  635. }
  636. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  637. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  638. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  639. };
  640. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  641. return {
  642. x: x,
  643. y: y,
  644. row: context.row,
  645. col: context.col,
  646. cellStyle: cellStyle,
  647. cellRect: cellRect,
  648. sheetArea: context.sheetArea
  649. };
  650. };
  651. selectButton.prototype.processMouseDown = function (hitinfo){
  652. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  653. let b1 = new moreButton();
  654. b1.marginLeft(cellWidth*4/5);
  655. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  656. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  657. }
  658. };
  659. return new selectButton();
  660. };*/
  661. sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  662. },
  663. setCusButton:function (row,col,sheet,setting,style) {
  664. let functionName = setting.header[col].callback;
  665. let readOnly = setting.disable?setting.disable[setting.header[col].disable]:false;
  666. if(typeof(readOnly) == 'function' ){
  667. readOnly = readOnly(row,col);
  668. }
  669. if(functionName){
  670. sheet.setCellType(row, col,this.getCusButtonCellType(setting.callback[functionName],readOnly,style));
  671. }
  672. //sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  673. },
  674. getCusButtonCellType: function (callback, readOnly = false, ostyle) {
  675. var ns = GC.Spread.Sheets;
  676. function CusButtonCellType() {
  677. }
  678. CusButtonCellType.prototype = new ns.CellTypes.Text();
  679. CusButtonCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  680. if (!readOnly) {
  681. if (options.sheet.getActiveRowIndex() == options.row && options.sheet.getActiveColumnIndex() == options.col) {
  682. var image = document.getElementById('f_btn'), imageMagin = 3;
  683. var imageHeight = 15;
  684. var imageWidth = 25;
  685. var imageX = x + w - imageWidth - imageMagin, imageY = y + h / 2 - imageHeight / 2;
  686. ctx.save();
  687. /* 2020-12-16 这里原先是还原背景色的,发现不需要了,先观察
  688. if (style.backColor) {
  689. ctx.fillStyle = style.backColor;
  690. ctx.fillRect(x, y, w, h);
  691. }
  692. */
  693. ctx.drawImage(image, imageX, imageY, imageWidth, imageHeight);
  694. ctx.beginPath();
  695. ctx.arc(imageX + imageWidth / 2, imageY + imageHeight / 2, 1, 0, 360, false);
  696. ctx.arc(imageX + imageWidth / 2 - 4, imageY + imageHeight / 2, 1, 0, 360, false);
  697. ctx.arc(imageX + imageWidth / 2 + 4, imageY + imageHeight / 2, 1, 0, 360, false);
  698. ctx.fillStyle = "black";//填充颜色,默认是黑色
  699. ctx.fill();//画实心圆
  700. ctx.closePath();
  701. ctx.restore();
  702. w = w - imageWidth - imageMagin;
  703. //这里的左对齐的,当显示的字长度超过空白地方时,要改成右对齐
  704. if (style.hAlign == 0) {
  705. if (value) {
  706. let textWidth = ctx.measureText(value).width;
  707. let spaceWidth = w;
  708. if (spaceWidth < textWidth) {
  709. style.hAlign = 2;
  710. }
  711. }
  712. }
  713. }
  714. }
  715. if (ostyle) gljUtil.setProperty(style, ostyle);
  716. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  717. };
  718. CusButtonCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  719. return {
  720. x: x,
  721. y: y,
  722. row: context.row,
  723. col: context.col,
  724. cellStyle: cellStyle,
  725. cellRect: cellRect,
  726. sheetArea: context.sheetArea
  727. };
  728. };
  729. CusButtonCellType.prototype.processMouseDown = function (hitinfo) {
  730. if (hitinfo.sheet.getActiveRowIndex() == hitinfo.row && hitinfo.sheet.getActiveColumnIndex() == hitinfo.col) {
  731. var offset = hitinfo.cellRect.x + hitinfo.cellRect.width - 6;
  732. var imageWidth = 25;
  733. if (hitinfo.x < offset && hitinfo.x > offset - imageWidth) {
  734. if (!readOnly) {
  735. if (callback) callback(hitinfo)
  736. }
  737. }
  738. }
  739. };
  740. return new CusButtonCellType();
  741. },
  742. getSelectButton(cellWidth=100){
  743. function moreButton() {
  744. }
  745. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  746. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  747. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  748. ctx.font = '14px Calibri';
  749. let buttonW = cellWidth/5;
  750. let endX = x+w-2;
  751. if(value){
  752. let textWidth = ctx.measureText(value).width;
  753. let spaceWidth = cellWidth - buttonW;
  754. let textEndX = x+textWidth+2;
  755. if(spaceWidth<textWidth){
  756. for(let i = value.length-1;i>1;i--){
  757. let newValue = value.substr(0,i);
  758. let newTestWidth = ctx.measureText(newValue).width;
  759. if(spaceWidth>newTestWidth){
  760. value = newValue;
  761. textEndX = x+newTestWidth+2;
  762. break;
  763. }
  764. }
  765. }
  766. ctx.fillText(value,textEndX,y+h-6);
  767. }
  768. //画三个点
  769. ctx.save();
  770. ctx.beginPath();
  771. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  772. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  773. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  774. ctx.fillStyle="black";//填充颜色,默认是黑色
  775. ctx.fill();//画实心圆
  776. ctx.closePath();
  777. ctx.restore();
  778. };
  779. moreButton.prototype.processMouseLeave= function (hitinfo) {
  780. let newCell = new selectButton();
  781. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  782. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  783. };
  784. function selectButton() {
  785. }
  786. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  787. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  788. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  789. };
  790. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  791. return {
  792. x: x,
  793. y: y,
  794. row: context.row,
  795. col: context.col,
  796. cellStyle: cellStyle,
  797. cellRect: cellRect,
  798. sheetArea: context.sheetArea
  799. };
  800. };
  801. selectButton.prototype.processMouseDown = function (hitinfo){
  802. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  803. let b1 = new moreButton();
  804. b1.marginLeft(cellWidth*4/5);
  805. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  806. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  807. }
  808. };
  809. return new selectButton();
  810. },
  811. setReplaceButton(row,col,sheet){
  812. let replaceButton = function(){
  813. };
  814. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  815. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  816. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this,arguments);
  817. if(value){
  818. ctx.save();
  819. ctx.fillStyle = "white";
  820. let fh = options.fontInfo&&options.fontInfo.fontSize?options.fontInfo.fontSize:h-6;
  821. ctx.fillText(value,x+(w+ctx.measureText(value).width)/2,y+(h+fh)/2-2);
  822. ctx.restore();
  823. }
  824. };
  825. let cellType = new replaceButton();
  826. cellType.buttonBackColor("#07A0FF");
  827. sheet.setCellType(row, col,cellType,GC.Spread.Sheets.SheetArea.viewport);
  828. },
  829. setDatePickerCellType(row,col,sheet){
  830. let ns = GC.Spread.Sheets;
  831. function DatePickerCellType() {
  832. }
  833. DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  834. DatePickerCellType.prototype.createEditorElement = function (context) {
  835. //Create input presenter.
  836. return document.createElement("input");
  837. };
  838. DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  839. //Initialize input editor.
  840. if (editorContext) {
  841. $editor = $(editorContext);
  842. //DatePickerCellType.prototype.activateEditor.apply(this, arguments);
  843. $editor.datepicker({dateFormat: 'yy-mm-dd'});
  844. $editor.css("position", "absolute");
  845. $editor.attr("gcUIElement", "gcEditingInput");
  846. $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  847. }
  848. }
  849. DatePickerCellType.prototype.deactivateEditor = function (editorContext, context) {
  850. //Remove input editor when end editor status.
  851. if (editorContext) {
  852. var element = editorContext;
  853. $(element).datepicker("hide");
  854. $(element).datepicker("destroy");
  855. }
  856. // DatePickerCellType.prototype.deactivateEditor.apply(this, arguments)
  857. };
  858. DatePickerCellType.prototype.setEditorValue = function (editor, value, context) {
  859. //Sync value from Cell value to editor value.
  860. $(editor).datepicker("setDate", value);
  861. };
  862. DatePickerCellType.prototype.getEditorValue = function (editor, context) {
  863. //Sync value from editor value to cell value.
  864. return $(editor).datepicker("getDate");
  865. };
  866. DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  867. if (editorContext) {
  868. $editor = $(editorContext);
  869. $editor.css("width", cellRect.width - 1);
  870. $editor.css("height", cellRect.height - 3);
  871. }
  872. };
  873. let picker = new DatePickerCellType();
  874. sheet.getCell(row, col).cellType(picker).width(100).formatter('yyyy-mm-dd');
  875. },
  876. setTipsCell(row,col,sheet,header){
  877. let TipCellType = function () {};
  878. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  879. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  880. return {
  881. x: x,
  882. y: y,
  883. row: context.row,
  884. col: context.col,
  885. cellStyle: cellStyle,
  886. cellRect: cellRect,
  887. sheet: context.sheet,
  888. sheetArea: context.sheetArea
  889. };
  890. };
  891. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  892. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  893. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  894. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  895. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  896. zoom = hitinfo.sheet.zoom();
  897. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  898. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  899. let setting = {};
  900. if(textLength <= cellWidth){
  901. return;
  902. }
  903. if(sheet && sheet.getParent().qo){
  904. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  905. }
  906. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  907. };
  908. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  909. TREE_SHEET_HELPER.tipDiv = 'hide';
  910. if (TREE_SHEET_HELPER._toolTipElement) {
  911. $(TREE_SHEET_HELPER._toolTipElement).hide();
  912. TREE_SHEET_HELPER._toolTipElement = null;
  913. };
  914. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  915. };
  916. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  917. },
  918. chkIfEmpty: function(rObj, setting) {
  919. var rst = true;
  920. if (rObj) {
  921. for (var i = 0; i < setting.header.length; i++) {
  922. if (rObj[setting.header[i].dataCode]) {
  923. rst = false;
  924. break;
  925. }
  926. }
  927. }
  928. return rst;
  929. },
  930. //add by zhong 2017-10-10
  931. //动态下拉框,配合EnterCell, args.sheet.repaint();
  932. getDynamicCombo: function (forLocked) {
  933. let ComboCellForActiveCell = function () { };
  934. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  935. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  936. let sheet = options.sheet;
  937. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  938. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  939. } else {
  940. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  941. }
  942. };
  943. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  944. let sheet = options.sheet;
  945. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  946. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  947. } else {
  948. return {
  949. x: x,
  950. y: y,
  951. row: options.row,
  952. col: options.col,
  953. cellStyle: cellStyle,
  954. cellRect: cellRect,
  955. sheetArea: options.sheetArea
  956. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  957. }
  958. };
  959. return new ComboCellForActiveCell();
  960. },
  961. getTipsCell: function (baseCell, cellPrototype, tips, setting, node) {
  962. let getTipsCell = function () {
  963. this.clickCom=false;
  964. };
  965. getTipsCell.prototype = baseCell;
  966. if(tips && tips !=""){
  967. getTipsCell.prototype.processMouseEnter = function(hitinfo){
  968. if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
  969. this.clickCom = false;
  970. return;
  971. }
  972. let text = typeof tips == 'function'?tips(node):tips;
  973. TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,text);
  974. };
  975. getTipsCell.prototype.processMouseLeave = function (hitinfo) {
  976. TREE_SHEET_HELPER.hideTipsDiv();
  977. };
  978. getTipsCell.prototype.processMouseDown = function (hitinfo){
  979. if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
  980. TREE_SHEET_HELPER.hideTipsDiv();
  981. this.clickCom = true;
  982. }
  983. cellPrototype.processMouseDown.apply(this, arguments);
  984. };
  985. getTipsCell.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
  986. TREE_SHEET_HELPER.hideTipsDiv();
  987. cellPrototype.updateEditor.apply(this, arguments);
  988. };
  989. }
  990. return new getTipsCell();
  991. },
  992. getTipsText: function (tips, setting, node) {
  993. function baseTextCell() {}
  994. baseTextCell.prototype = new GC.Spread.Sheets.CellTypes.Text();
  995. baseTextCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  996. return {
  997. x: x,
  998. y: y,
  999. row: context.row,
  1000. col: context.col,
  1001. cellStyle: cellStyle,
  1002. cellRect: cellRect,
  1003. sheetArea: context.sheetArea
  1004. };
  1005. };
  1006. return this.getTipsCell(new baseTextCell(), GC.Spread.Sheets.CellTypes.Text.prototype, tips, setting, node);
  1007. },
  1008. getTipsCombo:function (forLocked,tips,setting,node) {
  1009. const baseCell = sheetCommonObj.getDynamicCombo(forLocked);
  1010. return this.getTipsCell(baseCell, GC.Spread.Sheets.CellTypes.ComboBox.prototype, tips, setting, node);
  1011. },
  1012. // paintFunc,需要追加到paint方法中的自定义paint方法
  1013. getTreeNodeCellType:function (datas,row,parentMap,paintFunc) {// 2018-09-26 不用spreadjs默认的树结构,自定义控件
  1014. var ns = GC.Spread.Sheets;
  1015. let rectW = 10;
  1016. let rectH = 10;
  1017. let margin = 3;
  1018. function TreeNodeCellType() {
  1019. this.collapsed = gljUtil.isDef(datas[row].collapsed)?datas[row].collapsed: true; //默认是折叠的
  1020. this.treeNodeType = true;
  1021. this.rectInfo = {};
  1022. }
  1023. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  1024. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  1025. if (paintFunc) {
  1026. paintFunc(ctx, value, x, y, w, h, style, { rectW, rectH, margin }, datas[row]);
  1027. }
  1028. let offset = 0;
  1029. let step = 7;
  1030. let level = getTreeLevel(datas[row],datas);//从0开始,取当前节点是第几级的
  1031. let tem = offset+margin+ rectW/2+step;//两条线之间的间隔
  1032. let t_offset = offset;
  1033. let temParentID = datas[row].ParentID;
  1034. if(this.treeNodeType == true){
  1035. for(let i = level;i>0;i--){//这里是画子节点前面的竖线,从第二级开始
  1036. let temParent = getParent(temParentID,datas);
  1037. if(temParent){//父节点有下一个兄弟节点才需要画
  1038. if(hasNextBrother(parentMap,temParent)) sheetCommonObj.drawLine(ctx,x+t_offset+tem*i,y,x+t_offset+tem*i,y+h);
  1039. temParentID = temParent.ParentID;
  1040. }
  1041. offset +=tem;
  1042. }
  1043. }
  1044. offset+=step; //这个没法移动,所以要两个判断
  1045. if(this.treeNodeType == true){
  1046. if(hasChildern(datas[row].ID,datas)){//如果是有子节点
  1047. //第一条 或者没有父节点(如费率子表综合里程项)不用画方框头上那条竖线其它都要
  1048. if(row !=0 && gljUtil.isDef(datas[row].ParentID)) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y,x+offset+ rectW/2+margin,y + Math.round(h / 2) - rectH / 2);
  1049. //画方框下面的那条竖线,如果没有下一个兄弟节点,则不用画
  1050. if(hasNextBrother(parentMap,datas[row])) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y + Math.round(h / 2) + rectH / 2,x+offset+ rectW/2+margin,y + h);
  1051. sheetCommonObj.drowRect(ctx, x+offset, y, w, h,rectW,rectH,margin);
  1052. sheetCommonObj.drowSymbol(ctx, x+offset, y, w, h,rectW,rectH,margin, this.collapsed);
  1053. this.rectInfo = {x:x+offset+margin,rectW:rectW}//计录一下可点击位置
  1054. }else {
  1055. let hasNext = datas[row+1]?datas[row+1].ParentID == datas[row].ParentID:false;
  1056. sheetCommonObj.drowSubItem(ctx, x, y, w, h, offset,hasNext,margin+ rectW/2);
  1057. }
  1058. }
  1059. offset += step;
  1060. offset += rectW;
  1061. x = x + offset;//设置偏移
  1062. w = w - offset;
  1063. if(datas[row].foreColor) style.foreColor = datas[row].foreColor;
  1064. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  1065. };
  1066. // override getHitInfo to allow cell type get mouse messages
  1067. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1068. return {
  1069. x: x,
  1070. y: y,
  1071. row: context.row,
  1072. col: context.col,
  1073. cellStyle: cellStyle,
  1074. cellRect: cellRect,
  1075. sheetArea: context.sheetArea
  1076. };
  1077. }
  1078. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  1079. ////方框外1像素内都有效
  1080. if (!_.isEmpty(this.rectInfo)&&Math.floor(hitinfo.x) <= this.rectInfo.x+this.rectInfo.rectW+2 && Math.floor(hitinfo.x) >= this.rectInfo.x-2) {
  1081. this.collapsed = !this.collapsed;
  1082. datas[row].collapsed = this.collapsed;
  1083. this.refreshChildrenVisible(hitinfo.sheet);
  1084. hitinfo.sheet.invalidateLayout();
  1085. hitinfo.sheet.repaint();
  1086. }
  1087. };
  1088. TreeNodeCellType.prototype.refreshChildrenVisible = function (sheet) {
  1089. sheet.suspendPaint();
  1090. sheet.suspendEvent();
  1091. refreshVisible(datas[row]);
  1092. sheet.resumeEvent();
  1093. sheet.resumePaint();
  1094. function refreshVisible(item){
  1095. if(parentMap[item.ID]){
  1096. for(let sub of parentMap[item.ID]){
  1097. refreshVisible(sub)
  1098. }
  1099. }
  1100. let visible = getVisible(item);
  1101. let trow = datas.indexOf(item);
  1102. sheet.getRange(trow , -1, 1, -1).visible(visible);
  1103. }
  1104. function getVisible(item) {
  1105. if(item.ParentID){
  1106. let parent = getParent(item.ParentID,datas);
  1107. if(!parent) return true;
  1108. let p_row= datas.indexOf(parent);
  1109. let visible = !sheet.getCellType(p_row,0).collapsed;
  1110. if(visible == true){ //如果是显示的,则要再往父节点的父节点检查,只要有一个节点是隐藏的,则都是隐藏
  1111. return getVisible(parent);
  1112. }else {
  1113. return visible
  1114. }
  1115. }else {//如果parentID 为空则是最根节点
  1116. return true;
  1117. }
  1118. }
  1119. };
  1120. return new TreeNodeCellType()
  1121. function getTreeLevel(item,data) {
  1122. if(item && item.ParentID && item.ParentID!=-1){
  1123. let pitem = _.find(data,{'ID':item.ParentID});
  1124. return getTreeLevel(pitem,data) + 1;
  1125. }else {
  1126. return 0
  1127. }
  1128. }
  1129. function hasChildern(ID,data) {//返回是否有子项
  1130. let p = _.find(data,{'ParentID':ID});
  1131. if(p) return true;
  1132. return false
  1133. }
  1134. function getParent(ParentID,data) {
  1135. let p = _.find(data,{'ID':ParentID});
  1136. return p;
  1137. }
  1138. function hasNextBrother(parentMap,item){
  1139. let children =parentMap[item.ParentID];
  1140. if(!gljUtil.isDef(children)|| children.indexOf(item) == children.length -1) return false;
  1141. return true
  1142. }
  1143. },
  1144. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  1145. let me = this;
  1146. sheet.suspendPaint();
  1147. let combo = me.getDynamicCombo();
  1148. for(let i = 0, len = rowCount; i < len; i++){
  1149. if(itemsHeight) {
  1150. combo.itemHeight(itemsHeight);
  1151. combo._maxDropDownItems = itemsHeight + 5;
  1152. }
  1153. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1154. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  1155. else combo.items(items);
  1156. sheet.getCell(beginRow + i, col).cellType(combo);
  1157. }
  1158. sheet.resumePaint();
  1159. },
  1160. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  1161. sheet.suspendPaint();
  1162. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  1163. for(let i = 0, len = rowCount; i < len; i++){
  1164. if(itemsHeight) combo.itemHeight(itemsHeight);
  1165. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1166. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  1167. else combo.items(items);
  1168. sheet.getCell(beginRow + i, col).cellType(combo);
  1169. }
  1170. sheet.resumePaint();
  1171. },
  1172. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  1173. copyTextToClipboard: function(text) {
  1174. let textArea = document.createElement("textarea");
  1175. textArea.style.position = 'fixed';
  1176. textArea.style.top = 0;
  1177. textArea.style.left = 0;
  1178. textArea.style.width = '2em';
  1179. textArea.style.height = '2em';
  1180. textArea.style.padding = 0;
  1181. textArea.style.border = 'none';
  1182. textArea.style.outline = 'none';
  1183. textArea.style.boxShadow = 'none';
  1184. textArea.style.background = 'transparent';
  1185. textArea.value = text;
  1186. document.body.appendChild(textArea);
  1187. textArea.select();
  1188. try {
  1189. let successful = document.execCommand('copy');
  1190. let msg = successful ? 'successful' : 'unsuccessful';
  1191. console.log('Copying text command was ' + msg);
  1192. } catch (err) {
  1193. console.log('Oops, unable to copy');
  1194. }
  1195. document.body.removeChild(textArea);
  1196. },
  1197. //获取选中区域的表格类型数据(可粘贴到excel)
  1198. getTableData: function (sheet, colSettings = null) {
  1199. let rst = '';
  1200. let sel = sheet.getSelections()[0];
  1201. let pasteText = [];
  1202. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  1203. if(!sheet.getCell(row, -1).visible())
  1204. continue;
  1205. let rowText = [];
  1206. for(let j = 0; j < sel.colCount; j++){
  1207. let col = sel.col + j;
  1208. if(!sheet.getCell(-1, col).visible())
  1209. continue;
  1210. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  1211. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  1212. else
  1213. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  1214. }
  1215. pasteText.push(rowText.join('\t'));
  1216. }
  1217. return pasteText.join('\n');
  1218. },
  1219. transferToTreeSetting:function(setting,treeSetting,treeCol){
  1220. for(let h of setting.header){
  1221. treeSetting.cols.push(getSettingCol(h))
  1222. }
  1223. for(let l of setting.view.lockColumns){
  1224. if(_.isString(l)){//如果是dataCode 进行转换
  1225. l = _.findIndex(setting.header,{dataCode:l})
  1226. }
  1227. if(treeSetting.cols[l]) treeSetting.cols[l].readOnly = true;
  1228. }
  1229. return treeSetting;
  1230. function getSettingCol(header) {
  1231. let aMap ={left:0,center:1,right:2};
  1232. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  1233. let col = {
  1234. "width":header.headerWidth?header.headerWidth:100,
  1235. "head":{
  1236. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  1237. "spanCols":header.spanCols?header.spanCols:[1],
  1238. "spanRows":header.spanRows?header.spanRows:[1],
  1239. "vAlign":[1],
  1240. "hAlign":[1],
  1241. "font":["Arial"]
  1242. },
  1243. "data": {
  1244. "field": header.dataCode,
  1245. "vAlign": 1,
  1246. "hAlign": hAlign,
  1247. "font": "Arial"
  1248. }
  1249. };
  1250. if(header.showHint == true){
  1251. col.showHint = true;
  1252. }
  1253. if(header.cellType){
  1254. col.data.cellType = getCellType(header);
  1255. }
  1256. if(header.decimalField){//设置formatter
  1257. let decimal = getDecimal(header.decimalField);
  1258. col.formatter = getFormatter(decimal);
  1259. }
  1260. if(header.getText && treeCol){
  1261. col.data.getText = treeCol.getEvent(header.getText);
  1262. }
  1263. /*col.readOnly = function (node) {
  1264. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  1265. return true;
  1266. }
  1267. return false;
  1268. };*/
  1269. return col;
  1270. }
  1271. function getCellType(header) {
  1272. return function () {
  1273. if(header.cellType === "checkBox"){
  1274. return new GC.Spread.Sheets.CellTypes.CheckBox();
  1275. }
  1276. if(header.cellType === "comboBox"){
  1277. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  1278. if(header.options){
  1279. dynamicCombo.itemHeight(header.options.length).items(header.options);
  1280. if(header.editorValueType==true){
  1281. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1282. }
  1283. }
  1284. return dynamicCombo
  1285. }
  1286. }
  1287. }
  1288. },
  1289. //注册自定义回车键事件
  1290. bindEnterKey: function (workBook, operation) {
  1291. workBook.commandManager().register('myEnter', operation);
  1292. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  1293. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  1294. },
  1295. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  1296. bindEscKey: function (workBook, sheets) {
  1297. function isDef(v){
  1298. return typeof v !== 'undefined' && v !== null;
  1299. }
  1300. workBook.commandManager().register('myEsc', function () {
  1301. let activeSheet = workBook.getActiveSheet();
  1302. let hasTheSheet = false;
  1303. for(let sheetObj of sheets){
  1304. let sheet = sheetObj.sheet;
  1305. if(sheet === activeSheet){
  1306. hasTheSheet = true;
  1307. let editStarting = sheetObj.editStarting;
  1308. let editEnded = sheetObj.editEnded;
  1309. if(editStarting){
  1310. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  1311. }
  1312. if(editEnded){
  1313. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  1314. }
  1315. let row = sheet.getActiveRowIndex();
  1316. let col = sheet.getActiveColumnIndex();
  1317. let orgV = sheet.getValue(row, col);
  1318. let orgText = sheet.getText(row, col);
  1319. if(!isDef(orgV)){
  1320. orgV = '';
  1321. }
  1322. if(sheet.isEditing()){
  1323. sheet.endEdit();
  1324. sheet.setValue(row, col, orgV);
  1325. }
  1326. if(editStarting){
  1327. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  1328. }
  1329. if(editEnded){
  1330. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  1331. }
  1332. }
  1333. }
  1334. //容错处理,以防没把所有工作簿的表格信息传入参数
  1335. if(!hasTheSheet){
  1336. if(activeSheet.isEditing()){
  1337. activeSheet.endEdit();
  1338. }
  1339. }
  1340. });
  1341. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  1342. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  1343. },
  1344. //生成列字段与列号映射
  1345. initColMapping: function (obj, headers) {
  1346. //colToField 列下标与列字段映射
  1347. //fieldToCol 列字段与列下标映射
  1348. let colMapping = {colToField: {}, fieldToCol: {}};
  1349. for(let header of headers){
  1350. colMapping['colToField'][headers.indexOf(header)] = header.dataCode;
  1351. colMapping['fieldToCol'][header.dataCode] = headers.indexOf(header);
  1352. }
  1353. console.log(colMapping);
  1354. obj.colMapping = colMapping
  1355. },
  1356. //设置默认样式
  1357. spreadDefaultStyle: function (workBook) {
  1358. let defaultStyle = new GC.Spread.Sheets.Style();
  1359. defaultStyle.font = '14px Calibri';let sheetCount = workBook.getSheetCount();
  1360. for(let i = 0; i < sheetCount; i++){
  1361. let sheet = workBook.getSheet(i);
  1362. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  1363. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  1364. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  1365. }
  1366. },
  1367. //动态根据工作簿宽度和各列宽度比例设置宽度
  1368. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  1369. if(workBook){
  1370. const sheet = workBook.getActiveSheet();
  1371. sheet.suspendEvent();
  1372. sheet.suspendPaint();
  1373. for(let col = 0; col < headers.length; col++){
  1374. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  1375. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1376. }
  1377. else {
  1378. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  1379. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1380. }
  1381. }
  1382. }
  1383. sheet.resumeEvent();
  1384. sheet.resumePaint();
  1385. }
  1386. },
  1387. drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
  1388. ctx.save();
  1389. ctx.strokeStyle = "gray";
  1390. ctx.translate(0.5, 0.5);
  1391. ctx.beginPath();
  1392. let rectX = x + margin;
  1393. let rectY = y + Math.round(h / 2) - rectH / 2;
  1394. ctx.moveTo(rectX, rectY);
  1395. ctx.lineTo(rectX, rectY + rectH);
  1396. ctx.lineTo(rectX + rectW, rectY + rectH);
  1397. ctx.lineTo(rectX + rectW, rectY);
  1398. ctx.lineTo(rectX, rectY);
  1399. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  1400. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  1401. ctx.stroke();
  1402. ctx.restore();
  1403. },
  1404. drawLine : function (ctx, x1, y1, x2, y2, color) {
  1405. let l_color = color?color:"#ababab";
  1406. ctx.save();
  1407. ctx.translate(0.5, 0.5);
  1408. ctx.beginPath();
  1409. ctx.moveTo(x1, y1);
  1410. ctx.lineTo(x2, y2);
  1411. ctx.strokeStyle = l_color;
  1412. ctx.stroke();
  1413. ctx.restore();
  1414. },
  1415. drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
  1416. ctx.save();
  1417. ctx.strokeStyle = "#000000";
  1418. ctx.translate(0.5, 0.5);
  1419. ctx.beginPath();
  1420. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  1421. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  1422. let rectY = y + Math.round(h / 2) - rectH / 2;
  1423. if (collapsed) {
  1424. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  1425. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  1426. }
  1427. ctx.stroke();
  1428. ctx.restore();
  1429. },
  1430. drowTriangle:function(ctx,x,y){//画向下三角形
  1431. ctx.save();
  1432. ctx.fillStyle = "black";
  1433. ctx.beginPath();
  1434. ctx.moveTo(x, y);
  1435. ctx.lineTo(x-3, y -6);
  1436. ctx.lineTo(x+3, y -6);
  1437. ctx.fill();
  1438. ctx.restore();
  1439. },
  1440. drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
  1441. let t_step = step?step:6;
  1442. offset += t_step;
  1443. ctx.save();
  1444. ctx.strokeStyle = "#ababab";
  1445. ctx.translate(0.5, 0.5);
  1446. ctx.beginPath();
  1447. ctx.moveTo(x + offset, y);
  1448. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1449. offset += 9;
  1450. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1451. if (hasNext) {
  1452. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  1453. ctx.lineTo(x + offset - 9, y + h);
  1454. }
  1455. ctx.stroke();
  1456. ctx.restore();
  1457. return offset;
  1458. },
  1459. checkData:function(col,setting, value) {
  1460. let result = true;
  1461. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  1462. if (validator === null) {
  1463. return result;
  1464. }
  1465. switch (validator) {
  1466. case 'number':
  1467. let regular = /^\d+(\.\d+)?$/;
  1468. result = regular.test(value);
  1469. break;
  1470. case 'boolean':
  1471. let booleanValue = [true, false];
  1472. result = booleanValue.indexOf(value) >= 0;
  1473. break;
  1474. }
  1475. return result;
  1476. },
  1477. disableSpread(spread) {
  1478. spread.unbind(GC.Spread.Sheets.Events.ButtonClicked);
  1479. let sheetCount = spread.getSheetCount();
  1480. for(let i = 0; i < sheetCount; i++){
  1481. let sheet = spread.getSheet(i);
  1482. sheet.unbind(GC.Spread.Sheets.Events.ButtonClicked);
  1483. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  1484. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  1485. sheet.unbind(GC.Spread.Sheets.Events.RangeChanged);
  1486. sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanging);
  1487. sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanged);
  1488. sheet.unbind(GC.Spread.Sheets.Events.CellDoubleClick);
  1489. sheet.unbind(GC.Spread.Sheets.Events.CellClick);
  1490. sheet.unbind(GC.Spread.Sheets.Events.ValueChanged);
  1491. sheet.suspendPaint();
  1492. sheet.suspendEvent();
  1493. sheet.options.isProtected = true;
  1494. let rowCount = sheet.getRowCount();
  1495. let colCount = sheet.getColumnCount();
  1496. for(let row = 0; row < rowCount; row++){
  1497. for(let col = 0; col < colCount; col++){
  1498. sheet.getCell(row, col).locked(true);
  1499. }
  1500. }
  1501. sheet.resumePaint();
  1502. sheet.resumeEvent();
  1503. }
  1504. },
  1505. // 延迟一段时间刷新表格,因为有的弹窗里面有表格,马上刷新可能会造成,弹窗界面还未完全显示完就完成了表格刷新,导致表格显示不完整
  1506. refreshWorkbookDelDefer(workbook, time) {
  1507. if (workbook) {
  1508. setTimeout(() => workbook.refresh(), time);
  1509. }
  1510. },
  1511. setRowsAutoFit(sheet, rows, col, wordWrap) {
  1512. rows.forEach(row => {
  1513. sheet.getCell(row, col).wordWrap(wordWrap);
  1514. sheet.autoFitRow(row);
  1515. });
  1516. },
  1517. // 获取带输入框的右键子目
  1518. registerInputContextMenuItem(name, html, icon, callback) {
  1519. $.contextMenu.types[name] = function (item, opt, root) {
  1520. // 因为contextMenu有自己的键盘事件处理,因此输入框的键盘控制光标需要自己定义事件实现覆盖。
  1521. const Direction = {
  1522. BACKWARD: 'backward',
  1523. FORWARD: 'forward'
  1524. };
  1525. function moveLeft(input, isShifting) {
  1526. const start = input.selectionStart;
  1527. const end = input.selectionEnd;
  1528. const direction = end === start ? Direction.BACKWARD : input.selectionDirection;
  1529. if (isShifting) {
  1530. if (direction === Direction.FORWARD) {
  1531. const curEnd = end - 1;
  1532. input.setSelectionRange(start, curEnd);
  1533. } else {
  1534. const curStart = start - 1 < 0 ? 0 : start - 1;
  1535. input.setSelectionRange(curStart, end, Direction.BACKWARD);
  1536. }
  1537. } else {
  1538. const idx = end > start
  1539. ? start
  1540. : start - 1 < 0
  1541. ? 0
  1542. : start - 1;
  1543. input.setSelectionRange(idx, idx);
  1544. }
  1545. }
  1546. function moveRight(input, isShifting) {
  1547. const start = input.selectionStart;
  1548. const end = input.selectionEnd;
  1549. const direction = start === end ? Direction.FORWARD : input.selectionDirection;
  1550. if (isShifting) {
  1551. if (direction === Direction.BACKWARD) {
  1552. const curStart = start + 1;
  1553. input.setSelectionRange(curStart, end);
  1554. } else {
  1555. const curEnd = end + 1;
  1556. input.setSelectionRange(start, curEnd, Direction.FORWARD);
  1557. }
  1558. } else {
  1559. const idx = start < end
  1560. ? end
  1561. : end + 1
  1562. input.setSelectionRange(idx, idx);
  1563. }
  1564. }
  1565. function handleConfirm() {
  1566. if (callback) {
  1567. callback();
  1568. }
  1569. root.$menu.trigger('contextmenu:hide');
  1570. }
  1571. $(html)
  1572. .appendTo(this)
  1573. .on('input', 'input', function () {
  1574. const number = +$(this).val();
  1575. if (isNaN(number)) {
  1576. $(this).val(1);
  1577. } else if (number > 99) {
  1578. $(this).val(99);
  1579. }
  1580. })
  1581. .on('keydown', 'input', function (e) {
  1582. const key = e.key;
  1583. if (key === 'ArrowUp' || key === 'ArrowDown') {
  1584. return false;
  1585. }
  1586. if (key === 'Enter') {
  1587. handleConfirm();
  1588. return false;
  1589. }
  1590. const input = $(this)[0];
  1591. if (key === 'ArrowLeft') {
  1592. moveLeft(input, e.shiftKey);
  1593. } else if (key === 'ArrowRight') {
  1594. moveRight(input, e.shiftKey);
  1595. }
  1596. })
  1597. .parent().on('click', function (e) {
  1598. if (e.target.tagName === 'INPUT') {
  1599. return false;
  1600. }
  1601. handleConfirm();
  1602. });
  1603. this.addClass(`context-menu-icon context-menu-icon--fa fa ${icon}`);
  1604. };
  1605. return name;
  1606. }
  1607. }