spreadjs_zh.js 62 KB

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