spreadjs_zh.js 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. /**
  2. * Spreadjs 通用方法集
  3. *
  4. * @author Mai
  5. * @date 2018/02/06
  6. * @version
  7. */
  8. // 定义画点线的方法
  9. const proto = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype;
  10. proto.dottedLine = function (x1, y1, x2, y2, interval = 4) {
  11. const isHorizontal = x1 == x2 ? false : true;
  12. const dotLen = 1;
  13. let len = isHorizontal ? x2 - x1 : y2 - y1;
  14. this.moveTo(x1, y1);
  15. let progress = 0;
  16. while (len > progress) {
  17. if (progress > len) { progress = len; }
  18. if (isHorizontal) {
  19. this.moveTo(x1 + progress, y1);
  20. this.lineTo(x1 + progress + dotLen, y1);
  21. } else {
  22. this.moveTo(x1, y1 + progress);
  23. this.lineTo(x1, y1 + progress + dotLen);
  24. }
  25. progress += interval;
  26. }
  27. };
  28. // 简写Spread常量
  29. const spreadNS = GC.Spread.Sheets;
  30. // SpreadJs常用方法
  31. const SpreadJsObj = {
  32. initSpreadSettingEvents: function (setting, events) {
  33. const getEvent = function (eventName) {
  34. const names = eventName.split('.');
  35. let event = events;
  36. for (let name of names) {
  37. if (event[name]) {
  38. event = event[name];
  39. } else {
  40. return null;
  41. }
  42. }
  43. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  44. return null;
  45. } else {
  46. return event;
  47. }
  48. };
  49. for (const col of setting.cols) {
  50. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  51. col.readOnly = getEvent(col.readOnly);
  52. }
  53. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
  54. col.getValue = getEvent(col.getValue);
  55. }
  56. }
  57. },
  58. DataType: {
  59. Data: 'data',
  60. Tree: 'tree',
  61. },
  62. /**
  63. * 创建Spread(默认1张表,3行数据)
  64. * @param obj 用于创建spreadjs的Dom元素
  65. * @returns {GC.Spread.Sheets.Workbook}
  66. */
  67. createNewSpread: function (obj) {
  68. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  69. spread.options.tabStripVisible = false;
  70. spread.options.scrollbarMaxAlign = true;
  71. spread.options.cutCopyIndicatorVisible = false;
  72. spread.options.allowCopyPasteExcelStyle = false;
  73. spread.options.allowUserDragDrop = false;
  74. spread.options.allowUserEditFormula = false;
  75. spread.getActiveSheet().options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  76. spread.getActiveSheet().setRowCount(3);
  77. return spread;
  78. },
  79. /**
  80. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  81. * @param {GC.Spread.Sheets.Worksheet} sheet
  82. */
  83. protectedSheet: function (sheet) {
  84. const option = {
  85. allowSelectLockedCells: true,
  86. allowSelectUnlockedCells: true,
  87. allowResizeRows: true,
  88. allowResizeColumns: true
  89. };
  90. sheet.options.protectionOptions = option;
  91. sheet.options.isProtected = true;
  92. sheet.options.allowCellOverflow = false;
  93. },
  94. /**
  95. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  96. * @param {GC.Spread.Sheets.Worksheet} sheet
  97. * @param {function} operation
  98. */
  99. beginMassOperation: function (sheet) {
  100. sheet.suspendPaint();
  101. sheet.suspendEvent();
  102. },
  103. endMassOperation: function (sheet) {
  104. sheet.resumeEvent();
  105. sheet.resumePaint();
  106. },
  107. massOperationSheet: function (sheet, operation) {
  108. this.beginMassOperation(sheet);
  109. operation();
  110. this.endMassOperation(sheet);
  111. },
  112. /**
  113. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  114. * @param obj
  115. * @returns {{x: number, y: number}}
  116. */
  117. getObjPos: function (obj) {
  118. let target = obj;
  119. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  120. target = obj.offsetParent;
  121. while (target) {
  122. pos.x += target.offsetLeft;
  123. pos.y += target.offsetTop;
  124. target = target.offsetParent;
  125. }
  126. return pos;
  127. },
  128. /**
  129. * 以下四个方法来自Spread示例, 参见官网或文档
  130. */
  131. getHitTest: function (obj, e, sheet) {
  132. var offset = obj.offset(),
  133. x = e.pageX - offset.left,
  134. y = e.pageY - offset.top;
  135. return sheet.hitTest(x, y);
  136. },
  137. getTargetSelection: function (sheet, target) {
  138. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  139. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  140. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  141. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  142. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  143. return sheet.getRange(target.row, target.col, 1, 1);
  144. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  145. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  146. };
  147. },
  148. getCellInSelections: function (selections, row, col) {
  149. const count = selections.length;
  150. let range;
  151. for (var i = 0; i < count; i++) {
  152. range = selections[i];
  153. if (range.contains(row, col)) {
  154. return range;
  155. }
  156. }
  157. return null;
  158. },
  159. checkTargetInSelection: function (selections, range) {
  160. var count = selections.length, sel;
  161. for (var i = 0; i < count; i++) {
  162. sel = selections[i];
  163. if (sel.containsRange(range)) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. },
  169. /**
  170. * 获取 spread 在鼠标右键时, spread的选中区域
  171. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  172. * colHeader, 选中整列
  173. * rowHeader, 选中整行
  174. * corner, 全选
  175. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  176. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  177. * @param e: jquery-contextmenu.build方法的第二个变量
  178. * @param {GC.Spread.Sheets.Workbook} spread
  179. * @returns {*}
  180. */
  181. safeRightClickSelection: function (obj, e, spread) {
  182. const sheet = spread.getActiveSheet();
  183. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  184. if (!this.checkTargetInSelection(selections, range)) {
  185. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  186. }
  187. return target;
  188. },
  189. /**
  190. * 获取写入sheet的数据序列
  191. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  192. * @param sheet
  193. * @returns {*}
  194. */
  195. getSortData: function (sheet) {
  196. if (sheet.zh_dataType) {
  197. if (sheet.zh_dataType === this.DataType.Data) {
  198. return sheet.zh_data;
  199. } else if (sheet.zh_dataType === this.DataType.Tree) {
  200. return sheet.zh_tree.nodes;
  201. } else {
  202. return null;
  203. }
  204. } else {
  205. return null;
  206. }
  207. },
  208. /**
  209. * sheet中 使用delete键,触发EndEdited事件
  210. * @param {GC.Spreads.Sheets.Workbook} spread
  211. * @param {function} fun
  212. */
  213. addDeleteBind: function (spread, fun) {
  214. spread.commandManager().register('deleteEvent', function () {
  215. fun(spread.getActiveSheet());
  216. });
  217. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  218. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  219. },
  220. _initSheetDeafult: function (sheet) {
  221. if (sheet.zh_setting.headerFont) {
  222. const vStyle = new spreadNS.Style();
  223. vStyle.font = sheet.zh_setting.headerFont;
  224. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  225. }
  226. if (sheet.zh_setting.font) {
  227. const vStyle = new spreadNS.Style();
  228. vStyle.font = sheet.zh_setting.font;
  229. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  230. }
  231. },
  232. /**
  233. * 根据sheet.zh_setting初始化sheet表头
  234. * @param {GC.Spread.Sheets.Worksheet} sheet
  235. */
  236. _initSheetHeader: function (sheet) {
  237. if (!sheet.zh_setting) { return; }
  238. sheet.setColumnCount(sheet.zh_setting.cols.length);
  239. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  240. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  241. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  242. }
  243. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  244. const col = sheet.zh_setting.cols[iCol];
  245. const title = col.title.split('|');
  246. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  247. for (let i = 0; i < title.length; i++) {
  248. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  249. cell.text(title[i]).wordWrap(true);
  250. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  251. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  252. }
  253. }
  254. sheet.setColumnWidth(iCol, col.width);
  255. if (col.visible !== undefined && col.visible !== null) {
  256. sheet.setColumnVisible(iCol, col.visible);
  257. }
  258. }
  259. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  260. sheet.showRowOutline(false);
  261. if (sheet.zh_setting.defaultRowHeight) {
  262. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  263. }
  264. },
  265. /**
  266. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  267. * @param {GC.Spread.Sheets.Worksheet} sheet
  268. * @param setting
  269. */
  270. initSheet: function (sheet, setting) {
  271. this.beginMassOperation(sheet);
  272. sheet.zh_setting = setting;
  273. this._initSheetDeafult(sheet);
  274. this._initSheetHeader(sheet);
  275. sheet.setRowCount(sheet.zh_setting.emptyRows);
  276. sheet.extendCellType = {};
  277. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  278. this.endMassOperation(sheet);
  279. },
  280. _loadRowData: function (sheet, data, row) {
  281. // 单元格重新写入数据
  282. if (!data) { return }
  283. sheet.zh_setting.cols.forEach(function (col, j) {
  284. const cell = sheet.getCell(row, j);
  285. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  286. cell.value(col.getValue(data));
  287. } else if (col.field !== '' && data[col.field]) {
  288. cell.value(data[col.field]);
  289. }
  290. if (col.font) {
  291. cell.font(col.font);
  292. }
  293. if (col.foreColor) {
  294. cell.foreColor(col.foreColor);
  295. }
  296. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  297. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  298. } else {
  299. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  300. }
  301. if (col.formatter) {
  302. cell.formatter(col.formatter);
  303. }
  304. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  305. cell.backColor(sheet.zh_setting.getColor(data, col, sheet.getDefaultStyle().backColor));
  306. }
  307. });
  308. },
  309. _defineColCellType: function (sheet, col, colSetting) {
  310. if(colSetting.cellType === 'ellipsis') {
  311. if (!sheet.extendCellType.ellipsis) {
  312. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  313. }
  314. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  315. }
  316. if(colSetting.cellType === 'html') {
  317. if (!sheet.extendCellType.html) {
  318. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  319. }
  320. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  321. }
  322. if (colSetting.cellType === 'image') {
  323. if (!sheet.extendCellType.image) {
  324. sheet.extendCellType.image = this.CellType.getImageCellType();
  325. }
  326. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  327. }
  328. if (colSetting.cellType === 'imageBtn') {
  329. if (!sheet.extendCellType.image) {
  330. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  331. }
  332. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  333. }
  334. if (colSetting.cellType === 'tree') {
  335. if (!sheet.extendCellType.tree) {
  336. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  337. }
  338. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  339. }
  340. if (colSetting.cellType === 'tip') {
  341. if (!sheet.extendCellType.tip) {
  342. sheet.extendCellType.tip = this.CellType.getTipCellType();
  343. }
  344. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  345. }
  346. if (colSetting.cellType === 'checkbox') {
  347. if (!sheet.extendCellType.checkbox) {
  348. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  349. }
  350. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  351. }
  352. if (colSetting.cellType === 'unit') {
  353. if (!sheet.extendCellType.unit) {
  354. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  355. }
  356. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  357. }
  358. if (colSetting.formatter) {
  359. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  360. }
  361. },
  362. /**
  363. * 整个sheet重新加载数据
  364. * @param {GC.Spread.Sheets.Worksheet} sheet
  365. */
  366. reLoadSheetData: function (sheet) {
  367. const self = this;
  368. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  369. this.beginMassOperation(sheet);
  370. try {
  371. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data)
  372. // 设置总行数
  373. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  374. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  375. // 控制空白行
  376. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  377. emptyRows.locked(sheet.zh_dataType === 'tree');
  378. // 单元格写入数据
  379. sortData.forEach(function (data, i) {
  380. self._loadRowData(sheet, data, i);
  381. sheet.setRowVisible(i, data.visible);
  382. });
  383. // 设置列单元格格式
  384. sheet.zh_setting.cols.forEach(function (col, j) {
  385. //if (!col.cellType) { return; }
  386. self._defineColCellType(sheet, j, col);
  387. });
  388. this.endMassOperation(sheet);
  389. } catch (err) {
  390. this.endMassOperation(sheet);
  391. }
  392. },
  393. /**
  394. * 重新加载部分数据行
  395. * @param {GC.Spread.Sheets.Worksheet} sheet
  396. * @param {Number} row
  397. * @param {Number} count
  398. */
  399. reLoadRowData: function (sheet, row, count = 1) {
  400. //if (row < 0) { return; }
  401. const self = this;
  402. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  403. this.beginMassOperation(sheet);
  404. try {
  405. // 清空原单元格数据
  406. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  407. // 单元格重新写入数据
  408. for (let i = row; i < row + count; i++) {
  409. const data = sortData[i];
  410. if (!data) { continue; }
  411. this._loadRowData(sheet, data, i);
  412. }
  413. this.endMassOperation(sheet);
  414. } catch (err) {
  415. this.endMassOperation(sheet);
  416. }
  417. },
  418. /**
  419. * 重新加载部分行数据
  420. * @param {GC.Spread.Sheets.Worksheet} sheet
  421. * @param {Array} rows
  422. */
  423. reLoadRowsData: function (sheet, rows) {
  424. const self = this;
  425. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  426. this.beginMassOperation(sheet);
  427. try {
  428. for (const row of rows) {
  429. if (row < 0) { continue; }
  430. // 清空原单元格数据
  431. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  432. const data = sortData[row];
  433. // 单元格重新写入数据
  434. this._loadRowData(sheet, data, row);
  435. };
  436. this.endMassOperation(sheet);
  437. } catch (err) {
  438. this.endMassOperation(sheet);
  439. }
  440. },
  441. /**
  442. * 重新加载部分列数据
  443. * @param {GC.Spread.Sheets.Worksheet} sheet
  444. * @param {Array} cols
  445. */
  446. reLoadColsData: function (sheet, cols) {
  447. const self = this;
  448. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  449. this.beginMassOperation(sheet);
  450. try {
  451. for (const iCol of cols) {
  452. // 清空原单元格数据
  453. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  454. const col = sheet.zh_setting.cols[iCol];
  455. sortData.forEach(function (data, i) {
  456. // 设置值
  457. const cell = sheet.getCell(i, iCol);
  458. if (col.field !== '' && data[col.field]) {
  459. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  460. } else {
  461. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  462. }
  463. // 设置单元格格式
  464. if (col.formatter) {
  465. cell.formatter(col.formatter);
  466. }
  467. });
  468. }
  469. this.endMassOperation(sheet);
  470. } catch (err) {
  471. this.endMassOperation(sheet);
  472. }
  473. },
  474. reLoadNodesData: function (sheet, nodes) {
  475. this.beginMassOperation(sheet);
  476. nodes = nodes instanceof Array ? nodes : [nodes];
  477. for (const node of nodes) {
  478. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  479. this._loadRowData(sheet, node, sortData.indexOf(node));
  480. }
  481. this.endMassOperation(sheet);
  482. },
  483. /**
  484. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  485. * @param {GC.Spread.Sheets.Worksheet} sheet
  486. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  487. * @param {Array|PathTree} data - 对dataType对应
  488. */
  489. loadSheetData: function (sheet, dataType, data){
  490. sheet.zh_dataType = dataType;
  491. if (dataType === 'tree') {
  492. sheet.zh_tree = data;
  493. } else {
  494. sheet.zh_data = data;
  495. }
  496. this.protectedSheet(sheet);
  497. this.reLoadSheetData(sheet);
  498. },
  499. /**
  500. * 获取复制数据HTML格式(过滤不可见单元格)
  501. * @param {GC.Spread.Sheets.Worksheet} sheet
  502. * @returns {string}
  503. */
  504. getFilterCopyHTML: function (sheet) {
  505. const sel = sheet.getSelections()[0];
  506. const html = [];
  507. html.push('<table>');
  508. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  509. // 跳过隐藏行
  510. if (!sheet.getCell(i, -1).visible()) { continue; }
  511. const rowHtml = [];
  512. rowHtml.push('<tr>');
  513. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  514. const data = sheet.getText(i, j);
  515. rowHtml.push('<td>' + data + '</td>');
  516. }
  517. rowHtml.push('</tr>');
  518. html.push(rowHtml.join(''));
  519. }
  520. html.push('</table>');
  521. return html.join('');
  522. },
  523. /**
  524. * 获取复制数据Text格式(过滤不可见单元格)
  525. * @param {GC.Spread.Sheets.Worksheet} sheet
  526. * @returns {string}
  527. */
  528. getFilterCopyText: function (sheet) {
  529. const copyData = [];
  530. const sel = sheet.getSelections()[0];
  531. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  532. // 跳过隐藏行
  533. if (!sheet.getCell(i, -1).visible()) { continue; }
  534. const rowText = [];
  535. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  536. const data = sheet.getText(i, j);
  537. rowText.push(data);
  538. }
  539. copyData.push(rowText.join('\t'));
  540. }
  541. return copyData.join('\n');
  542. },
  543. /**
  544. * 树表结构,定位至指定的节点
  545. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  546. * @param {Number} id - 定位节点的id
  547. */
  548. locateTreeNode: function (sheet, id) {
  549. const tree = sheet.zh_tree;
  550. if (!tree) { return }
  551. const node = tree.getItems(id);
  552. if (!node) { return }
  553. const index = tree.nodes.indexOf(node);
  554. const sels = sheet.getSelections();
  555. sheet.setSelection(index, sels[0].col, 1, 1);
  556. sheet.showRow(index, spreadNS.VerticalPosition.center);
  557. },
  558. /**
  559. * 获取当前选行的数据对象
  560. * @param {GC.Spread.Sheets.Worksheet} sheet
  561. * @returns {Object}
  562. */
  563. getSelectObject: function (sheet) {
  564. if (!sheet) {
  565. return null;
  566. } else if (sheet.zh_dataType) {
  567. const sel = sheet.getSelections()[0];
  568. if (sheet.zh_dataType === this.DataType.Tree) {
  569. return sheet.zh_tree.nodes[sel.row];
  570. } else if (sheet.zh_dataType === this.DataType.Data) {
  571. return sheet.zh_data[sel.row];
  572. } else {
  573. return null;
  574. }
  575. }
  576. },
  577. /**
  578. * 刷新列显示
  579. * @param sheet
  580. */
  581. refreshColumnVisible: function (sheet) {
  582. if(sheet.zh_setting) {
  583. sheet.zh_setting.cols.forEach(function (col, index) {
  584. if (col.visible !== undefined && col.visible !== null) {
  585. sheet.setColumnVisible(index, col.visible);
  586. }
  587. });
  588. }
  589. },
  590. /**
  591. * 刷新行显示
  592. * @param sheet
  593. */
  594. refreshTreeRowVisible: function (sheet) {
  595. this.beginMassOperation(sheet);
  596. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  597. for (const iRow in sortData) {
  598. const node = sortData[iRow];
  599. if (node.visible !== undefined && node.visible !== null) {
  600. sheet.setRowVisible(iRow, node.visible);
  601. } else {
  602. sheet.setRowVisible(iRow, true);
  603. }
  604. }
  605. // if (sheet.zh_tree) {
  606. // for (const iRow in sheet.zh_tree.nodes) {
  607. // const node = sheet.zh_tree.nodes[iRow];
  608. // if (node.visible !== undefined && node.visible !== null) {
  609. // sheet.setRowVisible(iRow, node.visible);
  610. // } else {
  611. // sheet.setRowVisible(iRow, true);
  612. // }
  613. // }
  614. // }
  615. this.endMassOperation(sheet);
  616. },
  617. /**
  618. * 刷新列是否只读
  619. * @param sheet
  620. * @param field
  621. * @param readonly
  622. */
  623. resetFieldReadOnly: function (sheet, field, readonly) {
  624. const fields = field instanceof Array ? field : [field];
  625. if (sheet.zh_setting) {
  626. sheet.zh_setting.cols.forEach(function (col, i) {
  627. if (fields.indexOf(col.field) !== -1) {
  628. col.readOnly = readonly;
  629. sheet.getRange(-1, i, -1, 1).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  630. }
  631. });
  632. }
  633. },
  634. CellType: {
  635. /**
  636. * 获取树结构CellType
  637. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  638. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  639. * @returns {TreeNodeCellType}
  640. */
  641. getTreeNodeCellType: function () {
  642. const indent = 20;
  643. const levelIndent = -5;
  644. const halfBoxLength = 5;
  645. const halfExpandLength = 3;
  646. /**
  647. * 画一条点线段
  648. * @param canvas - 画布
  649. * @param x1 - 线段起点 x
  650. * @param y1 - 线段起点 y
  651. * @param x2 - 线段终点 x
  652. * @param y2 - 线段终点 y
  653. * @param color - 线段颜色
  654. */
  655. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  656. canvas.save();
  657. // 设置偏移量
  658. canvas.translate(0.5, 0.5);
  659. canvas.beginPath();
  660. canvas.strokeStyle = color;
  661. canvas.dottedLine(x1, y1, x2, y2);
  662. canvas.stroke();
  663. canvas.restore();
  664. };
  665. /**
  666. * 画一条线段
  667. * @param canvas - 画布
  668. * @param x1 - 线段起点 x
  669. * @param y1 - 线段起点 y
  670. * @param x2 - 线段终点 x
  671. * @param y2 - 线段终点 y
  672. * @param color - 线段颜色
  673. */
  674. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  675. canvas.save();
  676. // 设置偏移量
  677. canvas.translate(0.5, 0.5);
  678. canvas.beginPath();
  679. canvas.moveTo(x1, y1);
  680. canvas.lineTo(x2, y2);
  681. canvas.strokeStyle = color;
  682. canvas.stroke();
  683. canvas.restore();
  684. };
  685. /**
  686. * 画一个方框
  687. * @param {Object} canvas - 画布
  688. * @param {Object} rect - 方框区域
  689. * @param {String} lineColor - 画线颜色
  690. * @param {String} fillColor - 填充颜色
  691. */
  692. const drawBox = function (canvas, rect, lineColor, fillColor) {
  693. canvas.save();
  694. // 设置偏移量
  695. canvas.translate(0.5, 0.5);
  696. canvas.strokeStyle = lineColor;
  697. canvas.beginPath();
  698. canvas.moveTo(rect.left, rect.top);
  699. canvas.lineTo(rect.left, rect.bottom);
  700. canvas.lineTo(rect.right, rect.bottom);
  701. canvas.lineTo(rect.right, rect.top);
  702. canvas.lineTo(rect.left, rect.top);
  703. canvas.stroke();
  704. canvas.fillStyle = fillColor;
  705. canvas.fill();
  706. canvas.restore();
  707. };
  708. /**
  709. * 画树结构-展开收起按钮
  710. * @param {Object} canvas - 画布
  711. * @param {Number} x - 单元格左顶点坐标 x
  712. * @param {Number} y - 单元格左顶点坐标 y
  713. * @param {Number} w - 单元格宽度
  714. * @param {Number} h - 单元格高度
  715. * @param {Number} centerX - 按钮中央坐标
  716. * @param {Number} centerY - 按钮中央坐标
  717. * @param {Boolean} expanded - 当前节点展开收起状态
  718. */
  719. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  720. let rect = {
  721. top: centerY - halfBoxLength,
  722. bottom: centerY + halfBoxLength,
  723. left: centerX - halfBoxLength,
  724. right: centerX + halfBoxLength
  725. };
  726. let h1, h2, offset = 1;
  727. if (rect.left < x + w) {
  728. // 方框超出单元格宽度时,超出部分不画。
  729. rect.right = Math.min(rect.right, x + w);
  730. drawBox(canvas, rect, '#808080', 'white');
  731. // 画中心十字
  732. // 画十字横线
  733. h1 = centerX - halfExpandLength;
  734. h2 = Math.min(centerX + halfExpandLength, x + w);
  735. if (h2 > h1) {
  736. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  737. }
  738. // 画十字竖线
  739. if (!expanded && (centerX < x + w)) {
  740. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  741. }
  742. }
  743. };
  744. let TreeNodeCellType = function (){};
  745. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  746. const proto = TreeNodeCellType.prototype;
  747. /**
  748. * 绘制方法
  749. * @param {Object} canvas - 画布
  750. * @param value - cell.value
  751. * @param {Number} x - 单元格左顶点坐标 x
  752. * @param {Number} y - 单元格左顶点坐标 y
  753. * @param {Number} w - 单元格宽度
  754. * @param {Number} h - 单元格高度
  755. * @param {Object} style - cell.style
  756. * @param {Object} options
  757. */
  758. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  759. // 清理 画布--单元格范围 旧数据
  760. if (style.backColor) {
  761. canvas.save();
  762. canvas.fillStyle = style.backColor;
  763. canvas.fillRect(x, y, w, h);
  764. canvas.restore();
  765. } else {
  766. canvas.clearRect(x, y, w, h);
  767. }
  768. const tree = options.sheet.zh_tree;
  769. // 使用TreeCellType前,需定义sheet.tree
  770. if (tree) {
  771. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  772. if (node) {
  773. const showTreeLine = true;
  774. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  775. const centerY = Math.floor((y + (y + h)) / 2);
  776. // Draw Sibling Line
  777. if (showTreeLine) {
  778. // Draw Horizontal Line
  779. if (centerX < x + w) {
  780. const x1 = centerX + indent / 2;
  781. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  782. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  783. }
  784. // Draw Vertical Line
  785. if (centerX < x + w) {
  786. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  787. const parent = tree.getParent(node);
  788. const y2 = y1 - centerY;
  789. if (node.order === 1 && !parent) {
  790. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  791. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  792. } else {
  793. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  794. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  795. }
  796. }
  797. }
  798. // Draw Expand Box
  799. if (!node.is_leaf) {
  800. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  801. }
  802. // Draw Parent Line
  803. if (showTreeLine) {
  804. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  805. while (parent) {
  806. if (!tree.isLastSibling(parent)) {
  807. if (parentCenterX < x + w) {
  808. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  809. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  810. }
  811. }
  812. parent = tree.getParent(parent);
  813. parentCenterX -= (indent + levelIndent);
  814. }
  815. };
  816. // 重定位x
  817. const move = (node.level + 1) * indent + (node.level) * levelIndent;
  818. x = x + move;
  819. w = w - move;
  820. }
  821. }
  822. // Drawing Text
  823. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  824. };
  825. /**
  826. * 获取点击信息
  827. * @param {Number} x
  828. * @param {Number} y
  829. * @param {Object} cellStyle
  830. * @param {Object} cellRect
  831. * @param {Object} context
  832. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  833. */
  834. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  835. return {
  836. x: x,
  837. y: y,
  838. row: context.row,
  839. col: context.col,
  840. cellStyle: cellStyle,
  841. cellRect: cellRect,
  842. sheet: context.sheet,
  843. sheetArea: context.sheetArea
  844. };
  845. };
  846. /**
  847. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  848. * @param {Object} hitinfo - 见getHitInfo
  849. */
  850. proto.processMouseDown = function (hitinfo) {
  851. const offset = -1;
  852. const tree = hitinfo.sheet.zh_tree;
  853. if (!tree) { return; }
  854. const node = tree.nodes[hitinfo.row];
  855. if (!node) { return; }
  856. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  857. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  858. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  859. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  860. const children = tree.getChildren(node);
  861. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  862. tree.loadChildren(node, function () {
  863. node.expanded = true;
  864. const children = tree.getChildren(node);
  865. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  866. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  867. });
  868. } else {
  869. tree.setExpanded(node, !node.expanded);
  870. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  871. const posterity = tree.getPosterity(node);
  872. for (const child of posterity) {
  873. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  874. }
  875. });
  876. hitinfo.sheet.repaint();
  877. }
  878. }
  879. };
  880. return new TreeNodeCellType();
  881. },
  882. /**
  883. * 获取 带悬浮提示的CellType
  884. * @returns {TipCellType}
  885. */
  886. getTipCellType: function () {
  887. const TipCellType = function () {};
  888. // 继承 SpreadJs定义的 普通的TextCellType
  889. TipCellType.prototype = new spreadNS.CellTypes.Text();
  890. const proto = TipCellType.prototype;
  891. /**
  892. * 获取点击信息
  893. * @param {Number} x
  894. * @param {Number} y
  895. * @param {Object} cellStyle
  896. * @param {Object} cellRect
  897. * @param {Object} context
  898. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  899. */
  900. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  901. return {
  902. x: x,
  903. y: y,
  904. row: context.row,
  905. col: context.col,
  906. cellStyle: cellStyle,
  907. cellRect: cellRect,
  908. sheet: context.sheet,
  909. sheetArea: context.sheetArea
  910. };
  911. };
  912. /**
  913. * 鼠标进入单元格事件 - 显示悬浮提示
  914. * @param {Object} hitinfo - 见getHitInfo返回值
  915. */
  916. proto.processMouseEnter = function (hitinfo) {
  917. const text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  918. const setting = hitinfo.sheet.setting;
  919. if (setting.pos && text && text !== '') {
  920. if (!this._toolTipElement) {
  921. let div = $('#autoTip')[0];
  922. if (!div) {
  923. div = document.createElement("div");
  924. $(div).css("position", "absolute")
  925. .css("border", "1px #C0C0C0 solid")
  926. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  927. .css("font", "9pt Arial")
  928. .css("background", "white")
  929. .css("padding", 5)
  930. .attr("id", 'autoTip');
  931. $(div).hide();
  932. document.body.insertBefore(div, null);
  933. }
  934. this._toolTipElement = div;
  935. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  936. $(this._toolTipElement).show("fast");
  937. }
  938. }
  939. };
  940. /**
  941. * 鼠标移出单元格事件 - 隐藏悬浮提示
  942. * @param {Object} hitinfo - 见getHitInfo返回值
  943. */
  944. proto.processMouseLeave = function (hitinfo) {
  945. if (this._toolTipElement) {
  946. $(this._toolTipElement).hide();
  947. this._toolTipElement = null;
  948. }
  949. };
  950. return new TipCellType();
  951. },
  952. /**
  953. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  954. *
  955. * img:
  956. * 1. 整列固定,则传入img的select
  957. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  958. *
  959. * 2. 各单元格自定义,则
  960. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  961. * function getAttachmentImage (data) {
  962. * $('#attachment-img').url = data.attachmentImageUrl;
  963. * return $('#attachment-img')[0];
  964. * }
  965. *
  966. * @returns {ImageCellType}
  967. */
  968. getImageCellType: function () {
  969. const indent = 10;
  970. const ImageCellType = function (){};
  971. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  972. const proto = ImageCellType.prototype;
  973. proto.getImage = function (sheet, iRow, iCol) {
  974. const col = sheet.zh_setting.cols[iCol];
  975. let imgSource = col.img;
  976. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  977. const sortData = SpreadJsObj.getSortData(sheet);
  978. const data = sortData ? sortData[iRow] : null;
  979. return data ? imgSource(data) : null;
  980. } else {
  981. return $(imgSource)[0] ? $(imgSource)[0] : null;
  982. }
  983. };
  984. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  985. const img = this.getImage(options.sheet, options.row, options.col);
  986. if (img) {
  987. if (style.backColor) {
  988. canvas.save();
  989. canvas.fillStyle = style.backColor;
  990. canvas.fillRect(x, y, indent + img.width, h);
  991. canvas.restore();
  992. }
  993. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  994. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  995. style.hAlign = spreadNS.HorizontalAlign.left;
  996. }
  997. x = x + indent + img.width;
  998. w = w - indent - img.width;
  999. }
  1000. // Drawing Text
  1001. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1002. };
  1003. /**
  1004. * 获取点击信息
  1005. * @param {Number} x
  1006. * @param {Number} y
  1007. * @param {Object} cellStyle
  1008. * @param {Object} cellRect
  1009. * @param {Object} context
  1010. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1011. */
  1012. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1013. return {
  1014. x: x,
  1015. y: y,
  1016. row: context.row,
  1017. col: context.col,
  1018. cellStyle: cellStyle,
  1019. cellRect: cellRect,
  1020. sheet: context.sheet,
  1021. sheetArea: context.sheetArea
  1022. };
  1023. };
  1024. /**
  1025. * 鼠标点击
  1026. * @param {Object} hitinfo - 见getHitInfo
  1027. */
  1028. proto.processMouseDown = function (hitinfo) {
  1029. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1030. if (img) {
  1031. const halfX = img.width / 2, halfY = img.height / 2;
  1032. const centerX = hitinfo.cellRect.x + indent + halfX;
  1033. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1034. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1035. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1036. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1037. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1038. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1039. const data = sortData ? sortData[hitinfo.row] : null;
  1040. imageClick(data);
  1041. }
  1042. }
  1043. }
  1044. };
  1045. return new ImageCellType();
  1046. },
  1047. /**
  1048. *
  1049. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1050. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1051. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1052. *
  1053. * @returns {ImageCellType}
  1054. */
  1055. getImageButtonCellType: function () {
  1056. let hover = 1, active = 2;
  1057. const ImageCellType = function (){};
  1058. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1059. const proto = ImageCellType.prototype;
  1060. proto.getImage = function (sheet, iRow, iCol) {
  1061. const col = sheet.zh_setting.cols[iCol];
  1062. let imgSource = col.normalImg;
  1063. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1064. if (tag === active) {
  1065. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1066. } else if (tag === hover) {
  1067. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1068. }
  1069. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1070. const sortData = SpreadJsObj.getSortData(sheet);
  1071. const data = sortData ? sortData[iRow] : null;
  1072. return data ? imgSource(data) : null;
  1073. } else {
  1074. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1075. }
  1076. };
  1077. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1078. const col = options.sheet.zh_setting.cols[options.col];
  1079. const sortData = SpreadJsObj.getSortData(options.sheet);
  1080. const data = sortData ? sortData[options.row] : null;
  1081. let showImage = true;
  1082. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1083. showImage = col.showImage(data);
  1084. }
  1085. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1086. const indent = col.indent ? col.indent : 10;
  1087. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1088. if (img) {
  1089. if (style.backColor) {
  1090. canvas.save();
  1091. canvas.fillStyle = style.backColor;
  1092. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1093. canvas.restore();
  1094. }
  1095. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1096. w = w - indent - img.width;
  1097. }
  1098. // Drawing Text
  1099. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1100. } else {
  1101. if (img) {
  1102. if (style.backColor) {
  1103. canvas.save();
  1104. canvas.fillStyle = style.backColor;
  1105. canvas.fillRect(x, y, indent + img.width, h);
  1106. canvas.restore();
  1107. }
  1108. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1109. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1110. style.hAlign = spreadNS.HorizontalAlign.left;
  1111. }
  1112. x = x + indent + img.width;
  1113. w = w - indent - img.width;
  1114. }
  1115. // Drawing Text
  1116. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1117. }
  1118. };
  1119. /**
  1120. * 获取点击信息
  1121. * @param {Number} x
  1122. * @param {Number} y
  1123. * @param {Object} cellStyle
  1124. * @param {Object} cellRect
  1125. * @param {Object} context
  1126. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1127. */
  1128. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1129. return {
  1130. x: x,
  1131. y: y,
  1132. row: context.row,
  1133. col: context.col,
  1134. cellStyle: cellStyle,
  1135. cellRect: cellRect,
  1136. sheet: context.sheet,
  1137. sheetArea: context.sheetArea
  1138. };
  1139. };
  1140. /**
  1141. * 鼠标点击
  1142. * @param {Object} hitinfo - 见getHitInfo
  1143. */
  1144. proto.processMouseEnter = function (hitinfo) {
  1145. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1146. // Drawing Image
  1147. if (col.hoverImg) {
  1148. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1149. cell.tag(hover);
  1150. hitinfo.sheet.repaint(hitinfo.cellRect);
  1151. }
  1152. };
  1153. proto.processMouseLeave = function (hitinfo) {
  1154. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1155. // Drawing Image
  1156. if (col.hoverImg) {
  1157. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1158. cell.tag(null);
  1159. hitinfo.sheet.repaint(hitinfo.cellRect);
  1160. }
  1161. };
  1162. proto.processMouseDown = function (hitinfo) {
  1163. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1164. if (col.activeImg) {
  1165. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1166. cell.tag(active);
  1167. hitinfo.sheet.repaint(hitinfo.cellRect);
  1168. }
  1169. };
  1170. proto.processMouseUp = function (hitinfo) {
  1171. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1172. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1173. const data = sortData ? sortData[hitinfo.row] : null;
  1174. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1175. if (!col.showImage(data)) {
  1176. return;
  1177. }
  1178. }
  1179. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1180. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1181. imageClick(data);
  1182. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1183. cell.tag(null);
  1184. hitinfo.sheet.repaint(hitinfo.cellRect);
  1185. }
  1186. };
  1187. /*
  1188. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1189. */
  1190. // proto.processMouseDown = function (hitinfo) {
  1191. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1192. // const halfX = img.width / 2, halfY = img.height / 2;
  1193. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1194. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1195. //
  1196. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1197. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1198. // cell.tag(down);
  1199. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1200. // }
  1201. // };
  1202. // proto.processMouseUp = function (hitinfo) {
  1203. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1204. // const halfX = img.width / 2, halfY = img.height / 2;
  1205. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1206. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1207. //
  1208. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1209. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1210. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1211. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1212. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1213. // const data = sortData ? sortData[hitinfo.row] : null;
  1214. // imageClick(data);
  1215. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1216. // cell.tag(null);
  1217. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1218. // }
  1219. // }
  1220. // };
  1221. // proto.processMouseMove = function (hitinfo) {
  1222. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1223. // const halfX = img.width / 2, halfY = img.height / 2;
  1224. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1225. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1226. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1227. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1228. // if (cell.tag() !== hover) {
  1229. // cell.tag(hover);
  1230. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1231. // }
  1232. // } else {
  1233. // if (cell.tag() === hover) {
  1234. // cell.tag(null);
  1235. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1236. // }
  1237. // }
  1238. // };
  1239. return new ImageCellType();
  1240. },
  1241. /**
  1242. * 获取 嵌入Html的cellType
  1243. * @returns {HTMLCellType}
  1244. */
  1245. getHtmlCellType: function () {
  1246. const HTMLCellType = function (){};
  1247. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1248. const proto = ImageCellType.prototype;
  1249. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1250. let DOMURL = window.URL || window.webkitURL || window;
  1251. let cell = context.sheet.getCell(context.row, context.col);
  1252. let img = cell.tag();
  1253. if (img) {
  1254. try {
  1255. ctx.save();
  1256. ctx.rect(x, y, w, h);
  1257. ctx.clip();
  1258. ctx.drawImage(img, x + 2, y + 2)
  1259. ctx.restore();
  1260. cell.tag(null);
  1261. return;
  1262. }
  1263. catch (err) {
  1264. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1265. cell.tag(null);
  1266. return;
  1267. }
  1268. }
  1269. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1270. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1271. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1272. let doc = document.implementation.createHTMLDocument("");
  1273. doc.write(data);
  1274. // Get well-formed markup
  1275. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1276. img = new Image();
  1277. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1278. //var url = DOMURL.createObjectURL(svg);
  1279. //img.src = url;
  1280. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1281. cell.tag(img);
  1282. img.onload = function () {
  1283. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1284. }
  1285. };
  1286. return new HTMLCellType();
  1287. },
  1288. /**
  1289. * 获取 字符超长缩略的cellType
  1290. * @returns {EllipsisTextCellType}
  1291. */
  1292. getEllipsisTextCellType: function () {
  1293. const EllipsisTextCellType = function (){};
  1294. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1295. const proto = EllipsisTextCellType.prototype;
  1296. const getEllipsisText = function(c, str, maxWidth) {
  1297. var width = c.measureText(str).width;
  1298. var ellipsis = '…';
  1299. var ellipsisWidth = c.measureText(ellipsis).width;
  1300. if (width <= maxWidth || width <= ellipsisWidth) {
  1301. return str;
  1302. } else {
  1303. var len = str.length;
  1304. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1305. str = str.substring(0, len);
  1306. width = c.measureText(str).width;
  1307. }
  1308. return str + ellipsis;
  1309. }
  1310. };
  1311. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1312. ctx.font = style.font;
  1313. value = getEllipsisText(ctx, value, w - 2);
  1314. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1315. };
  1316. return new EllipsisTextCellType();
  1317. },
  1318. /**
  1319. * 获取 动态显示ComboBox的cellType
  1320. * @returns {ActiveComboCellType}
  1321. */
  1322. getActiveComboCellType: function () {
  1323. const ActiveComboCellType = function () { };
  1324. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1325. const proto = ActiveComboCellType.prototype;
  1326. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1327. const sheet = options.sheet;
  1328. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1329. && !sheet.getCell(options.row, options.col).locked()) {
  1330. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1331. } else {
  1332. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1333. }
  1334. };
  1335. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1336. const sheet = options.sheet;
  1337. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1338. && !sheet.getCell(options.row, options.col).locked()) {
  1339. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1340. } else {
  1341. return {
  1342. x: x,
  1343. y: y,
  1344. row: options.row,
  1345. col: options.col,
  1346. cellStyle: cellStyle,
  1347. cellRect: cellRect,
  1348. sheetArea: options.sheetArea
  1349. };
  1350. }
  1351. };
  1352. return new ActiveComboCellType();
  1353. },
  1354. /**
  1355. * 获取 单位的CellType
  1356. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1357. */
  1358. getUnitCellType: function () {
  1359. let combo = this.getActiveComboCellType();
  1360. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'kg', 't', 'm3·km', '总额', '月', '项', '处', '个', '根',
  1361. '棵', '块', '每一试桩', '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '系统', '艘班', 'm/处',
  1362. 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', '亩', 'm3/m2', 'dm3']);
  1363. return combo;
  1364. }
  1365. }
  1366. };