spreadjs_zh.js 64 KB

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