spreadjs_zh.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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. if (sheet.zh_tree) {
  597. for (const iRow in sheet.zh_tree.nodes) {
  598. const node = sheet.zh_tree.nodes[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. }
  606. this.endMassOperation(sheet);
  607. },
  608. /**
  609. * 刷新列是否只读
  610. * @param sheet
  611. * @param field
  612. * @param readonly
  613. */
  614. resetFieldReadOnly: function (sheet, field, readonly) {
  615. const fields = field instanceof Array ? field : [field];
  616. if (sheet.zh_setting) {
  617. sheet.zh_setting.cols.forEach(function (col, i) {
  618. if (fields.indexOf(col.field) !== -1) {
  619. col.readOnly = readonly;
  620. sheet.getRange(-1, i, -1, 1).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  621. }
  622. });
  623. }
  624. },
  625. CellType: {
  626. /**
  627. * 获取树结构CellType
  628. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  629. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  630. * @returns {TreeNodeCellType}
  631. */
  632. getTreeNodeCellType: function () {
  633. const indent = 20;
  634. const levelIndent = -5;
  635. const halfBoxLength = 5;
  636. const halfExpandLength = 3;
  637. /**
  638. * 画一条点线段
  639. * @param canvas - 画布
  640. * @param x1 - 线段起点 x
  641. * @param y1 - 线段起点 y
  642. * @param x2 - 线段终点 x
  643. * @param y2 - 线段终点 y
  644. * @param color - 线段颜色
  645. */
  646. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  647. canvas.save();
  648. // 设置偏移量
  649. canvas.translate(0.5, 0.5);
  650. canvas.beginPath();
  651. canvas.strokeStyle = color;
  652. canvas.dottedLine(x1, y1, x2, y2);
  653. canvas.stroke();
  654. canvas.restore();
  655. };
  656. /**
  657. * 画一条线段
  658. * @param canvas - 画布
  659. * @param x1 - 线段起点 x
  660. * @param y1 - 线段起点 y
  661. * @param x2 - 线段终点 x
  662. * @param y2 - 线段终点 y
  663. * @param color - 线段颜色
  664. */
  665. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  666. canvas.save();
  667. // 设置偏移量
  668. canvas.translate(0.5, 0.5);
  669. canvas.beginPath();
  670. canvas.moveTo(x1, y1);
  671. canvas.lineTo(x2, y2);
  672. canvas.strokeStyle = color;
  673. canvas.stroke();
  674. canvas.restore();
  675. };
  676. /**
  677. * 画一个方框
  678. * @param {Object} canvas - 画布
  679. * @param {Object} rect - 方框区域
  680. * @param {String} lineColor - 画线颜色
  681. * @param {String} fillColor - 填充颜色
  682. */
  683. const drawBox = function (canvas, rect, lineColor, fillColor) {
  684. canvas.save();
  685. // 设置偏移量
  686. canvas.translate(0.5, 0.5);
  687. canvas.strokeStyle = lineColor;
  688. canvas.beginPath();
  689. canvas.moveTo(rect.left, rect.top);
  690. canvas.lineTo(rect.left, rect.bottom);
  691. canvas.lineTo(rect.right, rect.bottom);
  692. canvas.lineTo(rect.right, rect.top);
  693. canvas.lineTo(rect.left, rect.top);
  694. canvas.stroke();
  695. canvas.fillStyle = fillColor;
  696. canvas.fill();
  697. canvas.restore();
  698. };
  699. /**
  700. * 画树结构-展开收起按钮
  701. * @param {Object} canvas - 画布
  702. * @param {Number} x - 单元格左顶点坐标 x
  703. * @param {Number} y - 单元格左顶点坐标 y
  704. * @param {Number} w - 单元格宽度
  705. * @param {Number} h - 单元格高度
  706. * @param {Number} centerX - 按钮中央坐标
  707. * @param {Number} centerY - 按钮中央坐标
  708. * @param {Boolean} expanded - 当前节点展开收起状态
  709. */
  710. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  711. let rect = {
  712. top: centerY - halfBoxLength,
  713. bottom: centerY + halfBoxLength,
  714. left: centerX - halfBoxLength,
  715. right: centerX + halfBoxLength
  716. };
  717. let h1, h2, offset = 1;
  718. if (rect.left < x + w) {
  719. // 方框超出单元格宽度时,超出部分不画。
  720. rect.right = Math.min(rect.right, x + w);
  721. drawBox(canvas, rect, '#808080', 'white');
  722. // 画中心十字
  723. // 画十字横线
  724. h1 = centerX - halfExpandLength;
  725. h2 = Math.min(centerX + halfExpandLength, x + w);
  726. if (h2 > h1) {
  727. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  728. }
  729. // 画十字竖线
  730. if (!expanded && (centerX < x + w)) {
  731. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  732. }
  733. }
  734. };
  735. let TreeNodeCellType = function (){};
  736. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  737. const proto = TreeNodeCellType.prototype;
  738. /**
  739. * 绘制方法
  740. * @param {Object} canvas - 画布
  741. * @param value - cell.value
  742. * @param {Number} x - 单元格左顶点坐标 x
  743. * @param {Number} y - 单元格左顶点坐标 y
  744. * @param {Number} w - 单元格宽度
  745. * @param {Number} h - 单元格高度
  746. * @param {Object} style - cell.style
  747. * @param {Object} options
  748. */
  749. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  750. // 清理 画布--单元格范围 旧数据
  751. if (style.backColor) {
  752. canvas.save();
  753. canvas.fillStyle = style.backColor;
  754. canvas.fillRect(x, y, w, h);
  755. canvas.restore();
  756. } else {
  757. canvas.clearRect(x, y, w, h);
  758. }
  759. const tree = options.sheet.zh_tree;
  760. // 使用TreeCellType前,需定义sheet.tree
  761. if (tree) {
  762. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  763. if (node) {
  764. const showTreeLine = true;
  765. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  766. const centerY = Math.floor((y + (y + h)) / 2);
  767. // Draw Sibling Line
  768. if (showTreeLine) {
  769. // Draw Horizontal Line
  770. if (centerX < x + w) {
  771. const x1 = centerX + indent / 2;
  772. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  773. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  774. }
  775. // Draw Vertical Line
  776. if (centerX < x + w) {
  777. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  778. const parent = tree.getParent(node);
  779. const y2 = y1 - centerY;
  780. if (node.order === 1 && !parent) {
  781. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  782. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  783. } else {
  784. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  785. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  786. }
  787. }
  788. }
  789. // Draw Expand Box
  790. if (!node.is_leaf) {
  791. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  792. }
  793. // Draw Parent Line
  794. if (showTreeLine) {
  795. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  796. while (parent) {
  797. if (!tree.isLastSibling(parent)) {
  798. if (parentCenterX < x + w) {
  799. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  800. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  801. }
  802. }
  803. parent = tree.getParent(parent);
  804. parentCenterX -= (indent + levelIndent);
  805. }
  806. };
  807. // 重定位x
  808. const move = (node.level + 1) * indent + (node.level) * levelIndent;
  809. x = x + move;
  810. w = w - move;
  811. }
  812. }
  813. // Drawing Text
  814. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  815. };
  816. /**
  817. * 获取点击信息
  818. * @param {Number} x
  819. * @param {Number} y
  820. * @param {Object} cellStyle
  821. * @param {Object} cellRect
  822. * @param {Object} context
  823. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  824. */
  825. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  826. return {
  827. x: x,
  828. y: y,
  829. row: context.row,
  830. col: context.col,
  831. cellStyle: cellStyle,
  832. cellRect: cellRect,
  833. sheet: context.sheet,
  834. sheetArea: context.sheetArea
  835. };
  836. };
  837. /**
  838. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  839. * @param {Object} hitinfo - 见getHitInfo
  840. */
  841. proto.processMouseDown = function (hitinfo) {
  842. const offset = -1;
  843. const tree = hitinfo.sheet.zh_tree;
  844. if (!tree) { return; }
  845. const node = tree.nodes[hitinfo.row];
  846. if (!node) { return; }
  847. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  848. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  849. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  850. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  851. const children = tree.getChildren(node);
  852. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  853. tree.loadChildren(node, function () {
  854. node.expanded = true;
  855. const children = tree.getChildren(node);
  856. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  857. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  858. });
  859. } else {
  860. tree.setExpanded(node, !node.expanded);
  861. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  862. const posterity = tree.getPosterity(node);
  863. for (const child of posterity) {
  864. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  865. }
  866. });
  867. hitinfo.sheet.repaint();
  868. }
  869. }
  870. };
  871. return new TreeNodeCellType();
  872. },
  873. /**
  874. * 获取 带悬浮提示的CellType
  875. * @returns {TipCellType}
  876. */
  877. getTipCellType: function () {
  878. const TipCellType = function () {};
  879. // 继承 SpreadJs定义的 普通的TextCellType
  880. TipCellType.prototype = new spreadNS.CellTypes.Text();
  881. const proto = TipCellType.prototype;
  882. /**
  883. * 获取点击信息
  884. * @param {Number} x
  885. * @param {Number} y
  886. * @param {Object} cellStyle
  887. * @param {Object} cellRect
  888. * @param {Object} context
  889. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  890. */
  891. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  892. return {
  893. x: x,
  894. y: y,
  895. row: context.row,
  896. col: context.col,
  897. cellStyle: cellStyle,
  898. cellRect: cellRect,
  899. sheet: context.sheet,
  900. sheetArea: context.sheetArea
  901. };
  902. };
  903. /**
  904. * 鼠标进入单元格事件 - 显示悬浮提示
  905. * @param {Object} hitinfo - 见getHitInfo返回值
  906. */
  907. proto.processMouseEnter = function (hitinfo) {
  908. const text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  909. const setting = hitinfo.sheet.setting;
  910. if (setting.pos && text && text !== '') {
  911. if (!this._toolTipElement) {
  912. let div = $('#autoTip')[0];
  913. if (!div) {
  914. div = document.createElement("div");
  915. $(div).css("position", "absolute")
  916. .css("border", "1px #C0C0C0 solid")
  917. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  918. .css("font", "9pt Arial")
  919. .css("background", "white")
  920. .css("padding", 5)
  921. .attr("id", 'autoTip');
  922. $(div).hide();
  923. document.body.insertBefore(div, null);
  924. }
  925. this._toolTipElement = div;
  926. $(this._toolTipElement).text(text).css("top", setting.pos.y + hitinfo.y + 15).css("left", setting.pos.x + hitinfo.x + 15);
  927. $(this._toolTipElement).show("fast");
  928. }
  929. }
  930. };
  931. /**
  932. * 鼠标移出单元格事件 - 隐藏悬浮提示
  933. * @param {Object} hitinfo - 见getHitInfo返回值
  934. */
  935. proto.processMouseLeave = function (hitinfo) {
  936. if (this._toolTipElement) {
  937. $(this._toolTipElement).hide();
  938. this._toolTipElement = null;
  939. }
  940. };
  941. return new TipCellType();
  942. },
  943. /**
  944. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  945. *
  946. * img:
  947. * 1. 整列固定,则传入img的select
  948. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  949. *
  950. * 2. 各单元格自定义,则
  951. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  952. * function getAttachmentImage (data) {
  953. * $('#attachment-img').url = data.attachmentImageUrl;
  954. * return $('#attachment-img')[0];
  955. * }
  956. *
  957. * @returns {ImageCellType}
  958. */
  959. getImageCellType: function () {
  960. const indent = 10;
  961. const ImageCellType = function (){};
  962. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  963. const proto = ImageCellType.prototype;
  964. proto.getImage = function (sheet, iRow, iCol) {
  965. const col = sheet.zh_setting.cols[iCol];
  966. let imgSource = col.img;
  967. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  968. const sortData = SpreadJsObj.getSortData(sheet);
  969. const data = sortData ? sortData[iRow] : null;
  970. return data ? imgSource(data) : null;
  971. } else {
  972. return $(imgSource)[0] ? $(imgSource)[0] : null;
  973. }
  974. };
  975. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  976. const img = this.getImage(options.sheet, options.row, options.col);
  977. if (img) {
  978. if (style.backColor) {
  979. canvas.save();
  980. canvas.fillStyle = style.backColor;
  981. canvas.fillRect(x, y, indent + img.width, h);
  982. canvas.restore();
  983. }
  984. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  985. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  986. style.hAlign = spreadNS.HorizontalAlign.left;
  987. }
  988. x = x + indent + img.width;
  989. w = w - indent - img.width;
  990. }
  991. // Drawing Text
  992. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  993. };
  994. /**
  995. * 获取点击信息
  996. * @param {Number} x
  997. * @param {Number} y
  998. * @param {Object} cellStyle
  999. * @param {Object} cellRect
  1000. * @param {Object} context
  1001. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1002. */
  1003. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1004. return {
  1005. x: x,
  1006. y: y,
  1007. row: context.row,
  1008. col: context.col,
  1009. cellStyle: cellStyle,
  1010. cellRect: cellRect,
  1011. sheet: context.sheet,
  1012. sheetArea: context.sheetArea
  1013. };
  1014. };
  1015. /**
  1016. * 鼠标点击
  1017. * @param {Object} hitinfo - 见getHitInfo
  1018. */
  1019. proto.processMouseDown = function (hitinfo) {
  1020. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1021. if (img) {
  1022. const halfX = img.width / 2, halfY = img.height / 2;
  1023. const centerX = hitinfo.cellRect.x + indent + halfX;
  1024. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1025. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1026. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1027. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1028. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1029. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1030. const data = sortData ? sortData[hitinfo.row] : null;
  1031. imageClick(data);
  1032. }
  1033. }
  1034. }
  1035. };
  1036. return new ImageCellType();
  1037. },
  1038. /**
  1039. *
  1040. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1041. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1042. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1043. *
  1044. * @returns {ImageCellType}
  1045. */
  1046. getImageButtonCellType: function () {
  1047. let hover = 1, active = 2;
  1048. const ImageCellType = function (){};
  1049. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1050. const proto = ImageCellType.prototype;
  1051. proto.getImage = function (sheet, iRow, iCol) {
  1052. const col = sheet.zh_setting.cols[iCol];
  1053. let imgSource = col.normalImg;
  1054. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1055. if (tag === active) {
  1056. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1057. } else if (tag === hover) {
  1058. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1059. }
  1060. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1061. const sortData = SpreadJsObj.getSortData(sheet);
  1062. const data = sortData ? sortData[iRow] : null;
  1063. return data ? imgSource(data) : null;
  1064. } else {
  1065. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1066. }
  1067. };
  1068. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1069. const img = this.getImage(options.sheet, options.row, options.col);
  1070. const col = options.sheet.zh_setting.cols[options.col];
  1071. const indent = col.indent ? col.indent : 10;
  1072. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1073. if (img) {
  1074. if (style.backColor) {
  1075. canvas.save();
  1076. canvas.fillStyle = style.backColor;
  1077. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1078. canvas.restore();
  1079. }
  1080. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1081. w = w - indent - img.width;
  1082. }
  1083. // Drawing Text
  1084. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1085. } else {
  1086. if (img) {
  1087. if (style.backColor) {
  1088. canvas.save();
  1089. canvas.fillStyle = style.backColor;
  1090. canvas.fillRect(x, y, indent + img.width, h);
  1091. canvas.restore();
  1092. }
  1093. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1094. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1095. style.hAlign = spreadNS.HorizontalAlign.left;
  1096. }
  1097. x = x + indent + img.width;
  1098. w = w - indent - img.width;
  1099. }
  1100. // Drawing Text
  1101. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1102. }
  1103. };
  1104. /**
  1105. * 获取点击信息
  1106. * @param {Number} x
  1107. * @param {Number} y
  1108. * @param {Object} cellStyle
  1109. * @param {Object} cellRect
  1110. * @param {Object} context
  1111. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1112. */
  1113. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1114. return {
  1115. x: x,
  1116. y: y,
  1117. row: context.row,
  1118. col: context.col,
  1119. cellStyle: cellStyle,
  1120. cellRect: cellRect,
  1121. sheet: context.sheet,
  1122. sheetArea: context.sheetArea
  1123. };
  1124. };
  1125. /**
  1126. * 鼠标点击
  1127. * @param {Object} hitinfo - 见getHitInfo
  1128. */
  1129. proto.processMouseEnter = function (hitinfo) {
  1130. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1131. // Drawing Image
  1132. if (col.hoverImg) {
  1133. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1134. cell.tag(hover);
  1135. hitinfo.sheet.repaint(hitinfo.cellRect);
  1136. }
  1137. };
  1138. proto.processMouseLeave = function (hitinfo) {
  1139. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1140. // Drawing Image
  1141. if (col.hoverImg) {
  1142. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1143. cell.tag(null);
  1144. hitinfo.sheet.repaint(hitinfo.cellRect);
  1145. }
  1146. };
  1147. proto.processMouseDown = function (hitinfo) {
  1148. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1149. if (col.activeImg) {
  1150. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1151. cell.tag(active);
  1152. hitinfo.sheet.repaint(hitinfo.cellRect);
  1153. }
  1154. };
  1155. proto.processMouseUp = function (hitinfo) {
  1156. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1157. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1158. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1159. const data = sortData ? sortData[hitinfo.row] : null;
  1160. imageClick(data);
  1161. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1162. cell.tag(null);
  1163. hitinfo.sheet.repaint(hitinfo.cellRect);
  1164. }
  1165. };
  1166. /*
  1167. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1168. */
  1169. // proto.processMouseDown = function (hitinfo) {
  1170. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1171. // const halfX = img.width / 2, halfY = img.height / 2;
  1172. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1173. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1174. //
  1175. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1176. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1177. // cell.tag(down);
  1178. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1179. // }
  1180. // };
  1181. // proto.processMouseUp = function (hitinfo) {
  1182. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1183. // const halfX = img.width / 2, halfY = img.height / 2;
  1184. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1185. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1186. //
  1187. // // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1188. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1189. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1190. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1191. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1192. // const data = sortData ? sortData[hitinfo.row] : null;
  1193. // imageClick(data);
  1194. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1195. // cell.tag(null);
  1196. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1197. // }
  1198. // }
  1199. // };
  1200. // proto.processMouseMove = function (hitinfo) {
  1201. // const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1202. // const halfX = img.width / 2, halfY = img.height / 2;
  1203. // const centerX = hitinfo.cellRect.x + indent + halfX;
  1204. // const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1205. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1206. // if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1207. // if (cell.tag() !== hover) {
  1208. // cell.tag(hover);
  1209. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1210. // }
  1211. // } else {
  1212. // if (cell.tag() === hover) {
  1213. // cell.tag(null);
  1214. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1215. // }
  1216. // }
  1217. // };
  1218. return new ImageCellType();
  1219. },
  1220. /**
  1221. * 获取 嵌入Html的cellType
  1222. * @returns {HTMLCellType}
  1223. */
  1224. getHtmlCellType: function () {
  1225. const HTMLCellType = function (){};
  1226. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1227. const proto = ImageCellType.prototype;
  1228. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1229. let DOMURL = window.URL || window.webkitURL || window;
  1230. let cell = context.sheet.getCell(context.row, context.col);
  1231. let img = cell.tag();
  1232. if (img) {
  1233. try {
  1234. ctx.save();
  1235. ctx.rect(x, y, w, h);
  1236. ctx.clip();
  1237. ctx.drawImage(img, x + 2, y + 2)
  1238. ctx.restore();
  1239. cell.tag(null);
  1240. return;
  1241. }
  1242. catch (err) {
  1243. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1244. cell.tag(null);
  1245. return;
  1246. }
  1247. }
  1248. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1249. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1250. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1251. let doc = document.implementation.createHTMLDocument("");
  1252. doc.write(data);
  1253. // Get well-formed markup
  1254. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1255. img = new Image();
  1256. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1257. //var url = DOMURL.createObjectURL(svg);
  1258. //img.src = url;
  1259. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1260. cell.tag(img);
  1261. img.onload = function () {
  1262. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1263. }
  1264. };
  1265. return new HTMLCellType();
  1266. },
  1267. /**
  1268. * 获取 字符超长缩略的cellType
  1269. * @returns {EllipsisTextCellType}
  1270. */
  1271. getEllipsisTextCellType: function () {
  1272. const EllipsisTextCellType = function (){};
  1273. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1274. const proto = EllipsisTextCellType.prototype;
  1275. const getEllipsisText = function(c, str, maxWidth) {
  1276. var width = c.measureText(str).width;
  1277. var ellipsis = '…';
  1278. var ellipsisWidth = c.measureText(ellipsis).width;
  1279. if (width <= maxWidth || width <= ellipsisWidth) {
  1280. return str;
  1281. } else {
  1282. var len = str.length;
  1283. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1284. str = str.substring(0, len);
  1285. width = c.measureText(str).width;
  1286. }
  1287. return str + ellipsis;
  1288. }
  1289. };
  1290. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1291. ctx.font = style.font;
  1292. value = getEllipsisText(ctx, value, w - 2);
  1293. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1294. };
  1295. return new EllipsisTextCellType();
  1296. },
  1297. /**
  1298. * 获取 动态显示ComboBox的cellType
  1299. * @returns {ActiveComboCellType}
  1300. */
  1301. getActiveComboCellType: function () {
  1302. const ActiveComboCellType = function () { };
  1303. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1304. const proto = ActiveComboCellType.prototype;
  1305. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1306. const sheet = options.sheet;
  1307. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1308. && !sheet.getCell(options.row, options.col).locked()) {
  1309. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1310. } else {
  1311. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1312. }
  1313. };
  1314. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1315. const sheet = options.sheet;
  1316. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1317. && !sheet.getCell(options.row, options.col).locked()) {
  1318. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1319. } else {
  1320. return {
  1321. x: x,
  1322. y: y,
  1323. row: options.row,
  1324. col: options.col,
  1325. cellStyle: cellStyle,
  1326. cellRect: cellRect,
  1327. sheetArea: options.sheetArea
  1328. };
  1329. }
  1330. };
  1331. return new ActiveComboCellType();
  1332. },
  1333. /**
  1334. * 获取 单位的CellType
  1335. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1336. */
  1337. getUnitCellType: function () {
  1338. let combo = this.getActiveComboCellType();
  1339. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'kg', 't', 'à·km', '总额', '月', '项', '处', '个', '根',
  1340. '棵', '块', '每一试桩', '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', 'm/处',
  1341. 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', '亩', 'm3/m2', 'dm3']);
  1342. return combo;
  1343. }
  1344. }
  1345. };