spreadjs_zh.js 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  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.allowUserDragFill = false;
  77. spread.options.showDragFillSmartTag = false;
  78. spread.options.allowUserEditFormula = false;
  79. spread.options.allowExtendPasteRange = true;
  80. spread.options.copyPasteHeaderOptions = spreadNS.CopyPasteHeaderOptions.noHeaders;
  81. spread.getActiveSheet().options.clipBoardOptions = spreadNS.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  82. spread.getActiveSheet().setRowCount(3);
  83. return spread;
  84. },
  85. addCutEvents: function (spread, fun) {
  86. const cut = spreadNS.Commands.cut.execute;
  87. spreadNS.Commands.cut.execute = function (context, options, isUndo) {
  88. const self = this, sheet = context.getActiveSheet();
  89. const sels = sheet.getSelections();
  90. const sel = sels ? sels[0] : null;
  91. if (sel) {
  92. return fun(sheet, sel, function () {
  93. cut(context, options, isUndo);
  94. });
  95. }
  96. }
  97. },
  98. /**
  99. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  100. * @param {GC.Spread.Sheets.Worksheet} sheet
  101. */
  102. protectedSheet: function (sheet) {
  103. const option = {
  104. allowSelectLockedCells: true,
  105. allowSelectUnlockedCells: true,
  106. allowResizeRows: true,
  107. allowResizeColumns: true
  108. };
  109. const defaultStyle = sheet.getDefaultStyle();
  110. defaultStyle.locked = false;
  111. sheet.setDefaultStyle(defaultStyle);
  112. sheet.options.protectionOptions = option;
  113. sheet.options.isProtected = true;
  114. sheet.options.allowCellOverflow = false;
  115. },
  116. /**
  117. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  118. * @param {GC.Spread.Sheets.Worksheet} sheet
  119. * @param {function} operation
  120. */
  121. beginMassOperation: function (sheet) {
  122. sheet.suspendPaint();
  123. sheet.suspendEvent();
  124. },
  125. endMassOperation: function (sheet) {
  126. sheet.resumeEvent();
  127. sheet.resumePaint();
  128. },
  129. massOperationSheet: function (sheet, operation) {
  130. this.beginMassOperation(sheet);
  131. operation();
  132. this.endMassOperation(sheet);
  133. },
  134. /**
  135. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  136. * @param obj
  137. * @returns {{x: number, y: number}}
  138. */
  139. getObjPos: function (obj) {
  140. if (!obj) return null;
  141. let target = obj;
  142. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  143. target = obj.offsetParent;
  144. while (target) {
  145. pos.x += target.offsetLeft;
  146. pos.y += target.offsetTop;
  147. target = target.offsetParent;
  148. }
  149. return pos;
  150. },
  151. /**
  152. * 以下四个方法来自Spread示例, 参见官网或文档
  153. */
  154. getHitTest: function (obj, e, sheet) {
  155. var offset = obj.offset(),
  156. x = e.pageX - offset.left,
  157. y = e.pageY - offset.top;
  158. return sheet.hitTest(x, y);
  159. },
  160. getTargetSelection: function (sheet, target) {
  161. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  162. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  163. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  164. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  165. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  166. return sheet.getRange(target.row, target.col, 1, 1);
  167. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  168. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  169. };
  170. },
  171. getCellInSelections: function (selections, row, col) {
  172. const count = selections.length;
  173. let range;
  174. for (var i = 0; i < count; i++) {
  175. range = selections[i];
  176. if (range.contains(row, col)) {
  177. return range;
  178. }
  179. }
  180. return null;
  181. },
  182. checkTargetInSelection: function (selections, range) {
  183. var count = selections.length, sel;
  184. for (var i = 0; i < count; i++) {
  185. sel = selections[i];
  186. if (sel.containsRange(range)) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. },
  192. /**
  193. * 获取 spread 在鼠标右键时, spread的选中区域
  194. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  195. * colHeader, 选中整列
  196. * rowHeader, 选中整行
  197. * corner, 全选
  198. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  199. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  200. * @param e: jquery-contextmenu.build方法的第二个变量
  201. * @param {GC.Spread.Sheets.Workbook} spread
  202. * @returns {*}
  203. */
  204. safeRightClickSelection: function (obj, e, spread) {
  205. const sheet = spread.getActiveSheet();
  206. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  207. if (!this.checkTargetInSelection(selections, range)) {
  208. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  209. }
  210. return target;
  211. },
  212. /**
  213. * 禁用右键菜单
  214. * @param selector
  215. * @param spread
  216. */
  217. forbiddenSpreadContextMenu: function (selector, spread) {
  218. $.contextMenu({
  219. selector: selector,
  220. build: function ($trigger, e) {
  221. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  222. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  223. }
  224. });
  225. },
  226. /**
  227. * 获取写入sheet的数据序列
  228. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  229. * @param sheet
  230. * @returns {*}
  231. */
  232. getSortData: function (sheet) {
  233. if (sheet.zh_dataType) {
  234. if (sheet.zh_dataType === this.DataType.Data) {
  235. return sheet.zh_data;
  236. } else if (sheet.zh_dataType === this.DataType.Tree) {
  237. return sheet.zh_tree.nodes;
  238. } else {
  239. return null;
  240. }
  241. } else {
  242. return null;
  243. }
  244. },
  245. /**
  246. * sheet中 使用delete键,触发EndEdited事件
  247. * @param {GC.Spreads.Sheets.Workbook} spread
  248. * @param {function} fun
  249. */
  250. addDeleteBind: function (spread, fun) {
  251. spread.commandManager().register('deleteEvent', function () {
  252. fun(spread.getActiveSheet());
  253. });
  254. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  255. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  256. },
  257. _initSheetDeafult: function (sheet) {
  258. if (sheet.zh_setting.headerFont) {
  259. const vStyle = new spreadNS.Style();
  260. vStyle.font = sheet.zh_setting.headerFont;
  261. vStyle.locked = false;
  262. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  263. }
  264. if (sheet.zh_setting.font) {
  265. const vStyle = new spreadNS.Style();
  266. vStyle.font = sheet.zh_setting.font;
  267. vStyle.locked = false;
  268. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  269. }
  270. },
  271. /**
  272. * 根据sheet.zh_setting初始化sheet表头
  273. * @param {GC.Spread.Sheets.Worksheet} sheet
  274. */
  275. _initSheetHeader: function (sheet) {
  276. if (!sheet.zh_setting) { return; }
  277. sheet.setColumnCount(sheet.zh_setting.cols.length);
  278. sheet.setRowCount(sheet.zh_setting.headRows, spreadNS.SheetArea.colHeader);
  279. for (let iRow = 0; iRow < sheet.zh_setting.headRowHeight.length; iRow ++) {
  280. sheet.setRowHeight(iRow, sheet.zh_setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  281. }
  282. for (let iCol = 0; iCol < sheet.zh_setting.cols.length; iCol++) {
  283. const col = sheet.zh_setting.cols[iCol];
  284. const title = col.title.split('|');
  285. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  286. for (let i = 0; i < title.length; i++) {
  287. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  288. cell.text(title[i]).wordWrap(true);
  289. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  290. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  291. }
  292. }
  293. sheet.setColumnWidth(iCol, col.width);
  294. if (col.visible !== undefined && col.visible !== null) {
  295. sheet.setColumnVisible(iCol, col.visible);
  296. }
  297. }
  298. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  299. sheet.showRowOutline(false);
  300. if (sheet.zh_setting.defaultRowHeight) {
  301. sheet.defaults.rowHeight = sheet.zh_setting.defaultRowHeight;
  302. }
  303. },
  304. /**
  305. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  306. * @param {GC.Spread.Sheets.Worksheet} sheet
  307. * @param setting
  308. */
  309. initSheet: function (sheet, setting) {
  310. this.beginMassOperation(sheet);
  311. setting.pos = sheet.getParent().pos;
  312. sheet.zh_setting = setting;
  313. this._initSheetDeafult(sheet);
  314. this._initSheetHeader(sheet);
  315. sheet.setRowCount(sheet.zh_setting.emptyRows);
  316. sheet.extendCellType = {};
  317. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly);
  318. this.endMassOperation(sheet);
  319. },
  320. _loadRowData: function (sheet, data, row) {
  321. // 单元格重新写入数据
  322. if (!data) { return }
  323. sheet.zh_setting.cols.forEach(function (col, j) {
  324. const cell = sheet.getCell(row, j);
  325. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  326. cell.value(col.getValue(data));
  327. } else if (col.field !== '' && data[col.field]) {
  328. cell.value(data[col.field]);
  329. }
  330. if (col.font) {
  331. cell.font(col.font);
  332. }
  333. if (col.foreColor) {
  334. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  335. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  336. } else {
  337. cell.foreColor(col.foreColor);
  338. }
  339. }
  340. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  341. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  342. } else {
  343. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  344. }
  345. if (col.formatter) {
  346. cell.formatter(col.formatter);
  347. } else if (col.type === 'Number') {
  348. cell.formatter('0.######');
  349. }
  350. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  351. cell.backColor(sheet.zh_setting.getColor(sheet, data, col, sheet.getDefaultStyle().backColor));
  352. }
  353. });
  354. },
  355. _defineColCellType: function (sheet, col, colSetting) {
  356. sheet.AcitveComboRefresh = false;
  357. if(colSetting.cellType === 'ellipsis') {
  358. if (!sheet.extendCellType.ellipsis) {
  359. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  360. }
  361. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  362. }
  363. if(colSetting.cellType === 'ellipsisAutoTip') {
  364. if (!sheet.extendCellType.ellipsisAutoTip) {
  365. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  366. }
  367. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  368. }
  369. if(colSetting.cellType === 'html') {
  370. if (!sheet.extendCellType.html) {
  371. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  372. }
  373. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  374. }
  375. if (colSetting.cellType === 'image') {
  376. if (!sheet.extendCellType.image) {
  377. sheet.extendCellType.image = this.CellType.getImageCellType();
  378. }
  379. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  380. }
  381. if (colSetting.cellType === 'imageBtn') {
  382. if (!sheet.extendCellType.imageBtn) {
  383. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  384. }
  385. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  386. }
  387. if (colSetting.cellType === 'activeImageBtn') {
  388. if (!sheet.extendCellType.activeImageBtn) {
  389. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  390. }
  391. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  392. }
  393. if (colSetting.cellType === 'tree') {
  394. if (!sheet.extendCellType.tree) {
  395. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  396. }
  397. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  398. }
  399. if (colSetting.cellType === 'tip') {
  400. if (!sheet.extendCellType.tip) {
  401. sheet.extendCellType.tip = this.CellType.getTipCellType();
  402. }
  403. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  404. }
  405. if (colSetting.cellType === 'autoTip') {
  406. if (!sheet.extendCellType.autoTip) {
  407. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  408. }
  409. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  410. }
  411. if (colSetting.cellType === 'checkbox') {
  412. if (!sheet.extendCellType.checkbox) {
  413. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  414. }
  415. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  416. }
  417. if (colSetting.cellType === 'unit') {
  418. if (!sheet.extendCellType.unit) {
  419. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  420. if (!sheet.AcitveComboRefresh) {
  421. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  422. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  423. if (cellType === sheet.extendCellType.unit) {
  424. info.sheet.leaveCell = {row: info.row, col: info.col};
  425. } else {
  426. delete info.sheet.leaveCell;
  427. }
  428. });
  429. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  430. if (info.sheet.leaveCell) {
  431. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  432. }
  433. });
  434. sheet.AcitveComboRefresh = true;
  435. }
  436. }
  437. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  438. }
  439. if (colSetting.cellType === 'customizeCombo') {
  440. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  441. if (!sheet.extendCellType[cellKey]) {
  442. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  443. if (!sheet.AcitveComboRefresh) {
  444. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  445. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  446. if (cellType === sheet.extendCellType[cellKey]) {
  447. info.sheet.leaveCell = {row: info.row, col: info.col};
  448. } else {
  449. delete info.sheet.leaveCell;
  450. }
  451. });
  452. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  453. if (info.sheet.leaveCell) {
  454. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  455. }
  456. });
  457. sheet.AcitveComboRefresh = true;
  458. }
  459. }
  460. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  461. }
  462. if (colSetting.formatter) {
  463. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  464. }
  465. },
  466. /**
  467. * 整个sheet重新加载数据
  468. * @param {GC.Spread.Sheets.Worksheet} sheet
  469. */
  470. reLoadSheetData: function (sheet) {
  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. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  476. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  477. // 设置总行数
  478. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  479. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  480. // 控制空白行
  481. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  482. emptyRows.locked(sheet.zh_dataType === 'tree');
  483. if (sortData) {
  484. // 单元格写入数据
  485. sortData.forEach(function (data, i) {
  486. self._loadRowData(sheet, data, i);
  487. sheet.setRowVisible(i, data.visible);
  488. });
  489. }
  490. // 设置列单元格格式
  491. sheet.zh_setting.cols.forEach(function (col, j) {
  492. //if (!col.cellType) { return; }
  493. self._defineColCellType(sheet, j, col);
  494. });
  495. this.endMassOperation(sheet);
  496. } catch (err) {
  497. this.endMassOperation(sheet);
  498. }
  499. },
  500. /**
  501. * 重新加载部分数据行
  502. * @param {GC.Spread.Sheets.Worksheet} sheet
  503. * @param {Number} row
  504. * @param {Number} count
  505. */
  506. reLoadRowData: function (sheet, row, count = 1) {
  507. if (row < 0) { return; }
  508. const self = this;
  509. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  510. this.beginMassOperation(sheet);
  511. try {
  512. // 清空原单元格数据
  513. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  514. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  515. // 单元格重新写入数据
  516. for (let i = row; i < row + count; i++) {
  517. const data = sortData[i];
  518. if (!data) { continue; }
  519. this._loadRowData(sheet, data, i);
  520. sheet.setRowVisible(i, data.visible);
  521. }
  522. this.endMassOperation(sheet);
  523. } catch (err) {
  524. this.endMassOperation(sheet);
  525. }
  526. },
  527. /**
  528. * 重新加载部分行数据
  529. * @param {GC.Spread.Sheets.Worksheet} sheet
  530. * @param {Array} rows
  531. */
  532. reLoadRowsData: function (sheet, rows) {
  533. const self = this;
  534. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  535. this.beginMassOperation(sheet);
  536. try {
  537. for (const row of rows) {
  538. if (row < 0) { continue; }
  539. // 清空原单元格数据
  540. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  541. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  542. const data = sortData[row];
  543. // 单元格重新写入数据
  544. this._loadRowData(sheet, data, row);
  545. sheet.setRowVisible(row, data.visible);
  546. };
  547. this.endMassOperation(sheet);
  548. } catch (err) {
  549. this.endMassOperation(sheet);
  550. }
  551. },
  552. /**
  553. * 重新加载部分列数据
  554. * @param {GC.Spread.Sheets.Worksheet} sheet
  555. * @param {Array} cols
  556. */
  557. reLoadColsData: function (sheet, cols) {
  558. const self = this;
  559. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  560. this.beginMassOperation(sheet);
  561. try {
  562. for (const iCol of cols) {
  563. // 清空原单元格数据
  564. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  565. const col = sheet.zh_setting.cols[iCol];
  566. sortData.forEach(function (data, i) {
  567. // 设置值
  568. const cell = sheet.getCell(i, iCol);
  569. if (col.field !== '' && data[col.field]) {
  570. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  571. } else {
  572. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  573. }
  574. // 设置单元格格式
  575. if (col.formatter) {
  576. cell.formatter(col.formatter);
  577. }
  578. });
  579. }
  580. this.endMassOperation(sheet);
  581. } catch (err) {
  582. this.endMassOperation(sheet);
  583. }
  584. },
  585. reLoadNodesData: function (sheet, nodes) {
  586. this.beginMassOperation(sheet);
  587. nodes = nodes instanceof Array ? nodes : [nodes];
  588. for (const node of nodes) {
  589. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  590. this._loadRowData(sheet, node, sortData.indexOf(node));
  591. }
  592. this.endMassOperation(sheet);
  593. },
  594. /**
  595. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  596. * @param {GC.Spread.Sheets.Worksheet} sheet
  597. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  598. * @param {Array|PathTree} data - 对dataType对应
  599. */
  600. loadSheetData: function (sheet, dataType, data){
  601. sheet.zh_dataType = dataType;
  602. if (dataType === 'tree') {
  603. sheet.zh_tree = data;
  604. } else {
  605. sheet.zh_data = data;
  606. }
  607. this.protectedSheet(sheet);
  608. this.reLoadSheetData(sheet);
  609. },
  610. /**
  611. * 获取复制数据HTML格式(过滤不可见单元格)
  612. * @param {GC.Spread.Sheets.Worksheet} sheet
  613. * @returns {string}
  614. */
  615. getFilterCopyHTML: function (sheet) {
  616. const sel = sheet.getSelections()[0];
  617. const html = [];
  618. html.push('<table>');
  619. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  620. // 跳过隐藏行
  621. if (!sheet.getCell(i, -1).visible()) { continue; }
  622. const rowHtml = [];
  623. rowHtml.push('<tr>');
  624. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  625. const data = sheet.getText(i, j);
  626. rowHtml.push('<td>' + data + '</td>');
  627. }
  628. rowHtml.push('</tr>');
  629. html.push(rowHtml.join(''));
  630. }
  631. html.push('</table>');
  632. return html.join('');
  633. },
  634. /**
  635. * 获取复制数据Text格式(过滤不可见单元格)
  636. * @param {GC.Spread.Sheets.Worksheet} sheet
  637. * @returns {string}
  638. */
  639. getFilterCopyText: function (sheet) {
  640. const copyData = [];
  641. const sel = sheet.getSelections()[0];
  642. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  643. // 跳过隐藏行
  644. if (!sheet.getCell(i, -1).visible()) { continue; }
  645. const rowText = [];
  646. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  647. const data = sheet.getText(i, j);
  648. rowText.push(data);
  649. }
  650. copyData.push(rowText.join('\t'));
  651. }
  652. return copyData.join('\n');
  653. },
  654. analysisPasteText: function (text) {
  655. const result = [];
  656. if (text === '') return result;
  657. const rows = text.split('\n');
  658. for (const r of rows) {
  659. const cols = r.split('\t');
  660. result.push(cols);
  661. }
  662. return result;
  663. },
  664. analysisPasteHtml: function (html) {
  665. const result = [];
  666. if (!html || html === '') return result;
  667. },
  668. /**
  669. * 树表结构,定位至指定的节点
  670. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  671. * @param {Number} id - 定位节点的id
  672. */
  673. locateTreeNode: function (sheet, id, autoExpand = false) {
  674. const tree = sheet.zh_tree;
  675. if (!tree) { return }
  676. const node = tree.getItems(id);
  677. if (!node) { return }
  678. if (autoExpand && !node.visible) {
  679. const parents = tree.autoExpandNode(node);
  680. if (parents && parents.length > 0) {
  681. SpreadJsObj.reLoadNodesData(sheet, parents);
  682. SpreadJsObj.refreshTreeRowVisible(sheet);
  683. }
  684. }
  685. const index = tree.nodes.indexOf(node);
  686. const sels = sheet.getSelections();
  687. sheet.setSelection(index, sels[0].col, 1, 1);
  688. sheet.showRow(index, spreadNS.VerticalPosition.center);
  689. },
  690. saveTopAndSelect: function (sheet, cacheKey) {
  691. const sel = sheet.getSelections()[0];
  692. const top = sheet.getViewportTopRow(1);
  693. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  694. },
  695. loadTopAndSelect: function (sheet, cacheKey) {
  696. let ts = getLocalCache(cacheKey);
  697. if (ts !== '') {
  698. ts = JSON.parse(ts);
  699. if (ts === undefined || ts === null) return;
  700. if (ts.sel) {
  701. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  702. }
  703. if (ts.top) {
  704. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  705. }
  706. }
  707. },
  708. resetTopAndSelect: function (sheet) {
  709. sheet.setSelection(0, 0, 1, 1);
  710. sheet.showRow(0, spreadNS.VerticalPosition.top);
  711. },
  712. /**
  713. * 获取当前选行的数据对象
  714. * @param {GC.Spread.Sheets.Worksheet} sheet
  715. * @returns {Object}
  716. */
  717. getSelectObject: function (sheet) {
  718. if (!sheet) {
  719. return null;
  720. } else if (sheet.zh_dataType) {
  721. const sel = sheet.getSelections()[0];
  722. if (!sel) {
  723. return null;
  724. }
  725. if (sheet.zh_dataType === this.DataType.Tree) {
  726. return sheet.zh_tree.nodes[sel.row];
  727. } else if (sheet.zh_dataType === this.DataType.Data) {
  728. return sheet.zh_data[sel.row];
  729. } else {
  730. return null;
  731. }
  732. }
  733. },
  734. /**
  735. * 刷新列显示
  736. * @param sheet
  737. */
  738. refreshColumnVisible: function (sheet) {
  739. if(sheet.zh_setting) {
  740. sheet.zh_setting.cols.forEach(function (col, index) {
  741. if (col.visible !== undefined && col.visible !== null) {
  742. sheet.setColumnVisible(index, col.visible);
  743. }
  744. });
  745. }
  746. },
  747. /**
  748. * 刷新行显示
  749. * @param sheet
  750. */
  751. refreshTreeRowVisible: function (sheet) {
  752. this.beginMassOperation(sheet);
  753. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  754. for (const iRow in sortData) {
  755. const node = sortData[iRow];
  756. if (node.visible !== undefined && node.visible !== null) {
  757. sheet.setRowVisible(iRow, node.visible);
  758. } else {
  759. sheet.setRowVisible(iRow, true);
  760. }
  761. }
  762. // if (sheet.zh_tree) {
  763. // for (const iRow in sheet.zh_tree.nodes) {
  764. // const node = sheet.zh_tree.nodes[iRow];
  765. // if (node.visible !== undefined && node.visible !== null) {
  766. // sheet.setRowVisible(iRow, node.visible);
  767. // } else {
  768. // sheet.setRowVisible(iRow, true);
  769. // }
  770. // }
  771. // }
  772. this.endMassOperation(sheet);
  773. },
  774. refreshColumnAlign: function (sheet) {
  775. if (sheet.zh_setting) {
  776. for (const iCol in sheet.zh_setting.cols) {
  777. const col = sheet.zh_setting.cols[iCol];
  778. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  779. }
  780. }
  781. },
  782. /**
  783. * 刷新列是否只读
  784. * @param sheet
  785. * @param field
  786. * @param readonly
  787. */
  788. resetFieldReadOnly: function (sheet, field, readonly) {
  789. const fields = field instanceof Array ? field : [field];
  790. if (sheet.zh_setting) {
  791. sheet.zh_setting.cols.forEach(function (col, i) {
  792. if (fields.indexOf(col.field) !== -1) {
  793. col.readOnly = readonly;
  794. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  795. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  796. }
  797. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  798. }
  799. });
  800. }
  801. },
  802. CellType: {
  803. /**
  804. * 获取树结构CellType
  805. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  806. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  807. * @returns {TreeNodeCellType}
  808. */
  809. getTreeNodeCellType: function () {
  810. const indent = 20;
  811. const levelIndent = -5;
  812. const halfBoxLength = 5;
  813. const halfExpandLength = 3;
  814. /**
  815. * 画一条点线段
  816. * @param canvas - 画布
  817. * @param x1 - 线段起点 x
  818. * @param y1 - 线段起点 y
  819. * @param x2 - 线段终点 x
  820. * @param y2 - 线段终点 y
  821. * @param color - 线段颜色
  822. */
  823. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  824. canvas.save();
  825. // 设置偏移量
  826. canvas.translate(0.5, 0.5);
  827. canvas.beginPath();
  828. canvas.strokeStyle = color;
  829. canvas.dottedLine(x1, y1, x2, y2);
  830. canvas.stroke();
  831. canvas.restore();
  832. };
  833. /**
  834. * 画一条线段
  835. * @param canvas - 画布
  836. * @param x1 - 线段起点 x
  837. * @param y1 - 线段起点 y
  838. * @param x2 - 线段终点 x
  839. * @param y2 - 线段终点 y
  840. * @param color - 线段颜色
  841. */
  842. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  843. canvas.save();
  844. // 设置偏移量
  845. canvas.translate(0.5, 0.5);
  846. canvas.beginPath();
  847. canvas.moveTo(x1, y1);
  848. canvas.lineTo(x2, y2);
  849. canvas.strokeStyle = color;
  850. canvas.stroke();
  851. canvas.restore();
  852. };
  853. /**
  854. * 画一个方框
  855. * @param {Object} canvas - 画布
  856. * @param {Object} rect - 方框区域
  857. * @param {String} lineColor - 画线颜色
  858. * @param {String} fillColor - 填充颜色
  859. */
  860. const drawBox = function (canvas, rect, lineColor, fillColor) {
  861. canvas.save();
  862. // 设置偏移量
  863. canvas.translate(0.5, 0.5);
  864. canvas.strokeStyle = lineColor;
  865. canvas.beginPath();
  866. canvas.moveTo(rect.left, rect.top);
  867. canvas.lineTo(rect.left, rect.bottom);
  868. canvas.lineTo(rect.right, rect.bottom);
  869. canvas.lineTo(rect.right, rect.top);
  870. canvas.lineTo(rect.left, rect.top);
  871. canvas.stroke();
  872. canvas.fillStyle = fillColor;
  873. canvas.fill();
  874. canvas.restore();
  875. };
  876. /**
  877. * 画树结构-展开收起按钮
  878. * @param {Object} canvas - 画布
  879. * @param {Number} x - 单元格左顶点坐标 x
  880. * @param {Number} y - 单元格左顶点坐标 y
  881. * @param {Number} w - 单元格宽度
  882. * @param {Number} h - 单元格高度
  883. * @param {Number} centerX - 按钮中央坐标
  884. * @param {Number} centerY - 按钮中央坐标
  885. * @param {Boolean} expanded - 当前节点展开收起状态
  886. */
  887. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded) {
  888. let rect = {
  889. top: centerY - halfBoxLength,
  890. bottom: centerY + halfBoxLength,
  891. left: centerX - halfBoxLength,
  892. right: centerX + halfBoxLength
  893. };
  894. let h1, h2, offset = 1;
  895. if (rect.left < x + w) {
  896. // 方框超出单元格宽度时,超出部分不画。
  897. rect.right = Math.min(rect.right, x + w);
  898. drawBox(canvas, rect, '#808080', 'white');
  899. // 画中心十字
  900. // 画十字横线
  901. h1 = centerX - halfExpandLength;
  902. h2 = Math.min(centerX + halfExpandLength, x + w);
  903. if (h2 > h1) {
  904. drawLine(canvas, h1, centerY, h2, centerY, '#808080');
  905. }
  906. // 画十字竖线
  907. if (!expanded && (centerX < x + w)) {
  908. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, '#808080');
  909. }
  910. }
  911. };
  912. let TreeNodeCellType = function (){};
  913. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  914. const proto = TreeNodeCellType.prototype;
  915. /**
  916. * 绘制方法
  917. * @param {Object} canvas - 画布
  918. * @param value - cell.value
  919. * @param {Number} x - 单元格左顶点坐标 x
  920. * @param {Number} y - 单元格左顶点坐标 y
  921. * @param {Number} w - 单元格宽度
  922. * @param {Number} h - 单元格高度
  923. * @param {Object} style - cell.style
  924. * @param {Object} options
  925. */
  926. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  927. // 清理 画布--单元格范围 旧数据
  928. if (style.backColor) {
  929. canvas.save();
  930. canvas.fillStyle = style.backColor;
  931. canvas.fillRect(x, y, w, h);
  932. canvas.restore();
  933. } else {
  934. canvas.clearRect(x, y, w, h);
  935. }
  936. const tree = options.sheet.zh_tree;
  937. // 使用TreeCellType前,需定义sheet.tree
  938. if (tree) {
  939. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  940. if (node) {
  941. const showTreeLine = true;
  942. const centerX = Math.floor(x) + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  943. const centerY = Math.floor((y + (y + h)) / 2);
  944. // Draw Sibling Line
  945. if (showTreeLine) {
  946. // Draw Horizontal Line
  947. if (centerX < x + w) {
  948. const x1 = centerX + indent / 2;
  949. //drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, 'gray');
  950. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, '#b8b8b8');
  951. }
  952. // Draw Vertical Line
  953. if (centerX < x + w) {
  954. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  955. const parent = tree.getParent(node);
  956. const y2 = y1 - centerY;
  957. if (node.order === 1 && !parent) {
  958. //drawLine(canvas, centerX, centerY, centerX, y1, 'gray');
  959. drawDotLine(canvas, centerX, centerY, centerX, y1, '#b8b8b8');
  960. } else {
  961. //drawLine(canvas, centerX, y, centerX, y1, 'gray');
  962. drawDotLine(canvas, centerX, y, centerX, y1, '#b8b8b8');
  963. }
  964. }
  965. }
  966. // Draw Expand Box
  967. if (!node.is_leaf) {
  968. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded);
  969. }
  970. // Draw Parent Line
  971. if (showTreeLine) {
  972. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  973. while (parent) {
  974. if (!tree.isLastSibling(parent)) {
  975. if (parentCenterX < x + w) {
  976. //drawLine(canvas, parentCenterX, y, parentCenterX, y + h, 'gray');
  977. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, '#b8b8b8');
  978. }
  979. }
  980. parent = tree.getParent(parent);
  981. parentCenterX -= (indent + levelIndent);
  982. }
  983. };
  984. // 重定位x
  985. const move = (node.level + 1) * indent + (node.level) * levelIndent;
  986. x = x + move;
  987. w = w - move;
  988. }
  989. }
  990. // Drawing Text
  991. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  992. };
  993. /**
  994. * 获取点击信息
  995. * @param {Number} x
  996. * @param {Number} y
  997. * @param {Object} cellStyle
  998. * @param {Object} cellRect
  999. * @param {Object} context
  1000. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1001. */
  1002. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1003. return {
  1004. x: x,
  1005. y: y,
  1006. row: context.row,
  1007. col: context.col,
  1008. cellStyle: cellStyle,
  1009. cellRect: cellRect,
  1010. sheet: context.sheet,
  1011. sheetArea: context.sheetArea
  1012. };
  1013. };
  1014. /**
  1015. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1016. * @param {Object} hitinfo - 见getHitInfo
  1017. */
  1018. proto.processMouseDown = function (hitinfo) {
  1019. const offset = -1;
  1020. const tree = hitinfo.sheet.zh_tree;
  1021. if (!tree) { return; }
  1022. const node = tree.nodes[hitinfo.row];
  1023. if (!node) { return; }
  1024. let centerX = hitinfo.cellRect.x + offset + (node.level) * indent + (node.level) * levelIndent + indent / 2;
  1025. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1026. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1027. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1028. const children = tree.getChildren(node);
  1029. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1030. tree.loadChildren(node, function () {
  1031. node.expanded = true;
  1032. const children = tree.getChildren(node);
  1033. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1034. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1035. });
  1036. } else {
  1037. tree.setExpanded(node, !node.expanded);
  1038. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1039. const posterity = tree.getPosterity(node);
  1040. for (const child of posterity) {
  1041. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1042. }
  1043. });
  1044. hitinfo.sheet.repaint();
  1045. }
  1046. }
  1047. };
  1048. return new TreeNodeCellType();
  1049. },
  1050. /**
  1051. * 获取 带悬浮提示的CellType
  1052. * @returns {TipCellType}
  1053. */
  1054. getTipCellType: function () {
  1055. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1056. const TipCellType = function () {};
  1057. // 继承 SpreadJs定义的 普通的TextCellType
  1058. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1059. const proto = TipCellType.prototype;
  1060. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1061. const xs = hitinfo.sheet.getParent().xs;
  1062. const ctx = xs.childNodes[0].getContext("2d");
  1063. if (font && font !== '') {
  1064. ctx.font = font;
  1065. } else {
  1066. ctx.font = hitinfo.cellStyle.font;
  1067. }
  1068. return ctx.measureText(str).width;
  1069. };
  1070. proto.showTip = function (hitinfo, text) {
  1071. return text && text !== '';
  1072. };
  1073. /**
  1074. * 获取点击信息
  1075. * @param {Number} x
  1076. * @param {Number} y
  1077. * @param {Object} cellStyle
  1078. * @param {Object} cellRect
  1079. * @param {Object} context
  1080. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1081. */
  1082. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1083. return {
  1084. x: x,
  1085. y: y,
  1086. row: context.row,
  1087. col: context.col,
  1088. cellStyle: cellStyle,
  1089. cellRect: cellRect,
  1090. sheet: context.sheet,
  1091. sheetArea: context.sheetArea,
  1092. ctx: context.sheet.getParent().xs,
  1093. };
  1094. };
  1095. /**
  1096. * 鼠标进入单元格事件 - 显示悬浮提示
  1097. * @param {Object} hitinfo - 见getHitInfo返回值
  1098. */
  1099. proto.processMouseEnter = function (hitinfo) {
  1100. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1101. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1102. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1103. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1104. text = col.getTip(sortData[hitinfo.row]);
  1105. }
  1106. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1107. if (pos && this.showTip(hitinfo, text)) {
  1108. if (!this._toolTipElement) {
  1109. let div = $('#autoTip')[0];
  1110. if (!div) {
  1111. div = document.createElement("div");
  1112. $(div).css("position", "absolute")
  1113. .css("border", "1px #C0C0C0 solid")
  1114. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1115. .css("font", "9pt Arial")
  1116. .css("background", "white")
  1117. .css("padding", 5)
  1118. .css("z-index", 999)
  1119. .css("max-width", maxHintWidth)
  1120. .css("word-wrap", "break-word")
  1121. .attr("id", 'autoTip');
  1122. document.body.insertBefore(div, null);
  1123. }
  1124. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1125. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1126. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1127. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1128. } else if (textWidth > maxHintWidth) {
  1129. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1130. } else {
  1131. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1132. }
  1133. this._toolTipElement = div;
  1134. $(div).show("fast");
  1135. }
  1136. }
  1137. };
  1138. /**
  1139. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1140. * @param {Object} hitinfo - 见getHitInfo返回值
  1141. */
  1142. proto.processMouseLeave = function (hitinfo) {
  1143. if (this._toolTipElement) {
  1144. $(this._toolTipElement).hide();
  1145. this._toolTipElement = null;
  1146. }
  1147. };
  1148. return new TipCellType();
  1149. },
  1150. getAutoTipCellType: function () {
  1151. const AutoTipCellType = function () {};
  1152. // 继承 TipCellType
  1153. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1154. const proto = AutoTipCellType.prototype
  1155. proto.showTip = function (hitinfo, text) {
  1156. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.widthidth;
  1157. };
  1158. return new AutoTipCellType();
  1159. },
  1160. /**
  1161. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1162. *
  1163. * img:
  1164. * 1. 整列固定,则传入img的select
  1165. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1166. *
  1167. * 2. 各单元格自定义,则
  1168. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1169. * function getAttachmentImage (data) {
  1170. * $('#attachment-img').url = data.attachmentImageUrl;
  1171. * return $('#attachment-img')[0];
  1172. * }
  1173. *
  1174. * @returns {ImageCellType}
  1175. */
  1176. getImageCellType: function () {
  1177. const ImageCellType = function (){};
  1178. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1179. const proto = ImageCellType.prototype;
  1180. proto.getImage = function (sheet, iRow, iCol) {
  1181. const col = sheet.zh_setting.cols[iCol];
  1182. let imgSource = col.img;
  1183. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1184. const sortData = SpreadJsObj.getSortData(sheet);
  1185. const data = sortData ? sortData[iRow] : null;
  1186. return data ? imgSource(data) : null;
  1187. } else {
  1188. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1189. }
  1190. };
  1191. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1192. const col = options.sheet.zh_setting.cols[options.col];
  1193. const img = this.getImage(options.sheet, options.row, options.col);
  1194. const indent = col.indent ? col.indent : 10;
  1195. if (img) {
  1196. if (style.backColor) {
  1197. canvas.save();
  1198. canvas.fillStyle = style.backColor;
  1199. canvas.fillRect(x, y, indent + img.width, h);
  1200. canvas.restore();
  1201. }
  1202. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1203. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1204. style.hAlign = spreadNS.HorizontalAlign.left;
  1205. }
  1206. x = x + indent + img.width;
  1207. w = w - indent - img.width;
  1208. }
  1209. // Drawing Text
  1210. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1211. };
  1212. /**
  1213. * 获取点击信息
  1214. * @param {Number} x
  1215. * @param {Number} y
  1216. * @param {Object} cellStyle
  1217. * @param {Object} cellRect
  1218. * @param {Object} context
  1219. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1220. */
  1221. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1222. return {
  1223. x: x,
  1224. y: y,
  1225. row: context.row,
  1226. col: context.col,
  1227. cellStyle: cellStyle,
  1228. cellRect: cellRect,
  1229. sheet: context.sheet,
  1230. sheetArea: context.sheetArea
  1231. };
  1232. };
  1233. /**
  1234. * 鼠标点击
  1235. * @param {Object} hitinfo - 见getHitInfo
  1236. */
  1237. proto.processMouseDown = function (hitinfo) {
  1238. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1239. if (img) {
  1240. const halfX = img.width / 2, halfY = img.height / 2;
  1241. const centerX = hitinfo.cellRect.x + indent + halfX;
  1242. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1243. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1244. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1245. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1246. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1247. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1248. const data = sortData ? sortData[hitinfo.row] : null;
  1249. imageClick(data);
  1250. }
  1251. }
  1252. }
  1253. };
  1254. return new ImageCellType();
  1255. },
  1256. /**
  1257. *
  1258. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1259. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1260. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1261. *
  1262. * @returns {ImageCellType}
  1263. */
  1264. getImageButtonCellType: function () {
  1265. let hover = 1, active = 2;
  1266. const ImageCellType = function (){};
  1267. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1268. const proto = ImageCellType.prototype;
  1269. proto.getImage = function (sheet, iRow, iCol) {
  1270. const col = sheet.zh_setting.cols[iCol];
  1271. let imgSource = col.normalImg;
  1272. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1273. if (tag === active) {
  1274. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1275. } else if (tag === hover) {
  1276. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1277. }
  1278. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1279. const sortData = SpreadJsObj.getSortData(sheet);
  1280. const data = sortData ? sortData[iRow] : null;
  1281. return data ? imgSource(data) : null;
  1282. } else {
  1283. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1284. }
  1285. };
  1286. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1287. const col = options.sheet.zh_setting.cols[options.col];
  1288. const sortData = SpreadJsObj.getSortData(options.sheet);
  1289. const data = sortData ? sortData[options.row] : null;
  1290. let showImage = true;
  1291. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1292. showImage = col.showImage(data);
  1293. }
  1294. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1295. const indent = col.indent ? col.indent : 10;
  1296. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1297. if (img) {
  1298. if (style.backColor) {
  1299. canvas.save();
  1300. canvas.fillStyle = style.backColor;
  1301. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1302. canvas.restore();
  1303. }
  1304. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1305. w = w - indent - img.width;
  1306. }
  1307. // Drawing Text
  1308. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1309. } else {
  1310. if (img) {
  1311. if (style.backColor) {
  1312. canvas.save();
  1313. canvas.fillStyle = style.backColor;
  1314. canvas.fillRect(x, y, indent + img.width, h);
  1315. canvas.restore();
  1316. }
  1317. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1318. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1319. style.hAlign = spreadNS.HorizontalAlign.left;
  1320. }
  1321. x = x + indent + img.width;
  1322. w = w - indent - img.width;
  1323. }
  1324. // Drawing Text
  1325. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1326. }
  1327. };
  1328. /**
  1329. * 获取点击信息
  1330. * @param {Number} x
  1331. * @param {Number} y
  1332. * @param {Object} cellStyle
  1333. * @param {Object} cellRect
  1334. * @param {Object} context
  1335. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1336. */
  1337. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1338. return {
  1339. x: x,
  1340. y: y,
  1341. row: context.row,
  1342. col: context.col,
  1343. cellStyle: cellStyle,
  1344. cellRect: cellRect,
  1345. sheet: context.sheet,
  1346. sheetArea: context.sheetArea
  1347. };
  1348. };
  1349. proto.processMouseEnter = function (hitinfo) {
  1350. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1351. // Drawing Image
  1352. if (col.hoverImg) {
  1353. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1354. cell.tag(hover);
  1355. hitinfo.sheet.repaint(hitinfo.cellRect);
  1356. }
  1357. };
  1358. proto.processMouseLeave = function (hitinfo) {
  1359. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1360. // Drawing Image
  1361. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1362. cell.tag(null);
  1363. hitinfo.sheet.repaint(hitinfo.cellRect);
  1364. };
  1365. // proto.processMouseDown = function (hitinfo) {
  1366. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1367. // if (col.activeImg) {
  1368. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1369. // cell.tag(active);
  1370. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1371. // }
  1372. // };
  1373. // proto.processMouseUp = function (hitinfo) {
  1374. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1375. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1376. // const data = sortData ? sortData[hitinfo.row] : null;
  1377. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1378. // if (!col.showImage(data)) {
  1379. // return;
  1380. // }
  1381. // }
  1382. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1383. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1384. // imageClick(data);
  1385. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1386. // cell.tag(null);
  1387. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1388. // }
  1389. // };
  1390. /*
  1391. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1392. */
  1393. proto.processMouseDown = function (hitinfo) {
  1394. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1395. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1396. const halfX = img.width / 2, halfY = img.height / 2;
  1397. const indent = col.indent ? col.indent : 10;
  1398. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1399. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1400. : hitinfo.cellRect.x + indent + halfX;
  1401. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1402. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1403. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1404. cell.tag(active);
  1405. hitinfo.sheet.repaint(hitinfo.cellRect);
  1406. }
  1407. };
  1408. proto.processMouseUp = function (hitinfo) {
  1409. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1410. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1411. const halfX = img.width / 2, halfY = img.height / 2;
  1412. const indent = col.indent ? col.indent : 10;
  1413. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1414. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1415. : hitinfo.cellRect.x + indent + halfX;
  1416. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1417. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1418. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1419. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1420. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1421. const data = sortData ? sortData[hitinfo.row] : null;
  1422. imageClick(data);
  1423. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1424. cell.tag(null);
  1425. hitinfo.sheet.repaint(hitinfo.cellRect);
  1426. }
  1427. }
  1428. };
  1429. proto.processMouseMove = function (hitinfo) {
  1430. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1431. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1432. const halfX = img.width / 2, halfY = img.height / 2;
  1433. const indent = col.indent ? col.indent : 10;
  1434. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1435. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1436. : hitinfo.cellRect.x + indent + halfX;
  1437. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1438. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1439. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1440. if (cell.tag() !== hover) {
  1441. cell.tag(hover);
  1442. hitinfo.sheet.repaint(hitinfo.cellRect);
  1443. }
  1444. } else {
  1445. if (cell.tag() === hover) {
  1446. cell.tag(null);
  1447. hitinfo.sheet.repaint(hitinfo.cellRect);
  1448. }
  1449. }
  1450. };
  1451. return new ImageCellType();
  1452. },
  1453. getActiveImageButtonCellType: function () {
  1454. let show = 1, hover = 2, active = 2;
  1455. const ImageCellType = function (){};
  1456. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1457. const proto = ImageCellType.prototype;
  1458. proto.getImage = function (sheet, iRow, iCol) {
  1459. const col = sheet.zh_setting.cols[iCol];
  1460. let imgSource = null;
  1461. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1462. if (tag === show) {
  1463. imgSource = col.normalImg;
  1464. } else if (tag === active) {
  1465. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1466. } else if (tag === hover) {
  1467. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1468. }
  1469. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1470. const sortData = SpreadJsObj.getSortData(sheet);
  1471. const data = sortData ? sortData[iRow] : null;
  1472. return data ? imgSource(data) : null;
  1473. } else {
  1474. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1475. }
  1476. };
  1477. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1478. const col = options.sheet.zh_setting.cols[options.col];
  1479. const sortData = SpreadJsObj.getSortData(options.sheet);
  1480. const data = sortData ? sortData[options.row] : null;
  1481. let showImage = true;
  1482. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1483. showImage = col.showImage(data);
  1484. }
  1485. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1486. const indent = col.indent ? col.indent : 10;
  1487. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1488. if (img) {
  1489. if (style.backColor) {
  1490. canvas.save();
  1491. canvas.fillStyle = style.backColor;
  1492. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1493. canvas.restore();
  1494. }
  1495. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1496. w = w - indent - img.width;
  1497. }
  1498. // Drawing Text
  1499. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1500. } else {
  1501. if (img) {
  1502. if (style.backColor) {
  1503. canvas.save();
  1504. canvas.fillStyle = style.backColor;
  1505. canvas.fillRect(x, y, indent + img.width, h);
  1506. canvas.restore();
  1507. }
  1508. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1509. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1510. style.hAlign = spreadNS.HorizontalAlign.left;
  1511. }
  1512. x = x + indent + img.width;
  1513. w = w - indent - img.width;
  1514. }
  1515. // Drawing Text
  1516. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1517. }
  1518. };
  1519. /**
  1520. * 获取点击信息
  1521. * @param {Number} x
  1522. * @param {Number} y
  1523. * @param {Object} cellStyle
  1524. * @param {Object} cellRect
  1525. * @param {Object} context
  1526. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1527. */
  1528. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1529. return {
  1530. x: x,
  1531. y: y,
  1532. row: context.row,
  1533. col: context.col,
  1534. cellStyle: cellStyle,
  1535. cellRect: cellRect,
  1536. sheet: context.sheet,
  1537. sheetArea: context.sheetArea
  1538. };
  1539. };
  1540. proto.processMouseEnter = function (hitinfo) {
  1541. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1542. // Drawing Image
  1543. if (col.normalImg) {
  1544. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1545. cell.tag(show);
  1546. hitinfo.sheet.repaint(hitinfo.cellRect);
  1547. }
  1548. };
  1549. proto.processMouseLeave = function (hitinfo) {
  1550. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1551. cell.tag(null);
  1552. hitinfo.sheet.repaint(hitinfo.cellRect);
  1553. };
  1554. // proto.processMouseDown = function (hitinfo) {
  1555. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1556. // if (col.activeImg) {
  1557. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1558. // cell.tag(active);
  1559. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1560. // }
  1561. // };
  1562. // proto.processMouseUp = function (hitinfo) {
  1563. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1564. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1565. // const data = sortData ? sortData[hitinfo.row] : null;
  1566. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1567. // if (!col.showImage(data)) {
  1568. // return;
  1569. // }
  1570. // }
  1571. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1572. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1573. // imageClick(data);
  1574. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1575. // cell.tag(null);
  1576. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1577. // }
  1578. // };
  1579. /*
  1580. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1581. */
  1582. proto.processMouseDown = function (hitinfo) {
  1583. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1584. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1585. const halfX = img.width / 2, halfY = img.height / 2;
  1586. const indent = col.indent ? col.indent : 10;
  1587. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1588. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1589. : hitinfo.cellRect.x + indent + halfX;
  1590. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1591. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1592. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1593. cell.tag(active);
  1594. hitinfo.sheet.repaint(hitinfo.cellRect);
  1595. }
  1596. };
  1597. proto.processMouseUp = function (hitinfo) {
  1598. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1599. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1600. const halfX = img.width / 2, halfY = img.height / 2;
  1601. const indent = col.indent ? col.indent : 10;
  1602. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1603. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1604. : hitinfo.cellRect.x + indent + halfX;
  1605. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1606. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1607. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1608. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1609. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1610. const data = sortData ? sortData[hitinfo.row] : null;
  1611. imageClick(data);
  1612. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1613. cell.tag(hover);
  1614. hitinfo.sheet.repaint(hitinfo.cellRect);
  1615. }
  1616. }
  1617. };
  1618. proto.processMouseMove = function (hitinfo) {
  1619. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1620. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1621. const halfX = img.width / 2, halfY = img.height / 2;
  1622. const indent = col.indent ? col.indent : 10;
  1623. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1624. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1625. : hitinfo.cellRect.x + indent + halfX;
  1626. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1627. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1628. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1629. if (cell.tag() !== hover) {
  1630. cell.tag(hover);
  1631. hitinfo.sheet.repaint(hitinfo.cellRect);
  1632. }
  1633. } else {
  1634. if (cell.tag() === hover) {
  1635. cell.tag(show);
  1636. hitinfo.sheet.repaint(hitinfo.cellRect);
  1637. }
  1638. }
  1639. };
  1640. return new ImageCellType();
  1641. },
  1642. /**
  1643. * 获取 嵌入Html的cellType
  1644. * @returns {HTMLCellType}
  1645. */
  1646. getHtmlCellType: function () {
  1647. const HTMLCellType = function (){};
  1648. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1649. const proto = ImageCellType.prototype;
  1650. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1651. let DOMURL = window.URL || window.webkitURL || window;
  1652. let cell = context.sheet.getCell(context.row, context.col);
  1653. let img = cell.tag();
  1654. if (img) {
  1655. try {
  1656. ctx.save();
  1657. ctx.rect(x, y, w, h);
  1658. ctx.clip();
  1659. ctx.drawImage(img, x + 2, y + 2)
  1660. ctx.restore();
  1661. cell.tag(null);
  1662. return;
  1663. }
  1664. catch (err) {
  1665. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1666. cell.tag(null);
  1667. return;
  1668. }
  1669. }
  1670. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1671. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1672. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1673. let doc = document.implementation.createHTMLDocument("");
  1674. doc.write(data);
  1675. // Get well-formed markup
  1676. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1677. img = new Image();
  1678. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1679. //var url = DOMURL.createObjectURL(svg);
  1680. //img.src = url;
  1681. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1682. cell.tag(img);
  1683. img.onload = function () {
  1684. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1685. }
  1686. };
  1687. return new HTMLCellType();
  1688. },
  1689. /**
  1690. * 获取 字符超长缩略的cellType
  1691. * @returns {EllipsisTextCellType}
  1692. */
  1693. getEllipsisTextCellType: function () {
  1694. const EllipsisTextCellType = function (){};
  1695. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1696. const proto = EllipsisTextCellType.prototype;
  1697. proto.getEllipsisText = function(c, str, maxWidth) {
  1698. var width = c.measureText(str).width;
  1699. var ellipsis = '…';
  1700. var ellipsisWidth = c.measureText(ellipsis).width;
  1701. if (width <= maxWidth || width <= ellipsisWidth) {
  1702. return str;
  1703. } else {
  1704. var len = str.length;
  1705. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1706. str = str.substring(0, len);
  1707. width = c.measureText(str).width;
  1708. }
  1709. return str + ellipsis;
  1710. }
  1711. };
  1712. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1713. ctx.font = style.font;
  1714. value = this.getEllipsisText(ctx, value, w - 2);
  1715. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1716. };
  1717. return new EllipsisTextCellType();
  1718. },
  1719. getEllipsisTextAutoTipCellType: function () {
  1720. const CellType = function () {};
  1721. // 继承 TipCellType
  1722. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1723. const proto = CellType.prototype;
  1724. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1725. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1726. proto.paint = ellipsisTextCellType.paint;
  1727. return new CellType();
  1728. },
  1729. /**
  1730. * 获取 动态显示ComboBox的cellType
  1731. * @returns {ActiveComboCellType}
  1732. */
  1733. getActiveComboCellType: function () {
  1734. const ActiveComboCellType = function () {};
  1735. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1736. const proto = ActiveComboCellType.prototype;
  1737. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1738. const sheet = options.sheet;
  1739. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1740. && !sheet.getCell(options.row, options.col).locked()) {
  1741. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1742. } else {
  1743. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1744. }
  1745. };
  1746. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1747. const sheet = options.sheet;
  1748. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1749. && !sheet.getCell(options.row, options.col).locked()) {
  1750. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1751. } else {
  1752. return {
  1753. x: x,
  1754. y: y,
  1755. row: options.row,
  1756. col: options.col,
  1757. cellStyle: cellStyle,
  1758. cellRect: cellRect,
  1759. sheetArea: options.sheetArea
  1760. };
  1761. }
  1762. };
  1763. return new ActiveComboCellType();
  1764. },
  1765. /**
  1766. * 获取 单位的CellType
  1767. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1768. */
  1769. getUnitCellType: function () {
  1770. let combo = this.getActiveComboCellType();
  1771. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1772. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '每一试桩',
  1773. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1774. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1775. return combo;
  1776. },
  1777. /**
  1778. * 获取 自定义的CellType
  1779. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1780. */
  1781. getCustomizeComboCellType: function (items) {
  1782. let combo = this.getActiveComboCellType();
  1783. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1784. return combo;
  1785. },
  1786. },
  1787. Clipboard: (function () {
  1788. let cText = '', cHtml;
  1789. const setCopyData = function (text, html) {
  1790. cText = text;
  1791. cHtml = html;
  1792. };
  1793. const getPasteData = function () {
  1794. return {text: cText, html: cHtml};
  1795. };
  1796. const clearData = function () {
  1797. cText = '';
  1798. cHtml = undefined;
  1799. };
  1800. const setSheetFilterCopyData = function (sheet) {
  1801. cText = SpreadJsObj.getFilterCopyText(sheet);
  1802. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  1803. };
  1804. const getAnalysisPasteText = function () {
  1805. return SpreadJsObj.analysisPasteText(cText);
  1806. };
  1807. const getAnalysisPasteHtml = function () {
  1808. return SpreadJsObj.analysisPasteHtml(cHtml);
  1809. };
  1810. return {
  1811. setCopyData, getPasteData, clearData,
  1812. setSheetFilterCopyData,
  1813. getAnalysisPasteText, getAnalysisPasteHtml,
  1814. }
  1815. })(),
  1816. };