sheet_common.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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. sheet.suspendPaint();
  24. sheet.suspendEvent();
  25. if (setting.frozenCols) sheet.frozenColumnCount(setting.frozenCols);//冻结列
  26. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  27. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  28. if (setting && setting.view && setting.view.colHeaderHeight) {
  29. sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
  30. };
  31. if (setting && setting.view && setting.view.rowHeaderWidth) {
  32. sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
  33. };
  34. if (setting.emptyRowHeader) {
  35. sheet.setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  36. }
  37. sheet.options.colHeaderAutoTextIndex = 1;
  38. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  39. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  40. sheet.options.protectionOptions = {
  41. allowResizeColumns: true
  42. };
  43. sheet.showRowOutline(false);
  44. sheet.options.allowCellOverflow = false;
  45. me.buildHeader(sheet, setting);
  46. if (rowCount > 0)
  47. sheet.setRowCount(rowCount);
  48. else
  49. sheet.setRowCount(1);
  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. buildHeader: function (sheet, setting) {
  62. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  63. for (var i = 0; i < setting.header.length; i++) {
  64. sheet.setValue(0, i, setting.header[i].headerName, ch);
  65. sheet.getCell(0, i, ch).wordWrap(true);
  66. sheet.setColumnWidth(i, setting.header[i].headerWidth ? setting.header[i].headerWidth : 100);
  67. sheet.setColumnVisible(i, setting.header[i].visible === false ? false : true);
  68. }
  69. },
  70. cleanSheet: function (sheet, setting, rowCount) {
  71. sheet.suspendPaint();
  72. sheet.suspendEvent();
  73. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  74. if (rowCount > 0) sheet.setRowCount(rowCount);
  75. sheet.clearSelection();
  76. sheet.resumeEvent();
  77. sheet.resumePaint();
  78. },
  79. cleanData: function (sheet, setting, rowCount) {
  80. sheet.suspendPaint();
  81. sheet.suspendEvent();
  82. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  83. if (rowCount > 0) sheet.setRowCount(rowCount);
  84. sheet.resumeEvent();
  85. sheet.resumePaint();
  86. },
  87. setAreaAlign: function (area, hAlign, vAlign) {
  88. if (!(hAlign) || hAlign === "left") {
  89. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  90. } else if (hAlign === "right") {
  91. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  92. } else if (hAlign === "center") {
  93. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  94. } else {
  95. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  96. }
  97. if (!(vAlign) || vAlign === "center") {
  98. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  99. } else if (vAlign === "top") {
  100. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  101. } else if (vAlign === "bottom") {
  102. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  103. } else {
  104. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  105. }
  106. },
  107. showData: function (sheet, setting, data, distTypeTree, callback) {//这个callback是为了在showdata后还做了引起重画表格的操作,在callback里调用能提高效率
  108. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  109. sheet.suspendPaint();
  110. sheet.suspendEvent();
  111. //sheet.addRows(row, 1);
  112. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  113. if (sheet.getRowCount() < data.length) {
  114. data.length < 30 ? sheet.setRowCount(30) : sheet.setRowCount(data.length);
  115. } else if (sheet.getRowCount() == 0) {
  116. sheet.setRowCount(30);
  117. }
  118. for (var col = 0; col < setting.header.length; col++) {
  119. var hAlign = "left", vAlign = "center";
  120. if (setting.header[col].hAlign) {
  121. hAlign = setting.header[col].hAlign;
  122. } else if (setting.header[col].dataType !== "String") {
  123. hAlign = "right";
  124. }
  125. vAlign = setting.header[col].vAlign ? setting.header[col].vAlign : vAlign;
  126. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  127. if (setting.header[col].formatter) {
  128. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  129. }
  130. /* 直接在showrowdata时当val为null时返回一个text类型的单元格
  131. if (setting.header[col].cellType === "checkBox" || setting.header[col].cellType === "button") {//clear and reset
  132. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  133. sheet.deleteColumns(col, 1);
  134. sheet.addColumns(col, 1);
  135. sheet.setValue(0, col, setting.header[col].headerName, header);
  136. sheet.setColumnWidth(col, setting.header[col].headerWidth ? setting.header[col].headerWidth : 100);
  137. } */
  138. if (setting.header[col].visible !== null && setting.header[col].visible !== undefined) {
  139. sheet.setColumnVisible(col, setting.header[col].visible);
  140. }
  141. sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  142. }
  143. for (var row = 0; row < data.length; row++) {
  144. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  145. this.showRowData(sheet, setting, row, data, distTypeTree);
  146. }
  147. if (setting.emptyRowHeader) {
  148. let rowCount = sheet.getRowCount();
  149. for (let row = 0; row < rowCount; row++) {
  150. sheet.setValue(row, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  151. }
  152. }
  153. this.lockCells(sheet, setting);
  154. if (callback) callback();
  155. sheet.resumeEvent();
  156. sheet.resumePaint();
  157. //me.shieldAllCells(sheet);
  158. },
  159. showRowData: function (sheet, setting, row, data, distTypeTree = null) {
  160. let ch = GC.Spread.Sheets.SheetArea.viewport;
  161. for (var col = 0; col < setting.header.length; col++) {
  162. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  163. var val = data[row][setting.header[col].dataCode];
  164. if (val && setting.header[col].dataType === "Number") {
  165. if (setting.header[col].hasOwnProperty('tofix')) {
  166. val = scMathUtil.roundToString(val, setting.header[col].tofix);
  167. }
  168. else if (setting.header[col].hasOwnProperty('decimalField')) {
  169. var decimal = getDecimal(setting.header[col].decimalField);
  170. val = scMathUtil.roundToString(val, decimal);
  171. sheet.setFormatter(-1, col, getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  172. } else {
  173. val = val + '';
  174. }
  175. }
  176. if (setting.header[col].cellType === "checkBox") {
  177. this.setCheckBoxCell(row, col, sheet, val)
  178. }
  179. if (setting.header[col].cellType === "comboBox") {
  180. this.setComboBox(row, col, sheet, setting.header[col].options, setting.header[col].editorValueType, setting.header[col].editable, setting.header[col].maxDropDownItems);
  181. }
  182. if (setting.header[col].cellType === "selectButton") {
  183. this.setSelectButton(row, col, sheet, setting.header[col]);
  184. }
  185. if (setting.header[col].cellType === "replaceButton") {
  186. this.setReplaceButton(row, col, sheet, setting.header[col]);
  187. }
  188. if (setting.header[col].cellType === "cusButton") {
  189. this.setCusButton(row, col, sheet, setting);
  190. }
  191. if (setting.header[col].cellType === "tipsCell") {
  192. this.setTipsCell(row, col, sheet, setting.header[col]);
  193. }
  194. if (setting.owner === 'gljTree') {
  195. if (setting.header[col].cellType === "checkBox") {
  196. val == 1 ? val : 0;
  197. this.setCheckBoxCell(row, col, sheet, val);
  198. }
  199. if (setting.header[col].dataCode === 'gljType' && data[row].gljType) {
  200. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  201. val = distTypeVal;
  202. }
  203. }
  204. if (setting.header[col].getText) {
  205. val = setting.getText[setting.header[col].getText](data[row], val)
  206. }
  207. sheet.setValue(row, col, val, ch);
  208. }
  209. this.setRowStyle(row, sheet, data[row].bgColour);
  210. if (setting.autoFit == true) {//设置自动行高
  211. if (setting.fitRow && setting.fitRow.length > 0) {//如果有设置特定的某些列才需要自动行高就按设置的来,没有设置就默认所有列
  212. for (let dataCode of setting.fitRow) {
  213. let col = _.findIndex(setting.header, { dataCode: dataCode });
  214. sheet.getCell(row, col).wordWrap(true);
  215. }
  216. } else {
  217. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  218. }
  219. sheet.autoFitRow(row);
  220. }
  221. if (setting.getStyle && setting.getStyle(data[row], row, sheet.getActiveRowIndex())) {
  222. sheet.setStyle(row, -1, setting.getStyle(data[row]));
  223. }
  224. },
  225. checkData: function (col, setting, value) {
  226. let result = true;
  227. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  228. if (validator === null) {
  229. return result;
  230. }
  231. switch (validator) {
  232. case 'number':
  233. let regular = /^\d+(\.\d+)?$/;
  234. result = regular.test(value);
  235. break;
  236. case 'boolean':
  237. let booleanValue = [true, false];
  238. result = booleanValue.indexOf(value) >= 0;
  239. break;
  240. }
  241. return result;
  242. },
  243. analyzePasteData: function (setting, pastedInfo) {
  244. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  245. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  246. if (pastedInfo.pasteData.text[i] === "\n") {
  247. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  248. preStrIdx = i + 1;
  249. rst.push(itemObj);
  250. if (i < pastedInfo.pasteData.text.length - 1) {
  251. itemObj = {};
  252. }
  253. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  254. if (setting.header[propId]) {
  255. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  256. }
  257. propId++;
  258. preStrIdx = i + 1;
  259. //if the last copied-cell were empty, should check whether the end of text
  260. if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  261. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  262. rst.push(itemObj);
  263. }
  264. } else if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  265. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  266. rst.push(itemObj);
  267. }
  268. }
  269. return rst;
  270. },
  271. combineRowData: function (sheet, setting, row) {
  272. var rst = {};
  273. for (var col = 0; col < setting.header.length; col++) {
  274. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  275. }
  276. return rst;
  277. },
  278. shieldAllCells: function (sheet) {
  279. sheet.options.isProtected = true;
  280. },
  281. unShieldAllCells: function (sheet) {
  282. sheet.options.isProtected = false;
  283. },
  284. lockCells: function (sheet, setting) {
  285. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  286. sheet.options.isProtected = true;
  287. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  288. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  289. let col = setting.view.lockColumns[i];
  290. if (_.isString(col)) {//如果是dataCode 进行转换
  291. col = _.findIndex(setting.header, { dataCode: col })
  292. }
  293. sheet.getRange(-1, col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  294. }
  295. }
  296. },
  297. setCheckBoxCell(row, col, sheet, val) {
  298. var c = val==null?new GC.Spread.Sheets.CellTypes.Text():new GC.Spread.Sheets.CellTypes.CheckBox();
  299. if(val != null) c.isThreeState(false);
  300. sheet.setCellType(row, col, c, GC.Spread.Sheets.SheetArea.viewport);
  301. sheet.getCell(row, col).value(val);
  302. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  303. },
  304. setComboBox(row, col, sheet, options, editorValueType, editable, maxDropDownItems) {
  305. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  306. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  307. if (options) {
  308. dynamicCombo.items(options);
  309. if (editorValueType == true) {
  310. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  311. }
  312. if (editable) dynamicCombo.editable(true);
  313. if (maxDropDownItems) dynamicCombo.maxDropDownItems(maxDropDownItems);
  314. }
  315. sheet.setCellType(row, col, dynamicCombo, GC.Spread.Sheets.SheetArea.viewport);
  316. },
  317. setRowStyle(row, sheet, bgColour) {
  318. if (bgColour) {
  319. let style = new GC.Spread.Sheets.Style();
  320. style.backColor = bgColour;
  321. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  322. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  323. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  324. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  325. sheet.setStyle(row, -1, style);
  326. }
  327. },
  328. getCustomerCoeCellType: function (htmlGenerator, setEditorValue, updateCallback) {
  329. let me = this;
  330. function CustomerCoeCellType() {
  331. this.isEscKey = false;
  332. this.displayText = '';
  333. }
  334. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  335. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  336. console.log("create editor")
  337. let element = document.createElement("div");//这里创建的,会自动销毁
  338. return element
  339. };
  340. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  341. if (editorContext) {
  342. $editor = $(editorContext);
  343. $editor.css("position", "fixed");
  344. $editor.css("background", "white");
  345. //$editor.css("width", cellRect.width); 2018-11-15 改成固定列宽
  346. $editor.css("width", 160);
  347. $editor.attr("gcUIElement", "gcEditingInput");
  348. if (htmlGenerator) htmlGenerator(context, cellRect, $editor);
  349. }
  350. }
  351. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  352. };
  353. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  354. console.log("set editor value");
  355. this.displayText = value;
  356. };
  357. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  358. console.log("get value");
  359. if (this.isEscKey != true && updateCallback) {
  360. updateCallback();
  361. }
  362. this.isEscKey = false;
  363. return this.displayText;
  364. };
  365. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  366. console.log(" update editor");
  367. if (setEditorValue) {//不是esc时才更新
  368. setEditorValue(context);
  369. }
  370. };
  371. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  372. //cell type handle tab key by itself
  373. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  374. return false;
  375. };
  376. return new CustomerCoeCellType();
  377. },
  378. scrollSheetForOption: function (sheet, cxt, cellRect, row, options) {
  379. let topRow = sheet.getViewportTopRow(1);
  380. if (row == topRow) return;//已经是最顶行了
  381. let length = options && options.length > 0 ? options.length : 1;
  382. let height = cxt.canvas.height;
  383. let startY = cellRect.y + cellRect.height;//下拉框的起始显示位置
  384. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  385. if (endY <= height) return; //如果没有超出显示位置,直接返回
  386. let overRow = Math.ceil((endY - height) / cellRect.height);//超出的行数
  387. let showRow = topRow + overRow > row ? row : topRow + overRow;
  388. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  389. },
  390. setSelectButton(row, col, sheet, header) {
  391. /* let getSelectButton = function (cellWidth=100) {
  392. function moreButton() {
  393. }
  394. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  395. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  396. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  397. let buttonW = cellWidth/5;
  398. let endX = x+w-2;
  399. if(value){
  400. let textWidth = ctx.measureText(value).width;
  401. let spaceWidth = cellWidth - buttonW;
  402. let textEndX = x+2+textWidth;
  403. if(spaceWidth<textWidth){
  404. for(let i = value.length-1;i>1;i--){
  405. let newValue = value.substr(0,i);
  406. let newTestWidth = ctx.measureText(newValue).width;
  407. if(spaceWidth>newTestWidth){
  408. value = newValue;
  409. textEndX = x+2+newTestWidth;
  410. break;
  411. }
  412. }
  413. }
  414. ctx.fillText(value,textEndX,y+h-5);
  415. }
  416. //画三个点
  417. ctx.save();
  418. ctx.beginPath();
  419. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  420. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  421. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  422. ctx.fillStyle="black";//填充颜色,默认是黑色
  423. ctx.fill();//画实心圆
  424. ctx.closePath();
  425. ctx.restore();
  426. };
  427. moreButton.prototype.processMouseLeave= function (hitinfo) {
  428. let newCell = new selectButton();
  429. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  430. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  431. };
  432. function selectButton() {
  433. }
  434. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  435. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  436. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  437. };
  438. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  439. return {
  440. x: x,
  441. y: y,
  442. row: context.row,
  443. col: context.col,
  444. cellStyle: cellStyle,
  445. cellRect: cellRect,
  446. sheetArea: context.sheetArea
  447. };
  448. };
  449. selectButton.prototype.processMouseDown = function (hitinfo){
  450. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  451. let b1 = new moreButton();
  452. b1.marginLeft(cellWidth*4/5);
  453. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  454. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  455. }
  456. };
  457. return new selectButton();
  458. };*/
  459. sheet.setCellType(row, col, this.getSelectButton(header.headerWidth), GC.Spread.Sheets.SheetArea.viewport);
  460. },
  461. getSelectButton(cellWidth = 100) {
  462. function moreButton() {
  463. }
  464. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  465. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  466. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  467. ctx.font = '14px Calibri';
  468. let buttonW = cellWidth / 5;
  469. let endX = x + w - 2;
  470. if (value) {
  471. let textWidth = ctx.measureText(value).width;
  472. let spaceWidth = cellWidth - buttonW;
  473. let textEndX = x + textWidth + 2;
  474. if (spaceWidth < textWidth) {
  475. for (let i = value.length - 1; i > 1; i--) {
  476. let newValue = value.substr(0, i);
  477. let newTestWidth = ctx.measureText(newValue).width;
  478. if (spaceWidth > newTestWidth) {
  479. value = newValue;
  480. textEndX = x + newTestWidth + 2;
  481. break;
  482. }
  483. }
  484. }
  485. ctx.fillText(value, textEndX, y + h - 6);
  486. }
  487. //画三个点
  488. ctx.save();
  489. ctx.beginPath();
  490. ctx.arc(endX - buttonW / 2, y + h / 2, 1, 0, 360, false);
  491. ctx.arc(endX - buttonW / 2 - 4, y + h / 2, 1, 0, 360, false);
  492. ctx.arc(endX - buttonW / 2 + 4, y + h / 2, 1, 0, 360, false);
  493. ctx.fillStyle = "black";//填充颜色,默认是黑色
  494. ctx.fill();//画实心圆
  495. ctx.closePath();
  496. ctx.restore();
  497. };
  498. moreButton.prototype.processMouseLeave = function (hitinfo) {
  499. let newCell = new selectButton();
  500. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  501. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  502. };
  503. function selectButton() {
  504. }
  505. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  506. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  507. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  508. };
  509. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  510. return {
  511. x: x,
  512. y: y,
  513. row: context.row,
  514. col: context.col,
  515. cellStyle: cellStyle,
  516. cellRect: cellRect,
  517. sheetArea: context.sheetArea
  518. };
  519. };
  520. selectButton.prototype.processMouseDown = function (hitinfo) {
  521. if (hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked() != true) {
  522. let b1 = new moreButton();
  523. b1.marginLeft(cellWidth * 4 / 5);
  524. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  525. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  526. }
  527. };
  528. return new selectButton();
  529. },
  530. setCusButton: function (row, col, sheet, setting) {
  531. let functionName = setting.header[col].callback;
  532. let readOnly = setting.disable[setting.header[col].disable];
  533. if (typeof (readOnly) == 'function') {
  534. readOnly = readOnly(row, col);
  535. }
  536. if (functionName) {
  537. sheet.setCellType(row, col, this.getCusButtonCellType(setting.callback[functionName], readOnly));
  538. }
  539. //sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  540. },
  541. getCusButtonCellType: function (callback, readOnly = false) {
  542. var ns = GC.Spread.Sheets;
  543. function CusButtonCellType() {
  544. }
  545. CusButtonCellType.prototype = new ns.CellTypes.Text();
  546. CusButtonCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  547. if (!readOnly) {
  548. if (options.sheet.getActiveRowIndex() == options.row && options.sheet.getActiveColumnIndex() == options.col) {
  549. var image = document.getElementById('f_btn'), imageMagin = 3;
  550. var imageHeight = 15;
  551. var imageWidth = 25;
  552. var imageX = x + w - imageWidth - imageMagin, imageY = y + h / 2 - imageHeight / 2;
  553. ctx.save();
  554. if (style.backColor) {
  555. ctx.fillStyle = style.backColor;
  556. ctx.fillRect(x, y, w, h);
  557. }
  558. ctx.drawImage(image, imageX, imageY, imageWidth, imageHeight);
  559. ctx.beginPath();
  560. ctx.arc(imageX + imageWidth / 2, imageY + imageHeight / 2, 1, 0, 360, false);
  561. ctx.arc(imageX + imageWidth / 2 - 4, imageY + imageHeight / 2, 1, 0, 360, false);
  562. ctx.arc(imageX + imageWidth / 2 + 4, imageY + imageHeight / 2, 1, 0, 360, false);
  563. ctx.fillStyle = "black";//填充颜色,默认是黑色
  564. ctx.fill();//画实心圆
  565. ctx.closePath();
  566. ctx.restore();
  567. w = w - imageWidth - imageMagin;
  568. //这里的左对齐的,当显示的字长度超过空白地方时,要改成右对齐
  569. if (style.hAlign == 0) {
  570. if (value) {
  571. let textWidth = ctx.measureText(value).width;
  572. let spaceWidth = w;
  573. if (spaceWidth < textWidth) {
  574. style.hAlign = 2;
  575. }
  576. }
  577. }
  578. }
  579. }
  580. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  581. };
  582. CusButtonCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  583. return {
  584. x: x,
  585. y: y,
  586. row: context.row,
  587. col: context.col,
  588. cellStyle: cellStyle,
  589. cellRect: cellRect,
  590. sheetArea: context.sheetArea
  591. };
  592. };
  593. CusButtonCellType.prototype.processMouseDown = function (hitinfo) {
  594. if (hitinfo.sheet.getActiveRowIndex() == hitinfo.row && hitinfo.sheet.getActiveColumnIndex() == hitinfo.col) {
  595. var offset = hitinfo.cellRect.x + hitinfo.cellRect.width - 6;
  596. var imageWidth = 25;
  597. if (hitinfo.x < offset && hitinfo.x > offset - imageWidth) {
  598. if (!readOnly) {
  599. if (callback) callback(hitinfo)
  600. }
  601. }
  602. }
  603. };
  604. return new CusButtonCellType();
  605. },
  606. setReplaceButton(row, col, sheet) {
  607. let replaceButton = function () {
  608. };
  609. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  610. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  611. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this, arguments);
  612. if (value) {
  613. ctx.save();
  614. ctx.fillStyle = "white";
  615. let fh = options.fontInfo && options.fontInfo.fontSize ? options.fontInfo.fontSize : h - 6;
  616. ctx.fillText(value, x + (w + ctx.measureText(value).width) / 2, y + (h + fh) / 2 - 2);
  617. ctx.restore();
  618. }
  619. };
  620. let cellType = new replaceButton();
  621. cellType.buttonBackColor("#07A0FF");
  622. sheet.setCellType(row, col, cellType, GC.Spread.Sheets.SheetArea.viewport);
  623. },
  624. setTipsCell(row, col, sheet, header) {
  625. let TipCellType = function () { };
  626. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  627. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  628. return {
  629. x: x,
  630. y: y,
  631. row: context.row,
  632. col: context.col,
  633. cellStyle: cellStyle,
  634. cellRect: cellRect,
  635. sheet: context.sheet,
  636. sheetArea: context.sheetArea
  637. };
  638. };
  639. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  640. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  641. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  642. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  643. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  644. zoom = hitinfo.sheet.zoom();
  645. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, { sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport });
  646. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  647. let setting = {};
  648. if (textLength <= cellWidth) {
  649. return;
  650. }
  651. if (sheet && sheet.getParent().qo) {
  652. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  653. }
  654. TREE_SHEET_HELPER.showTipsDiv(text, setting, hitinfo);
  655. };
  656. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  657. TREE_SHEET_HELPER.tipDiv = 'hide';
  658. if (TREE_SHEET_HELPER._toolTipElement) {
  659. $(TREE_SHEET_HELPER._toolTipElement).hide();
  660. TREE_SHEET_HELPER._toolTipElement = null;
  661. };
  662. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  663. };
  664. sheet.setCellType(row, col, new TipCellType(), GC.Spread.Sheets.SheetArea.viewport);
  665. },
  666. chkIfEmpty: function (rObj, setting) {
  667. var rst = true;
  668. if (rObj) {
  669. for (var i = 0; i < setting.header.length; i++) {
  670. if (rObj[setting.header[i].dataCode]) {
  671. rst = false;
  672. break;
  673. }
  674. }
  675. }
  676. return rst;
  677. },
  678. //add by zhong 2017-10-10
  679. //动态下拉框,配合EnterCell, args.sheet.repaint();
  680. getDynamicCombo: function (forLocked) {
  681. let ComboCellForActiveCell = function () { };
  682. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  683. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  684. let sheet = options.sheet;
  685. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  686. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  687. } else {
  688. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  689. }
  690. };
  691. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  692. let sheet = options.sheet;
  693. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  694. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  695. } else {
  696. return {
  697. x: x,
  698. y: y,
  699. row: options.row,
  700. col: options.col,
  701. cellStyle: cellStyle,
  702. cellRect: cellRect,
  703. sheetArea: options.sheetArea
  704. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  705. }
  706. };
  707. return new ComboCellForActiveCell();
  708. },
  709. getTipsCombo: function (forLocked, tips, setting, node) {
  710. let getTipsCombo = function () {
  711. this.clickCom = false;
  712. };
  713. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  714. if (tips && tips != "") {
  715. getTipsCombo.prototype.processMouseEnter = function (hitinfo) {
  716. if (this.clickCom == true) { //点击了下拉框的三角形,则不用再显示悬浮框了
  717. this.clickCom = false;
  718. return;
  719. }
  720. let text = typeof tips == 'function' ? tips(node) : tips;
  721. TREE_SHEET_HELPER.delayShowTips(hitinfo, setting, text);
  722. };
  723. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  724. TREE_SHEET_HELPER.hideTipsDiv();
  725. };
  726. getTipsCombo.prototype.processMouseDown = function (hitinfo) {
  727. if (hitinfo.isReservedLocation == true) {//这里是点击了下拉框的三角形才会有这个属性
  728. TREE_SHEET_HELPER.hideTipsDiv();
  729. this.clickCom = true;
  730. }
  731. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  732. };
  733. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  734. TREE_SHEET_HELPER.hideTipsDiv();
  735. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  736. };
  737. }
  738. return new getTipsCombo();
  739. },
  740. getTreeNodeCellType: function (datas, row, parentMap) {// 2018-09-26 不用spreadjs默认的树结构,自定义控件
  741. var ns = GC.Spread.Sheets;
  742. let rectW = 10;
  743. let rectH = 10;
  744. let margin = 3;
  745. function TreeNodeCellType() {
  746. this.collapsed = gljUtil.isDef(datas[row].collapsed) ? datas[row].collapsed : true; //默认是折叠的
  747. this.treeNodeType = true;
  748. this.rectInfo = {};
  749. }
  750. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  751. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  752. let offset = 0;
  753. let step = 7;
  754. let level = getTreeLevel(datas[row], datas);//从0开始,取当前节点是第几级的
  755. let tem = offset + margin + rectW / 2 + step;//两条线之间的间隔
  756. let t_offset = offset;
  757. let temParentID = datas[row].ParentID;
  758. if (this.treeNodeType == true) {
  759. for (let i = level; i > 0; i--) {//这里是画子节点前面的竖线,从第二级开始
  760. let temParent = getParent(temParentID, datas);
  761. if (temParent) {//父节点有下一个兄弟节点才需要画
  762. if (hasNextBrother(parentMap, temParent)) sheetCommonObj.drawLine(ctx, x + t_offset + tem * i, y, x + t_offset + tem * i, y + h);
  763. temParentID = temParent.ParentID;
  764. }
  765. offset += tem;
  766. }
  767. }
  768. offset += step; //这个没法移动,所以要两个判断
  769. if (this.treeNodeType == true) {
  770. if (hasChildern(datas[row].ID, datas)) {//如果是有子节点
  771. //第一条 或者没有父节点(如费率子表综合里程项)不用画方框头上那条竖线其它都要
  772. 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);
  773. //画方框下面的那条竖线,如果没有下一个兄弟节点,则不用画
  774. 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);
  775. sheetCommonObj.drowRect(ctx, x + offset, y, w, h, rectW, rectH, margin);
  776. sheetCommonObj.drowSymbol(ctx, x + offset, y, w, h, rectW, rectH, margin, this.collapsed);
  777. this.rectInfo = { x: x + offset + margin, rectW: rectW }//计录一下可点击位置
  778. } else {
  779. let hasNext = datas[row + 1] ? datas[row + 1].ParentID == datas[row].ParentID : false;
  780. sheetCommonObj.drowSubItem(ctx, x, y, w, h, offset, hasNext, margin + rectW / 2);
  781. }
  782. }
  783. offset += step;
  784. offset += rectW;
  785. x = x + offset;//设置偏移
  786. w = w - offset;
  787. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  788. };
  789. // override getHitInfo to allow cell type get mouse messages
  790. TreeNodeCellType.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. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  802. ////方框外1像素内都有效
  803. if (!_.isEmpty(this.rectInfo) && Math.floor(hitinfo.x) <= this.rectInfo.x + this.rectInfo.rectW + 2 && Math.floor(hitinfo.x) >= this.rectInfo.x - 2) {
  804. this.collapsed = !this.collapsed;
  805. datas[row].collapsed = this.collapsed;
  806. this.refreshChildrenVisible(hitinfo.sheet);
  807. hitinfo.sheet.invalidateLayout();
  808. hitinfo.sheet.repaint();
  809. }
  810. };
  811. TreeNodeCellType.prototype.refreshChildrenVisible = function (sheet) {
  812. sheet.suspendPaint();
  813. sheet.suspendEvent();
  814. refreshVisible(datas[row]);
  815. sheet.resumeEvent();
  816. sheet.resumePaint();
  817. function refreshVisible(item) {
  818. if (parentMap[item.ID]) {
  819. for (let sub of parentMap[item.ID]) {
  820. refreshVisible(sub)
  821. }
  822. }
  823. let visible = getVisible(item);
  824. let trow = datas.indexOf(item);
  825. sheet.getRange(trow, -1, 1, -1).visible(visible);
  826. }
  827. function getVisible(item) {
  828. if (item.ParentID) {
  829. let parent = getParent(item.ParentID, datas);
  830. if (!parent) return true;
  831. let p_row = datas.indexOf(parent);
  832. let visible = !sheet.getCellType(p_row, 0).collapsed;
  833. if (visible == true) { //如果是显示的,则要再往父节点的父节点检查,只要有一个节点是隐藏的,则都是隐藏
  834. return getVisible(parent);
  835. } else {
  836. return visible
  837. }
  838. } else {//如果parentID 为空则是最根节点
  839. return true;
  840. }
  841. }
  842. };
  843. return new TreeNodeCellType()
  844. function getTreeLevel(item, data) {
  845. if (item.ParentID) {
  846. let pitem = _.find(data, { 'ID': item.ParentID });
  847. return getTreeLevel(pitem, data) + 1;
  848. } else {
  849. return 0
  850. }
  851. }
  852. function hasChildern(ID, data) {//返回是否有子项
  853. let p = _.find(data, { 'ParentID': ID });
  854. if (p) return true;
  855. return false
  856. }
  857. function getParent(ParentID, data) {
  858. let p = _.find(data, { 'ID': ParentID });
  859. return p;
  860. }
  861. function hasNextBrother(parentMap, item) {
  862. let children = parentMap[item.ParentID];
  863. if (!gljUtil.isDef(children) || children.indexOf(item) == children.length - 1) return false;
  864. return true
  865. }
  866. },
  867. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  868. let me = this;
  869. sheet.suspendPaint();
  870. let combo = me.getDynamicCombo();
  871. for (let i = 0, len = rowCount; i < len; i++) {
  872. if (itemsHeight) {
  873. combo.itemHeight(itemsHeight);
  874. combo._maxDropDownItems = itemsHeight + 5;
  875. }
  876. if (itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  877. else if (itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  878. else combo.items(items);
  879. sheet.getCell(beginRow + i, col).cellType(combo);
  880. }
  881. sheet.resumePaint();
  882. },
  883. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  884. sheet.suspendPaint();
  885. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  886. for (let i = 0, len = rowCount; i < len; i++) {
  887. if (itemsHeight) combo.itemHeight(itemsHeight);
  888. if (itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  889. else if (itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  890. else combo.items(items);
  891. sheet.getCell(beginRow + i, col).cellType(combo);
  892. }
  893. sheet.resumePaint();
  894. },
  895. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  896. copyTextToClipboard: function (text) {
  897. let textArea = document.createElement("textarea");
  898. textArea.style.position = 'fixed';
  899. textArea.style.top = 0;
  900. textArea.style.left = 0;
  901. textArea.style.width = '2em';
  902. textArea.style.height = '2em';
  903. textArea.style.padding = 0;
  904. textArea.style.border = 'none';
  905. textArea.style.outline = 'none';
  906. textArea.style.boxShadow = 'none';
  907. textArea.style.background = 'transparent';
  908. textArea.value = text;
  909. document.body.appendChild(textArea);
  910. textArea.select();
  911. try {
  912. let successful = document.execCommand('copy');
  913. let msg = successful ? 'successful' : 'unsuccessful';
  914. console.log('Copying text command was ' + msg);
  915. } catch (err) {
  916. console.log('Oops, unable to copy');
  917. }
  918. document.body.removeChild(textArea);
  919. },
  920. //获取选中区域的表格类型数据(可粘贴到excel)
  921. getTableData: function (sheet, colSettings = null) {
  922. let rst = '';
  923. let sel = sheet.getSelections()[0];
  924. let pasteText = [];
  925. for (let row = sel.row; row < sel.row + sel.rowCount; row++) {
  926. if (!sheet.getCell(row, -1).visible())
  927. continue;
  928. let rowText = [];
  929. for (let j = 0; j < sel.colCount; j++) {
  930. let col = sel.col + j;
  931. if (!sheet.getCell(-1, col).visible())
  932. continue;
  933. if (colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  934. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  935. else
  936. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col) : '');
  937. }
  938. pasteText.push(rowText.join('\t'));
  939. }
  940. return pasteText.join('\n');
  941. },
  942. transferToTreeSetting: function (setting, treeSetting, treeCol) {
  943. for (let h of setting.header) {
  944. treeSetting.cols.push(getSettingCol(h))
  945. }
  946. for (let l of setting.view.lockColumns) {
  947. treeSetting.cols[l].readOnly = true;
  948. }
  949. return treeSetting;
  950. function getSettingCol(header) {
  951. let aMap = { left: 0, center: 1, right: 2 };
  952. let hAlign = header.hAlign ? aMap[header.hAlign] : 0;
  953. let col = {
  954. "width": header.headerWidth ? header.headerWidth : 100,
  955. "head": {
  956. "titleNames": Array.isArray(header.headerName) ? header.headerName : [header.headerName],
  957. "spanCols": header.spanCols ? header.spanCols : [1],
  958. "spanRows": header.spanRows ? header.spanRows : [1],
  959. "vAlign": [1],
  960. "hAlign": [1],
  961. "font": ["Arial"]
  962. },
  963. "data": {
  964. "field": header.dataCode,
  965. "vAlign": 1,
  966. "hAlign": hAlign,
  967. "font": "Arial"
  968. }
  969. };
  970. if (header.showHint == true) {
  971. col.showHint = true;
  972. }
  973. if (header.cellType) {
  974. col.data.cellType = getCellType(header);
  975. }
  976. if (header.decimalField) {//设置formatter
  977. let decimal = getDecimal(header.decimalField);
  978. col.formatter = getFormatter(decimal);
  979. }
  980. if (header.getText && treeCol) {
  981. col.data.getText = treeCol.getEvent(header.getText);
  982. }
  983. /*col.readOnly = function (node) {
  984. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  985. return true;
  986. }
  987. return false;
  988. };*/
  989. return col;
  990. }
  991. function getCellType(header) {
  992. return function () {
  993. if (header.cellType === "checkBox") {
  994. return new GC.Spread.Sheets.CellTypes.CheckBox();
  995. }
  996. if (header.cellType === "comboBox") {
  997. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  998. if (header.options) {
  999. dynamicCombo.itemHeight(header.options.length).items(header.options);
  1000. if (header.editorValueType == true) {
  1001. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1002. }
  1003. }
  1004. return dynamicCombo
  1005. }
  1006. }
  1007. }
  1008. },
  1009. //注册自定义回车键事件
  1010. bindEnterKey: function (workBook, operation) {
  1011. workBook.commandManager().register('myEnter', operation);
  1012. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  1013. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  1014. },
  1015. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  1016. bindEscKey: function (workBook, sheets) {
  1017. function isDef(v) {
  1018. return typeof v !== 'undefined' && v !== null;
  1019. }
  1020. workBook.commandManager().register('myEsc', function () {
  1021. let activeSheet = workBook.getActiveSheet();
  1022. let hasTheSheet = false;
  1023. for (let sheetObj of sheets) {
  1024. let sheet = sheetObj.sheet;
  1025. if (sheet === activeSheet) {
  1026. hasTheSheet = true;
  1027. let editStarting = sheetObj.editStarting;
  1028. let editEnded = sheetObj.editEnded;
  1029. if (editStarting) {
  1030. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  1031. }
  1032. if (editEnded) {
  1033. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  1034. }
  1035. let row = sheet.getActiveRowIndex();
  1036. let col = sheet.getActiveColumnIndex();
  1037. let orgV = sheet.getValue(row, col);
  1038. let orgText = sheet.getText(row, col);
  1039. if (!isDef(orgV)) {
  1040. orgV = '';
  1041. }
  1042. if (sheet.isEditing()) {
  1043. sheet.endEdit();
  1044. sheet.setValue(row, col, orgV);
  1045. }
  1046. if (editStarting) {
  1047. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  1048. }
  1049. if (editEnded) {
  1050. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  1051. }
  1052. }
  1053. }
  1054. //容错处理,以防没把所有工作簿的表格信息传入参数
  1055. if (!hasTheSheet) {
  1056. if (activeSheet.isEditing()) {
  1057. activeSheet.endEdit();
  1058. }
  1059. }
  1060. });
  1061. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  1062. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  1063. },
  1064. //设置默认样式
  1065. spreadDefaultStyle: function (workBook) {
  1066. let defaultStyle = new GC.Spread.Sheets.Style();
  1067. defaultStyle.font = '14px Calibri';
  1068. let sheetCount = workBook.getSheetCount();
  1069. for (let i = 0; i < sheetCount; i++) {
  1070. let sheet = workBook.getSheet(i);
  1071. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  1072. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  1073. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  1074. }
  1075. },
  1076. //动态根据工作簿宽度和各列宽度比例设置宽度
  1077. setColumnWidthByRate: function (workBookWidth, workBook, headers) {
  1078. if (workBook) {
  1079. const sheet = workBook.getActiveSheet();
  1080. sheet.suspendEvent();
  1081. sheet.suspendPaint();
  1082. for (let col = 0; col < headers.length; col++) {
  1083. if (headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== '') {
  1084. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1085. }
  1086. else {
  1087. if (headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== '') {
  1088. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1089. }
  1090. }
  1091. }
  1092. sheet.resumeEvent();
  1093. sheet.resumePaint();
  1094. }
  1095. },
  1096. drowRect: function (ctx, x, y, w, h, rectW, rectH, margin) {
  1097. ctx.save();
  1098. ctx.strokeStyle = "gray";
  1099. ctx.translate(0.5, 0.5);
  1100. ctx.beginPath();
  1101. let rectX = x + margin;
  1102. let rectY = y + Math.round(h / 2) - rectH / 2;
  1103. ctx.moveTo(rectX, rectY);
  1104. ctx.lineTo(rectX, rectY + rectH);
  1105. ctx.lineTo(rectX + rectW, rectY + rectH);
  1106. ctx.lineTo(rectX + rectW, rectY);
  1107. ctx.lineTo(rectX, rectY);
  1108. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  1109. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  1110. ctx.stroke();
  1111. ctx.restore();
  1112. },
  1113. drawLine: function (ctx, x1, y1, x2, y2, color) {
  1114. let l_color = color ? color : "gray";
  1115. ctx.save();
  1116. ctx.translate(0.5, 0.5);
  1117. ctx.beginPath();
  1118. ctx.moveTo(x1, y1);
  1119. ctx.lineTo(x2, y2);
  1120. ctx.strokeStyle = l_color;
  1121. ctx.stroke();
  1122. ctx.restore();
  1123. },
  1124. drowSymbol: function (ctx, x, y, w, h, rectW, rectH, margin, collapsed) {
  1125. ctx.save();
  1126. ctx.strokeStyle = "#000000";
  1127. ctx.translate(0.5, 0.5);
  1128. ctx.beginPath();
  1129. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  1130. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  1131. let rectY = y + Math.round(h / 2) - rectH / 2;
  1132. if (collapsed) {
  1133. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  1134. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  1135. }
  1136. ctx.stroke();
  1137. ctx.restore();
  1138. },
  1139. drowTriangle: function (ctx, x, y) {//画向下三角形
  1140. ctx.save();
  1141. ctx.fillStyle = "black";
  1142. ctx.beginPath();
  1143. ctx.moveTo(x, y);
  1144. ctx.lineTo(x - 3, y - 6);
  1145. ctx.lineTo(x + 3, y - 6);
  1146. ctx.fill();
  1147. ctx.restore();
  1148. },
  1149. drowSubItem: function (ctx, x, y, w, h, offset, hasNext, step) {
  1150. let t_step = step ? step : 6;
  1151. offset += t_step;
  1152. ctx.save();
  1153. ctx.strokeStyle = "gray";
  1154. ctx.translate(0.5, 0.5);
  1155. ctx.beginPath();
  1156. ctx.moveTo(x + offset, y);
  1157. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1158. offset += 9;
  1159. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1160. if (hasNext) {
  1161. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  1162. ctx.lineTo(x + offset - 9, y + h);
  1163. }
  1164. ctx.stroke();
  1165. ctx.restore();
  1166. return offset;
  1167. },
  1168. checkData: function (col, setting, value) {
  1169. let result = true;
  1170. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  1171. if (validator === null) {
  1172. return result;
  1173. }
  1174. switch (validator) {
  1175. case 'number':
  1176. let regular = /^\d+(\.\d+)?$/;
  1177. result = regular.test(value);
  1178. break;
  1179. case 'boolean':
  1180. let booleanValue = [true, false];
  1181. result = booleanValue.indexOf(value) >= 0;
  1182. break;
  1183. }
  1184. return result;
  1185. },
  1186. getSelectedRecode: function (sheet, datas) {
  1187. let sel = sheet.getSelections()[0];
  1188. let srow = sel.row == -1 || sel.row == "" ? 0 : sel.row;
  1189. if (gljUtil.isDef(srow) && datas.length > srow) {
  1190. return datas[srow];
  1191. }
  1192. return null;
  1193. },
  1194. // 延迟一段时间刷新表格,因为有的弹窗里面有表格,马上刷新可能会造成,弹窗界面还未完全显示完就完成了表格刷新,导致表格显示不完整
  1195. refreshWorkbookDelDefer(workbook, time) {
  1196. if (workbook) {
  1197. setTimeout(() => workbook.refresh(), time);
  1198. }
  1199. },
  1200. setRowsAutoFit(sheet, rows, col, wordWrap) {
  1201. rows.forEach(row => {
  1202. sheet.getCell(row, col).wordWrap(wordWrap);
  1203. sheet.autoFitRow(row);
  1204. });
  1205. }
  1206. }