sheet_common.js 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var sheetCommonObj = {
  5. // CSL.2017.06.05
  6. // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
  7. createSpread: function(container, SheetCount){
  8. var me = this;
  9. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: SheetCount });
  10. spreadBook.options.tabStripVisible = false;
  11. spreadBook.options.showHorizontalScrollbar = true;
  12. spreadBook.options.showVerticalScrollbar = true;
  13. spreadBook.options.allowCopyPasteExcelStyle = false;
  14. spreadBook.options.allowUserDragDrop = true;
  15. spreadBook.options.allowContextMenu = false;
  16. spreadBook.options.allowUserEditFormula = false;
  17. spreadBook.options.showDragFillSmartTag = false;
  18. return spreadBook;
  19. },
  20. initSheet: function(sheet, setting, rowCount) {
  21. var me = this;
  22. var spreadNS = GC.Spread.Sheets;
  23. sheet.suspendPaint();
  24. sheet.suspendEvent();
  25. if(setting.frozenCols) sheet.frozenColumnCount(setting.frozenCols);//冻结列s
  26. sheet.setRowCount(setting.headRows ? setting.headRows : 1, spreadNS.SheetArea.colHeader);
  27. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  28. if (setting.headRows > 1){
  29. setting.header.forEach(function (head, index) {
  30. var i, iRow = 0, cell;
  31. header.spanCols?header.spanCols:[1]
  32. for (i = 0; i < head.spanCols.length; i++) {
  33. if (head.spanCols[i] !== 0) {
  34. cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
  35. //cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
  36. if(Array.isArray(head.headerName)) cell.value(head.headerName[i]).hAlign(head.hAlign[i]).vAlign(head.vAlign[i]).wordWrap(true);
  37. }
  38. if (head.spanCols[i] > 1 || head.spanRows[i] > 1) {
  39. sheet.addSpan(iRow, index, head.spanRows[i], head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
  40. }
  41. iRow += head.spanRows[i];
  42. };
  43. // sheet.setColumnWidth(index, width);
  44. // sheet.setColumnVisible(index, visible && true);
  45. });
  46. }
  47. if (setting && setting.view && setting.view.colHeaderHeight) {
  48. sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
  49. };
  50. if (setting && setting.view && setting.view.rowHeaderWidth) {
  51. sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
  52. };
  53. if (setting.emptyRowHeader) {
  54. sheet.setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  55. }
  56. sheet.options.colHeaderAutoTextIndex = 1;
  57. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  58. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  59. sheet.options.protectionOptions = {
  60. allowResizeColumns: true
  61. };
  62. sheet.showRowOutline(false);
  63. sheet.options.allowCellOverflow = false;
  64. me.buildHeader(sheet, setting);
  65. if (rowCount > 0)
  66. sheet.setRowCount(rowCount);
  67. else
  68. sheet.setRowCount(1);
  69. sheet.resumeEvent();
  70. sheet.resumePaint();
  71. },
  72. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  73. buildSheet: function(container, setting, rowCount) {
  74. var me = this;
  75. var spreadBook = me.createSpread(container, { sheetCount: 1 });
  76. var sheet = spreadBook.getSheet(0);
  77. me.initSheet(sheet, setting, rowCount);
  78. return spreadBook;
  79. },
  80. buildHeader: function(sheet, setting){
  81. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  82. for (var i = 0; i < setting.header.length; i++) {
  83. sheet.setValue(0, i, setting.header[i].headerName, ch);
  84. sheet.getCell(0, i, ch).wordWrap(true);
  85. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  86. sheet.setColumnVisible(i,setting.header[i].visible === false ? false:true);
  87. }
  88. },
  89. cleanSheet: function(sheet, setting, rowCount) {
  90. sheet.suspendPaint();
  91. sheet.suspendEvent();
  92. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  93. if (rowCount > 0) sheet.setRowCount(rowCount);
  94. sheet.clearSelection();
  95. sheet.resumeEvent();
  96. sheet.resumePaint();
  97. },
  98. cleanData: function (sheet, setting, rowCount) {
  99. sheet.suspendPaint();
  100. sheet.suspendEvent();
  101. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  102. if (rowCount > 0) sheet.setRowCount(rowCount);
  103. sheet.resumeEvent();
  104. sheet.resumePaint();
  105. },
  106. setAreaAlign: function(area, hAlign, vAlign){
  107. if (!(hAlign) || hAlign === "left") {
  108. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  109. } else if (hAlign === "right") {
  110. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  111. } else if (hAlign === "center") {
  112. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  113. } else {
  114. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  115. }
  116. if (!(vAlign) || vAlign === "center") {
  117. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  118. } else if (vAlign === "top") {
  119. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  120. } else if (vAlign === "bottom") {
  121. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  122. } else {
  123. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  124. }
  125. },
  126. showTreeData:function (sheet,setting,data) {
  127. let ch = GC.Spread.Sheets.SheetArea.viewport;
  128. let parentMap=_.groupBy(data, 'ParentID');
  129. let visibleMap = {};
  130. let styleRow=[];
  131. sheet.suspendPaint();
  132. sheet.suspendEvent();
  133. for (let col = 0; col < setting.header.length; col++) {
  134. let hAlign = "left", vAlign = "center";
  135. if (setting.header[col].hAlign) {
  136. hAlign = setting.header[col].hAlign;
  137. } else if (setting.header[col].dataType !== "String"){
  138. hAlign = "right";
  139. }
  140. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  141. sheetCommonObj.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  142. if (setting.header[col].formatter) {
  143. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  144. }
  145. if(setting.header[col].cellType === "comboBox"){
  146. this.setComboBox(-1,col,sheet,setting.header[col].options,setting.header[col].editorValueType,setting.header[col].editable,setting.header[col].maxDropDownItems);
  147. }
  148. for (let row = 0; row < data.length; row++) {
  149. if(data[row].cellType === 'comboBox'){
  150. let options = data[row].options ? data[row].options.split("@") : [];
  151. this.setComboBox(row,col,sheet,options);
  152. }else if(data[row].cellType === 'String'){//默认设置字符输入,避免出现输入10:01变成日期的情况
  153. sheet.setFormatter(row, col,"@", GC.Spread.Sheets.SheetArea.viewport);
  154. }
  155. let val = data[row][setting.header[col].dataCode];
  156. if(val&&setting.header[col].dataType === "Number"){
  157. if(setting.header[col].hasOwnProperty('decimalField')){
  158. let decimal = getDecimal(setting.header[col].decimalField);
  159. val =scMathUtil.roundToString(val,decimal);
  160. sheet.setFormatter(row, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  161. }else {
  162. val =scMathUtil.roundToString(val,2);
  163. }
  164. }
  165. sheet.setValue(row, col, val, ch);
  166. if(col==0){
  167. let treeType = sheetCommonObj.getTreeNodeCellType(data,row,parentMap);
  168. sheet.getCell(row, 0).cellType(treeType);
  169. visibleMap[data[row].ID] = treeType.collapsed;
  170. this.setRowVisible(data,row,visibleMap,sheet);
  171. }
  172. if(data[row].bgColour) styleRow.push(row)
  173. }
  174. }
  175. for(let r of styleRow){
  176. this.setRowStyle(r,sheet,data[r].bgColour);
  177. }
  178. this.lockCells(sheet,setting);
  179. sheet.resumeEvent();
  180. sheet.resumePaint();
  181. },
  182. setRowVisible:function (data,row,visibleMap,sheet) {
  183. sheet.getRange(row , -1, 1, -1).visible(getVisible(data[row].ParentID));//显示或隐藏
  184. function getVisible(ParentID) {
  185. if(visibleMap[ParentID]) return false //如果父节点是缩起的,那就隐藏本身。
  186. if(visibleMap[ParentID] == false){//如果父节点不是缩起的,要再往父节点找看
  187. let pnode = _.find(data,{'ID':ParentID});
  188. if(pnode) return getVisible(pnode.ParentID);//如果有父节点,递归调用
  189. return true;//没有,返回显示
  190. }
  191. }
  192. },
  193. setSheetBySetting: function (sheet, setting) {
  194. function setCellType(range, cellType) {
  195. let type;
  196. if (cellType === 'checkBox') {
  197. type = new GC.Spread.Sheets.CellTypes.CheckBox();
  198. }
  199. range.cellType(type);
  200. }
  201. setting.header.forEach((item, index) => {
  202. const range = sheet.getRange(-1, index, -1, 1);
  203. this.setAreaAlign(range, item.hAlign, item.vAlign);
  204. if (item.cellType) {
  205. console.log(item);
  206. setCellType(range, item.cellType);
  207. }
  208. if (item.formatter) {
  209. range.formatter(item.formatter)
  210. }
  211. });
  212. },
  213. appendData: function (sheet, index, rowIndex, setting, data) {
  214. /*const viewport = GC.Spread.Sheets.SheetArea.viewport;
  215. const rowHeader = GC.Spread.Sheets.SheetArea.rowHeader;*/
  216. if (!index) {
  217. sheet.setRowCount(0);
  218. }
  219. sheet.suspendPaint();
  220. sheet.suspendEvent();
  221. // 追加空行
  222. sheet.addRows(index, data.length);
  223. // 显示数据
  224. data.forEach((item, rIdx) => {
  225. const row = index + rIdx;
  226. setting.header.forEach((hItem, cIdx) => {
  227. const dataCode = hItem.dataCode;
  228. const val = gljUtil.isDef(item[dataCode]) ? item[dataCode] : '';
  229. sheet.setValue(row, cIdx, val);
  230. });
  231. });
  232. sheet.resumeEvent();
  233. sheet.resumePaint();
  234. },
  235. showData: function(sheet, setting, data,distTypeTree,callback) {
  236. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  237. sheet.suspendPaint();
  238. sheet.suspendEvent();
  239. //sheet.addRows(row, 1);
  240. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  241. if(sheet.getRowCount()<data.length){
  242. data.length<30? sheet.setRowCount(30):sheet.setRowCount(data.length);
  243. }else if(sheet.getRowCount()==0){
  244. sheet.setRowCount(30);
  245. }
  246. for (var col = 0; col < setting.header.length; col++) {
  247. var hAlign = "left", vAlign = "center";
  248. if (setting.header[col].hAlign) {
  249. hAlign = setting.header[col].hAlign;
  250. } else if (setting.header[col].dataType !== "String"){
  251. hAlign = "right";
  252. }
  253. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  254. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  255. if (setting.header[col].formatter) {
  256. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  257. }
  258. if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
  259. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  260. sheet.deleteColumns(col,1);
  261. sheet.addColumns(col, 1);
  262. sheet.setValue(0, col, setting.header[col].headerName, header);
  263. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  264. }
  265. if(setting.header[col].visible!==null&&setting.header[col].visible!==undefined){
  266. sheet.setColumnVisible(col,setting.header[col].visible);
  267. }
  268. sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  269. }
  270. for (var row = 0; row < data.length; row++) {
  271. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  272. this.showRowData(sheet,setting,row,data,distTypeTree);
  273. if(setting.getStyle && setting.getStyle(data[row])){
  274. sheet.setStyle(row, -1, setting.getStyle(data[row]));
  275. }
  276. }
  277. if(setting.emptyRowHeader){
  278. let rowCount = sheet.getRowCount();
  279. for (let row = 0; row < rowCount; row++) {
  280. sheet.setValue(row, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  281. }
  282. }
  283. this.lockCells(sheet,setting);
  284. if(callback) callback();
  285. sheet.resumeEvent();
  286. sheet.resumePaint();
  287. //me.shieldAllCells(sheet);
  288. },
  289. showRowData:function (sheet,setting,row,data,distTypeTree=null) {
  290. let ch = GC.Spread.Sheets.SheetArea.viewport;
  291. if(data[row].cellType == "date"){//日期格式比较特殊,要判断数据里的属性,为data类型,同时读取列
  292. this.setDatePickerCellType(row,data[row].dateCol,sheet)
  293. }
  294. if(data[row].cellType === 'comboBox'){//对于选项在数据里下拉框类型,同时读取列
  295. let options = data[row].options ? data[row].options.split("@") : [];
  296. this.setComboBox(row,data[row].dateCol,sheet,options);
  297. }
  298. for (var col = 0; col < setting.header.length; col++) {
  299. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  300. var val = data[row][setting.header[col].dataCode];
  301. if(val&&setting.header[col].dataType === "Number"){
  302. if(setting.header[col].hasOwnProperty('tofix')){
  303. val =scMathUtil.roundToString(val,setting.header[col].tofix);
  304. }
  305. else if(setting.header[col].hasOwnProperty('decimalField')){
  306. var decimal = getDecimal(setting.header[col].decimalField);
  307. val =scMathUtil.roundToString(val,decimal);
  308. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  309. }else {
  310. val =val+'';
  311. }
  312. }
  313. if(val!=null&&setting.header[col].cellType === "checkBox"){
  314. this.setCheckBoxCell(row,col,sheet,val)
  315. }
  316. if(setting.header[col].cellType === "comboBox"){
  317. this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType,setting.header[col].editable,setting.header[col].maxDropDownItems);
  318. }
  319. if(setting.header[col].cellType === "selectButton"){
  320. this.setSelectButton(row,col,sheet,setting.header[col]);
  321. }
  322. if(setting.header[col].cellType === "replaceButton"){
  323. this.setReplaceButton(row,col,sheet,setting.header[col]);
  324. }
  325. if(setting.header[col].cellType === "cusButton"){
  326. this.setCusButton(row,col,sheet,setting);
  327. }
  328. if(setting.header[col].cellType === "tipsCell"){
  329. this.setTipsCell(row,col,sheet,setting.header[col]);
  330. }
  331. if(setting.owner==='gljTree'){
  332. if(setting.header[col].cellType === "checkBox"){
  333. val==1?val:0;
  334. this.setCheckBoxCell(row,col,sheet,val);
  335. }
  336. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  337. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  338. val=distTypeVal;
  339. }
  340. }
  341. if(setting.header[col].getText){
  342. val = setting.getText[setting.header[col].getText](data[row],val)
  343. }
  344. sheet.setValue(row, col, val, ch);
  345. }
  346. this.setRowStyle(row,sheet,data[row].bgColour);
  347. if(setting.autoFit==true){//设置自动行高
  348. if(setting.fitRow && setting.fitRow.length > 0){//如果有设置特定的某些列才需要自动行高就按设置的来,没有设置就默认所有列
  349. for(let dataCode of setting.fitRow){
  350. let col = _.findIndex(setting.header,{dataCode:dataCode})
  351. sheet.getCell(row,col).wordWrap(true);
  352. }
  353. }else {
  354. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  355. }
  356. sheet.autoFitRow(row);
  357. }
  358. },
  359. checkData : function(col,setting, value) {
  360. let result = true;
  361. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  362. if (validator === null) {
  363. return result;
  364. }
  365. switch (validator) {
  366. case 'number':
  367. let regular = /^\d+(\.\d+)?$/;
  368. result = regular.test(value);
  369. break;
  370. case 'boolean':
  371. let booleanValue = [true, false];
  372. result = booleanValue.indexOf(value) >= 0;
  373. break;
  374. }
  375. return result;
  376. },
  377. //todo
  378. analyzePasteData: function(setting, pastedInfo) {
  379. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  380. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  381. if (pastedInfo.pasteData.text[i] === "\n") {
  382. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  383. preStrIdx = i + 1;
  384. rst.push(itemObj);
  385. if (i < pastedInfo.pasteData.text.length - 1) {
  386. itemObj = {};
  387. }
  388. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  389. if (setting.header[propId]) {
  390. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  391. }
  392. propId++;
  393. preStrIdx = i + 1;
  394. //if the last copied-cell were empty, should check whether the end of text
  395. if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  396. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  397. rst.push(itemObj);
  398. }
  399. } else if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  400. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  401. rst.push(itemObj);
  402. }
  403. }
  404. return rst;
  405. },
  406. combineRowData: function(sheet, setting, row) {
  407. var rst = {};
  408. for (var col = 0; col < setting.header.length; col++) {
  409. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  410. }
  411. return rst;
  412. },
  413. shieldAllCells: function(sheet) {
  414. sheet.options.isProtected = true;
  415. },
  416. unShieldAllCells: function(sheet) {
  417. sheet.options.isProtected = false;
  418. },
  419. lockCells: function(sheet, setting){
  420. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  421. sheet.options.isProtected = true;
  422. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  423. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  424. let col = setting.view.lockColumns[i];
  425. if(_.isString(col)){//如果是dataCode 进行转换
  426. col = _.findIndex(setting.header,{dataCode:col})
  427. }
  428. sheet.getRange(-1,col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  429. }
  430. }
  431. if (setting && Array.isArray(setting.view.lockRows) && setting.view.lockRows.length) {
  432. if (!setting.view.lockColumns || !setting.view.lockColumns.length) {
  433. sheet.options.isProtected = true;
  434. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  435. }
  436. setting.view.lockRows.forEach(row => {
  437. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  438. });
  439. }
  440. },
  441. setCheckBoxCell(row,col,sheet,val){
  442. var c = this.getCheckBox();
  443. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  444. sheet.getCell(row, col).value(val);
  445. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  446. },
  447. getCheckBox(threeState = false){
  448. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  449. c.isThreeState(threeState);
  450. return c
  451. },
  452. // 无法勾选的复选框
  453. getReadOnlyCheckBox () {
  454. function ReadOnlyCheckBox() {}
  455. ReadOnlyCheckBox.prototype = new GC.Spread.Sheets.CellTypes.CheckBox();
  456. ReadOnlyCheckBox.prototype.processMouseUp = function () {
  457. return;
  458. };
  459. return new ReadOnlyCheckBox();
  460. },
  461. setComboBox(row,col,sheet,options,editorValueType,editable,maxDropDownItems){
  462. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  463. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  464. if(options){
  465. dynamicCombo.items(options);
  466. if(maxDropDownItems) dynamicCombo.maxDropDownItems(maxDropDownItems);
  467. if(editable == true) dynamicCombo.editable(true);//可编辑
  468. if(editorValueType==true){
  469. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  470. }
  471. }
  472. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  473. },
  474. setRowStyle(row,sheet,bgColour) {
  475. if(bgColour){
  476. let style = new GC.Spread.Sheets.Style();
  477. style.backColor = bgColour;
  478. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  479. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  480. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  481. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  482. sheet.setStyle(row, -1, style);
  483. }
  484. },
  485. getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
  486. let me = this;
  487. function CustomerCoeCellType() {
  488. this.isEscKey=false;
  489. this.displayText='';
  490. }
  491. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  492. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  493. console.log("create editor")
  494. let element = document.createElement("div");//这里创建的,会自动销毁
  495. return element
  496. };
  497. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  498. if (editorContext) {
  499. $editor = $(editorContext);
  500. $editor.css("position", "fixed");
  501. $editor.css("background", "white");
  502. //$editor.css("width", cellRect.width); 2018-11-15 改成固定列宽
  503. $editor.css("width", 160);
  504. $editor.attr("gcUIElement", "gcEditingInput");
  505. if(htmlGenerator) htmlGenerator(context,cellRect,$editor);
  506. }
  507. }
  508. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  509. };
  510. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  511. console.log("set editor value");
  512. this.displayText = value;
  513. };
  514. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  515. console.log("get value");
  516. if(this.isEscKey !=true&& updateCallback){
  517. updateCallback();
  518. }
  519. this.isEscKey = false;
  520. return this.displayText;
  521. };
  522. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  523. console.log(" update editor");
  524. if( setEditorValue){//不是esc时才更新
  525. setEditorValue(context);
  526. }
  527. };
  528. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  529. //cell type handle tab key by itself
  530. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  531. return false;
  532. };
  533. return new CustomerCoeCellType();
  534. },
  535. scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
  536. let topRow = sheet.getViewportTopRow(1);
  537. if(row == topRow) return;//已经是最顶行了
  538. let length = options&&options.length>0?options.length:1;
  539. let height = cxt.canvas.height;
  540. let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
  541. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  542. if(endY <= height) return; //如果没有超出显示位置,直接返回
  543. let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
  544. let showRow = topRow + overRow > row?row:topRow + overRow;
  545. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  546. },
  547. setSelectButton(row,col,sheet,header){
  548. /* let getSelectButton = function (cellWidth=100) {
  549. function moreButton() {
  550. }
  551. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  552. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  553. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  554. let buttonW = cellWidth/5;
  555. let endX = x+w-2;
  556. if(value){
  557. let textWidth = ctx.measureText(value).width;
  558. let spaceWidth = cellWidth - buttonW;
  559. let textEndX = x+2+textWidth;
  560. if(spaceWidth<textWidth){
  561. for(let i = value.length-1;i>1;i--){
  562. let newValue = value.substr(0,i);
  563. let newTestWidth = ctx.measureText(newValue).width;
  564. if(spaceWidth>newTestWidth){
  565. value = newValue;
  566. textEndX = x+2+newTestWidth;
  567. break;
  568. }
  569. }
  570. }
  571. ctx.fillText(value,textEndX,y+h-5);
  572. }
  573. //画三个点
  574. ctx.save();
  575. ctx.beginPath();
  576. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  577. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  578. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  579. ctx.fillStyle="black";//填充颜色,默认是黑色
  580. ctx.fill();//画实心圆
  581. ctx.closePath();
  582. ctx.restore();
  583. };
  584. moreButton.prototype.processMouseLeave= function (hitinfo) {
  585. let newCell = new selectButton();
  586. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  587. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  588. };
  589. function selectButton() {
  590. }
  591. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  592. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  593. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  594. };
  595. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  596. return {
  597. x: x,
  598. y: y,
  599. row: context.row,
  600. col: context.col,
  601. cellStyle: cellStyle,
  602. cellRect: cellRect,
  603. sheetArea: context.sheetArea
  604. };
  605. };
  606. selectButton.prototype.processMouseDown = function (hitinfo){
  607. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  608. let b1 = new moreButton();
  609. b1.marginLeft(cellWidth*4/5);
  610. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  611. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  612. }
  613. };
  614. return new selectButton();
  615. };*/
  616. sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  617. },
  618. setCusButton:function (row,col,sheet,setting) {
  619. let functionName = setting.header[col].callback;
  620. let readOnly = setting.disable[setting.header[col].disable];
  621. if(typeof(readOnly) == 'function' ){
  622. readOnly = readOnly(row,col);
  623. }
  624. if(functionName){
  625. sheet.setCellType(row, col,this.getCusButtonCellType(setting.callback[functionName],readOnly));
  626. }
  627. //sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  628. },
  629. getCusButtonCellType:function (callback,readOnly=false) {
  630. var ns = GC.Spread.Sheets;
  631. function CusButtonCellType() {
  632. }
  633. CusButtonCellType.prototype = new ns.CellTypes.Text();
  634. CusButtonCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  635. if(!readOnly){
  636. if(options.sheet.getActiveRowIndex()==options.row&&options.sheet.getActiveColumnIndex()==options.col){
  637. var image = document.getElementById('f_btn'),imageMagin = 3;
  638. var imageHeight = 15;
  639. var imageWidth = 25;
  640. var imageX = x + w - imageWidth- imageMagin, imageY = y + h / 2 - imageHeight / 2;
  641. ctx.save();
  642. if(style.backColor){
  643. ctx.fillStyle = style.backColor;
  644. ctx.fillRect(x,y,w,h);
  645. }
  646. ctx.drawImage(image, imageX, imageY,imageWidth,imageHeight);
  647. ctx.beginPath();
  648. ctx.arc(imageX+imageWidth/2,imageY+imageHeight/2,1,0,360,false);
  649. ctx.arc(imageX+imageWidth/2-4,imageY+imageHeight/2,1,0,360,false);
  650. ctx.arc(imageX+imageWidth/2+4,imageY+imageHeight/2,1,0,360,false);
  651. ctx.fillStyle="black";//填充颜色,默认是黑色
  652. ctx.fill();//画实心圆
  653. ctx.closePath();
  654. ctx.restore();
  655. w = w - imageWidth - imageMagin;
  656. //这里的左对齐的,当显示的字长度超过空白地方时,要改成右对齐
  657. if(style.hAlign == 0){
  658. if(value){
  659. let textWidth = ctx.measureText(value).width;
  660. let spaceWidth = w;
  661. if(spaceWidth<textWidth){
  662. style.hAlign = 2;
  663. }
  664. }
  665. }
  666. }
  667. }
  668. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  669. };
  670. CusButtonCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  671. return {
  672. x: x,
  673. y: y,
  674. row: context.row,
  675. col: context.col,
  676. cellStyle: cellStyle,
  677. cellRect: cellRect,
  678. sheetArea: context.sheetArea
  679. };
  680. };
  681. CusButtonCellType.prototype.processMouseDown = function (hitinfo) {
  682. if(hitinfo.sheet.getActiveRowIndex()==hitinfo.row&&hitinfo.sheet.getActiveColumnIndex()==hitinfo.col){
  683. var offset=hitinfo.cellRect.x+hitinfo.cellRect.width-6;
  684. var imageWidth = 25;
  685. if(hitinfo.x<offset&&hitinfo.x>offset-imageWidth){
  686. if(!readOnly){
  687. if(callback) callback(hitinfo)
  688. }
  689. }
  690. }
  691. };
  692. return new CusButtonCellType();
  693. },
  694. getSelectButton(cellWidth=100){
  695. function moreButton() {
  696. }
  697. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  698. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  699. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  700. ctx.font = '14px Calibri';
  701. let buttonW = cellWidth/5;
  702. let endX = x+w-2;
  703. if(value){
  704. let textWidth = ctx.measureText(value).width;
  705. let spaceWidth = cellWidth - buttonW;
  706. let textEndX = x+textWidth+2;
  707. if(spaceWidth<textWidth){
  708. for(let i = value.length-1;i>1;i--){
  709. let newValue = value.substr(0,i);
  710. let newTestWidth = ctx.measureText(newValue).width;
  711. if(spaceWidth>newTestWidth){
  712. value = newValue;
  713. textEndX = x+newTestWidth+2;
  714. break;
  715. }
  716. }
  717. }
  718. ctx.fillText(value,textEndX,y+h-6);
  719. }
  720. //画三个点
  721. ctx.save();
  722. ctx.beginPath();
  723. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  724. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  725. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  726. ctx.fillStyle="black";//填充颜色,默认是黑色
  727. ctx.fill();//画实心圆
  728. ctx.closePath();
  729. ctx.restore();
  730. };
  731. moreButton.prototype.processMouseLeave= function (hitinfo) {
  732. let newCell = new selectButton();
  733. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  734. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  735. };
  736. function selectButton() {
  737. }
  738. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  739. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  740. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  741. };
  742. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  743. return {
  744. x: x,
  745. y: y,
  746. row: context.row,
  747. col: context.col,
  748. cellStyle: cellStyle,
  749. cellRect: cellRect,
  750. sheetArea: context.sheetArea
  751. };
  752. };
  753. selectButton.prototype.processMouseDown = function (hitinfo){
  754. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  755. let b1 = new moreButton();
  756. b1.marginLeft(cellWidth*4/5);
  757. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  758. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  759. }
  760. };
  761. return new selectButton();
  762. },
  763. setReplaceButton(row,col,sheet){
  764. let replaceButton = function(){
  765. };
  766. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  767. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  768. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this,arguments);
  769. if(value){
  770. ctx.save();
  771. ctx.fillStyle = "white";
  772. let fh = options.fontInfo&&options.fontInfo.fontSize?options.fontInfo.fontSize:h-6;
  773. ctx.fillText(value,x+(w+ctx.measureText(value).width)/2,y+(h+fh)/2-2);
  774. ctx.restore();
  775. }
  776. };
  777. let cellType = new replaceButton();
  778. cellType.buttonBackColor("#07A0FF");
  779. sheet.setCellType(row, col,cellType,GC.Spread.Sheets.SheetArea.viewport);
  780. },
  781. setDatePickerCellType(row,col,sheet){
  782. let ns = GC.Spread.Sheets;
  783. function DatePickerCellType() {
  784. }
  785. DatePickerCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  786. DatePickerCellType.prototype.createEditorElement = function (context) {
  787. //Create input presenter.
  788. return document.createElement("input");
  789. };
  790. DatePickerCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  791. //Initialize input editor.
  792. if (editorContext) {
  793. $editor = $(editorContext);
  794. //DatePickerCellType.prototype.activateEditor.apply(this, arguments);
  795. $editor.datepicker({dateFormat: 'yy-mm-dd'});
  796. $editor.css("position", "absolute");
  797. $editor.attr("gcUIElement", "gcEditingInput");
  798. $(".ui-datepicker").attr("gcUIElement", "gcEditingInput");
  799. }
  800. }
  801. DatePickerCellType.prototype.deactivateEditor = function (editorContext, context) {
  802. //Remove input editor when end editor status.
  803. if (editorContext) {
  804. var element = editorContext;
  805. $(element).datepicker("hide");
  806. $(element).datepicker("destroy");
  807. }
  808. // DatePickerCellType.prototype.deactivateEditor.apply(this, arguments)
  809. };
  810. DatePickerCellType.prototype.setEditorValue = function (editor, value, context) {
  811. //Sync value from Cell value to editor value.
  812. $(editor).datepicker("setDate", value);
  813. };
  814. DatePickerCellType.prototype.getEditorValue = function (editor, context) {
  815. //Sync value from editor value to cell value.
  816. return $(editor).datepicker("getDate");
  817. };
  818. DatePickerCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  819. if (editorContext) {
  820. $editor = $(editorContext);
  821. $editor.css("width", cellRect.width - 1);
  822. $editor.css("height", cellRect.height - 3);
  823. }
  824. };
  825. let picker = new DatePickerCellType();
  826. sheet.getCell(row, col).cellType(picker).width(100).formatter('yyyy-mm-dd');
  827. },
  828. setTipsCell(row,col,sheet,header){
  829. let TipCellType = function () {};
  830. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  831. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  832. return {
  833. x: x,
  834. y: y,
  835. row: context.row,
  836. col: context.col,
  837. cellStyle: cellStyle,
  838. cellRect: cellRect,
  839. sheet: context.sheet,
  840. sheetArea: context.sheetArea
  841. };
  842. };
  843. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  844. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  845. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  846. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  847. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  848. zoom = hitinfo.sheet.zoom();
  849. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  850. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  851. let setting = {};
  852. if(textLength <= cellWidth){
  853. return;
  854. }
  855. if(sheet && sheet.getParent().qo){
  856. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  857. }
  858. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  859. };
  860. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  861. TREE_SHEET_HELPER.tipDiv = 'hide';
  862. if (TREE_SHEET_HELPER._toolTipElement) {
  863. $(TREE_SHEET_HELPER._toolTipElement).hide();
  864. TREE_SHEET_HELPER._toolTipElement = null;
  865. };
  866. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  867. };
  868. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  869. },
  870. chkIfEmpty: function(rObj, setting) {
  871. var rst = true;
  872. if (rObj) {
  873. for (var i = 0; i < setting.header.length; i++) {
  874. if (rObj[setting.header[i].dataCode]) {
  875. rst = false;
  876. break;
  877. }
  878. }
  879. }
  880. return rst;
  881. },
  882. //add by zhong 2017-10-10
  883. //动态下拉框,配合EnterCell, args.sheet.repaint();
  884. getDynamicCombo: function (forLocked) {
  885. let ComboCellForActiveCell = function () { };
  886. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  887. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  888. let sheet = options.sheet;
  889. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  890. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  891. } else {
  892. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  893. }
  894. };
  895. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  896. let sheet = options.sheet;
  897. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  898. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  899. } else {
  900. return {
  901. x: x,
  902. y: y,
  903. row: options.row,
  904. col: options.col,
  905. cellStyle: cellStyle,
  906. cellRect: cellRect,
  907. sheetArea: options.sheetArea
  908. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  909. }
  910. };
  911. return new ComboCellForActiveCell();
  912. },
  913. getTipsCombo:function (forLocked,tips,setting,node) {
  914. let getTipsCombo = function () {
  915. this.clickCom=false;
  916. };
  917. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  918. if(tips && tips !=""){
  919. getTipsCombo.prototype.processMouseEnter = function(hitinfo){
  920. if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
  921. this.clickCom = false;
  922. return;
  923. }
  924. let text = typeof tips == 'function'?tips(node):tips;
  925. TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,text);
  926. };
  927. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  928. TREE_SHEET_HELPER.hideTipsDiv();
  929. };
  930. getTipsCombo.prototype.processMouseDown = function (hitinfo){
  931. if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
  932. TREE_SHEET_HELPER.hideTipsDiv();
  933. this.clickCom = true;
  934. }
  935. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  936. };
  937. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
  938. TREE_SHEET_HELPER.hideTipsDiv();
  939. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  940. };
  941. }
  942. return new getTipsCombo();
  943. },
  944. getTreeNodeCellType:function (datas,row,parentMap) {// 2018-09-26 不用spreadjs默认的树结构,自定义控件
  945. var ns = GC.Spread.Sheets;
  946. let rectW = 10;
  947. let rectH = 10;
  948. let margin = 3;
  949. function TreeNodeCellType() {
  950. this.collapsed = gljUtil.isDef(datas[row].collapsed)?datas[row].collapsed: true; //默认是折叠的
  951. this.treeNodeType = true;
  952. this.rectInfo = {};
  953. }
  954. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  955. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  956. let offset = 0;
  957. let step = 7;
  958. let level = getTreeLevel(datas[row],datas);//从0开始,取当前节点是第几级的
  959. let tem = offset+margin+ rectW/2+step;//两条线之间的间隔
  960. let t_offset = offset;
  961. let temParentID = datas[row].ParentID;
  962. if(this.treeNodeType == true){
  963. for(let i = level;i>0;i--){//这里是画子节点前面的竖线,从第二级开始
  964. let temParent = getParent(temParentID,datas);
  965. if(temParent){//父节点有下一个兄弟节点才需要画
  966. if(hasNextBrother(parentMap,temParent)) sheetCommonObj.drawLine(ctx,x+t_offset+tem*i,y,x+t_offset+tem*i,y+h);
  967. temParentID = temParent.ParentID;
  968. }
  969. offset +=tem;
  970. }
  971. }
  972. offset+=step; //这个没法移动,所以要两个判断
  973. if(this.treeNodeType == true){
  974. if(hasChildern(datas[row].ID,datas)){//如果是有子节点
  975. //第一条 或者没有父节点(如费率子表综合里程项)不用画方框头上那条竖线其它都要
  976. if(row !=0 && gljUtil.isDef(datas[row].ParentID)) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y,x+offset+ rectW/2+margin,y + Math.round(h / 2) - rectH / 2);
  977. //画方框下面的那条竖线,如果没有下一个兄弟节点,则不用画
  978. if(hasNextBrother(parentMap,datas[row])) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y + Math.round(h / 2) + rectH / 2,x+offset+ rectW/2+margin,y + h);
  979. sheetCommonObj.drowRect(ctx, x+offset, y, w, h,rectW,rectH,margin);
  980. sheetCommonObj.drowSymbol(ctx, x+offset, y, w, h,rectW,rectH,margin, this.collapsed);
  981. this.rectInfo = {x:x+offset+margin,rectW:rectW}//计录一下可点击位置
  982. }else {
  983. let hasNext = datas[row+1]?datas[row+1].ParentID == datas[row].ParentID:false;
  984. sheetCommonObj.drowSubItem(ctx, x, y, w, h, offset,hasNext,margin+ rectW/2);
  985. }
  986. }
  987. offset += step;
  988. offset += rectW;
  989. x = x + offset;//设置偏移
  990. w = w - offset;
  991. if(datas[row].foreColor) style.foreColor = datas[row].foreColor;
  992. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  993. };
  994. // override getHitInfo to allow cell type get mouse messages
  995. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  996. return {
  997. x: x,
  998. y: y,
  999. row: context.row,
  1000. col: context.col,
  1001. cellStyle: cellStyle,
  1002. cellRect: cellRect,
  1003. sheetArea: context.sheetArea
  1004. };
  1005. }
  1006. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  1007. ////方框外1像素内都有效
  1008. if (!_.isEmpty(this.rectInfo)&&Math.floor(hitinfo.x) <= this.rectInfo.x+this.rectInfo.rectW+2 && Math.floor(hitinfo.x) >= this.rectInfo.x-2) {
  1009. this.collapsed = !this.collapsed;
  1010. datas[row].collapsed = this.collapsed;
  1011. this.refreshChildrenVisible(hitinfo.sheet);
  1012. hitinfo.sheet.invalidateLayout();
  1013. hitinfo.sheet.repaint();
  1014. }
  1015. };
  1016. TreeNodeCellType.prototype.refreshChildrenVisible = function (sheet) {
  1017. sheet.suspendPaint();
  1018. sheet.suspendEvent();
  1019. refreshVisible(datas[row]);
  1020. sheet.resumeEvent();
  1021. sheet.resumePaint();
  1022. function refreshVisible(item){
  1023. if(parentMap[item.ID]){
  1024. for(let sub of parentMap[item.ID]){
  1025. refreshVisible(sub)
  1026. }
  1027. }
  1028. let visible = getVisible(item);
  1029. let trow = datas.indexOf(item);
  1030. sheet.getRange(trow , -1, 1, -1).visible(visible);
  1031. }
  1032. function getVisible(item) {
  1033. if(item.ParentID){
  1034. let parent = getParent(item.ParentID,datas);
  1035. if(!parent) return true;
  1036. let p_row= datas.indexOf(parent);
  1037. let visible = !sheet.getCellType(p_row,0).collapsed;
  1038. if(visible == true){ //如果是显示的,则要再往父节点的父节点检查,只要有一个节点是隐藏的,则都是隐藏
  1039. return getVisible(parent);
  1040. }else {
  1041. return visible
  1042. }
  1043. }else {//如果parentID 为空则是最根节点
  1044. return true;
  1045. }
  1046. }
  1047. };
  1048. return new TreeNodeCellType()
  1049. function getTreeLevel(item,data) {
  1050. if(item.ParentID && item.ParentID!=-1){
  1051. let pitem = _.find(data,{'ID':item.ParentID});
  1052. return getTreeLevel(pitem,data) + 1;
  1053. }else {
  1054. return 0
  1055. }
  1056. }
  1057. function hasChildern(ID,data) {//返回是否有子项
  1058. let p = _.find(data,{'ParentID':ID});
  1059. if(p) return true;
  1060. return false
  1061. }
  1062. function getParent(ParentID,data) {
  1063. let p = _.find(data,{'ID':ParentID});
  1064. return p;
  1065. }
  1066. function hasNextBrother(parentMap,item){
  1067. let children =parentMap[item.ParentID];
  1068. if(!gljUtil.isDef(children)|| children.indexOf(item) == children.length -1) return false;
  1069. return true
  1070. }
  1071. },
  1072. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  1073. let me = this;
  1074. sheet.suspendPaint();
  1075. let combo = me.getDynamicCombo();
  1076. for(let i = 0, len = rowCount; i < len; i++){
  1077. if(itemsHeight) {
  1078. combo.itemHeight(itemsHeight);
  1079. combo._maxDropDownItems = itemsHeight + 5;
  1080. }
  1081. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1082. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  1083. else combo.items(items);
  1084. sheet.getCell(beginRow + i, col).cellType(combo);
  1085. }
  1086. sheet.resumePaint();
  1087. },
  1088. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  1089. sheet.suspendPaint();
  1090. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  1091. for(let i = 0, len = rowCount; i < len; i++){
  1092. if(itemsHeight) combo.itemHeight(itemsHeight);
  1093. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1094. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  1095. else combo.items(items);
  1096. sheet.getCell(beginRow + i, col).cellType(combo);
  1097. }
  1098. sheet.resumePaint();
  1099. },
  1100. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  1101. copyTextToClipboard: function(text) {
  1102. let textArea = document.createElement("textarea");
  1103. textArea.style.position = 'fixed';
  1104. textArea.style.top = 0;
  1105. textArea.style.left = 0;
  1106. textArea.style.width = '2em';
  1107. textArea.style.height = '2em';
  1108. textArea.style.padding = 0;
  1109. textArea.style.border = 'none';
  1110. textArea.style.outline = 'none';
  1111. textArea.style.boxShadow = 'none';
  1112. textArea.style.background = 'transparent';
  1113. textArea.value = text;
  1114. document.body.appendChild(textArea);
  1115. textArea.select();
  1116. try {
  1117. let successful = document.execCommand('copy');
  1118. let msg = successful ? 'successful' : 'unsuccessful';
  1119. console.log('Copying text command was ' + msg);
  1120. } catch (err) {
  1121. console.log('Oops, unable to copy');
  1122. }
  1123. document.body.removeChild(textArea);
  1124. },
  1125. //获取选中区域的表格类型数据(可粘贴到excel)
  1126. getTableData: function (sheet, colSettings = null) {
  1127. let rst = '';
  1128. let sel = sheet.getSelections()[0];
  1129. let pasteText = [];
  1130. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  1131. if(!sheet.getCell(row, -1).visible())
  1132. continue;
  1133. let rowText = [];
  1134. for(let j = 0; j < sel.colCount; j++){
  1135. let col = sel.col + j;
  1136. if(!sheet.getCell(-1, col).visible())
  1137. continue;
  1138. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  1139. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  1140. else
  1141. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  1142. }
  1143. pasteText.push(rowText.join('\t'));
  1144. }
  1145. return pasteText.join('\n');
  1146. },
  1147. transferToTreeSetting:function(setting,treeSetting,treeCol){
  1148. for(let h of setting.header){
  1149. treeSetting.cols.push(getSettingCol(h))
  1150. }
  1151. for(let l of setting.view.lockColumns){
  1152. treeSetting.cols[l].readOnly = true;
  1153. }
  1154. return treeSetting;
  1155. function getSettingCol(header) {
  1156. let aMap ={left:0,center:1,right:2};
  1157. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  1158. let col = {
  1159. "width":header.headerWidth?header.headerWidth:100,
  1160. "head":{
  1161. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  1162. "spanCols":header.spanCols?header.spanCols:[1],
  1163. "spanRows":header.spanRows?header.spanRows:[1],
  1164. "vAlign":[1],
  1165. "hAlign":[1],
  1166. "font":["Arial"]
  1167. },
  1168. "data": {
  1169. "field": header.dataCode,
  1170. "vAlign": 1,
  1171. "hAlign": hAlign,
  1172. "font": "Arial"
  1173. }
  1174. };
  1175. if(header.showHint == true){
  1176. col.showHint = true;
  1177. }
  1178. if(header.cellType){
  1179. col.data.cellType = getCellType(header);
  1180. }
  1181. if(header.decimalField){//设置formatter
  1182. let decimal = getDecimal(header.decimalField);
  1183. col.formatter = getFormatter(decimal);
  1184. }
  1185. if(header.getText && treeCol){
  1186. col.data.getText = treeCol.getEvent(header.getText);
  1187. }
  1188. /*col.readOnly = function (node) {
  1189. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  1190. return true;
  1191. }
  1192. return false;
  1193. };*/
  1194. return col;
  1195. }
  1196. function getCellType(header) {
  1197. return function () {
  1198. if(header.cellType === "checkBox"){
  1199. return new GC.Spread.Sheets.CellTypes.CheckBox();
  1200. }
  1201. if(header.cellType === "comboBox"){
  1202. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  1203. if(header.options){
  1204. dynamicCombo.itemHeight(header.options.length).items(header.options);
  1205. if(header.editorValueType==true){
  1206. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1207. }
  1208. }
  1209. return dynamicCombo
  1210. }
  1211. }
  1212. }
  1213. },
  1214. //注册自定义回车键事件
  1215. bindEnterKey: function (workBook, operation) {
  1216. workBook.commandManager().register('myEnter', operation);
  1217. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  1218. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  1219. },
  1220. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  1221. bindEscKey: function (workBook, sheets) {
  1222. function isDef(v){
  1223. return typeof v !== 'undefined' && v !== null;
  1224. }
  1225. workBook.commandManager().register('myEsc', function () {
  1226. let activeSheet = workBook.getActiveSheet();
  1227. let hasTheSheet = false;
  1228. for(let sheetObj of sheets){
  1229. let sheet = sheetObj.sheet;
  1230. if(sheet === activeSheet){
  1231. hasTheSheet = true;
  1232. let editStarting = sheetObj.editStarting;
  1233. let editEnded = sheetObj.editEnded;
  1234. if(editStarting){
  1235. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  1236. }
  1237. if(editEnded){
  1238. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  1239. }
  1240. let row = sheet.getActiveRowIndex();
  1241. let col = sheet.getActiveColumnIndex();
  1242. let orgV = sheet.getValue(row, col);
  1243. let orgText = sheet.getText(row, col);
  1244. if(!isDef(orgV)){
  1245. orgV = '';
  1246. }
  1247. if(sheet.isEditing()){
  1248. sheet.endEdit();
  1249. sheet.setValue(row, col, orgV);
  1250. }
  1251. if(editStarting){
  1252. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  1253. }
  1254. if(editEnded){
  1255. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  1256. }
  1257. }
  1258. }
  1259. //容错处理,以防没把所有工作簿的表格信息传入参数
  1260. if(!hasTheSheet){
  1261. if(activeSheet.isEditing()){
  1262. activeSheet.endEdit();
  1263. }
  1264. }
  1265. });
  1266. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  1267. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  1268. },
  1269. //设置默认样式
  1270. spreadDefaultStyle: function (workBook) {
  1271. let defaultStyle = new GC.Spread.Sheets.Style();
  1272. defaultStyle.font = '14px Calibri';
  1273. let sheetCount = workBook.getSheetCount();
  1274. for(let i = 0; i < sheetCount; i++){
  1275. let sheet = workBook.getSheet(i);
  1276. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  1277. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  1278. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  1279. }
  1280. },
  1281. //动态根据工作簿宽度和各列宽度比例设置宽度
  1282. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  1283. if(workBook){
  1284. const sheet = workBook.getActiveSheet();
  1285. sheet.suspendEvent();
  1286. sheet.suspendPaint();
  1287. for(let col = 0; col < headers.length; col++){
  1288. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  1289. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1290. }
  1291. else {
  1292. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  1293. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1294. }
  1295. }
  1296. }
  1297. sheet.resumeEvent();
  1298. sheet.resumePaint();
  1299. }
  1300. },
  1301. drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
  1302. ctx.save();
  1303. ctx.strokeStyle = "gray";
  1304. ctx.translate(0.5, 0.5);
  1305. ctx.beginPath();
  1306. let rectX = x + margin;
  1307. let rectY = y + Math.round(h / 2) - rectH / 2;
  1308. ctx.moveTo(rectX, rectY);
  1309. ctx.lineTo(rectX, rectY + rectH);
  1310. ctx.lineTo(rectX + rectW, rectY + rectH);
  1311. ctx.lineTo(rectX + rectW, rectY);
  1312. ctx.lineTo(rectX, rectY);
  1313. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  1314. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  1315. ctx.stroke();
  1316. ctx.restore();
  1317. },
  1318. drawLine : function (ctx, x1, y1, x2, y2, color) {
  1319. let l_color = color?color:"gray";
  1320. ctx.save();
  1321. ctx.translate(0.5, 0.5);
  1322. ctx.beginPath();
  1323. ctx.moveTo(x1, y1);
  1324. ctx.lineTo(x2, y2);
  1325. ctx.strokeStyle = l_color;
  1326. ctx.stroke();
  1327. ctx.restore();
  1328. },
  1329. drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
  1330. ctx.save();
  1331. ctx.strokeStyle = "#000000";
  1332. ctx.translate(0.5, 0.5);
  1333. ctx.beginPath();
  1334. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  1335. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  1336. let rectY = y + Math.round(h / 2) - rectH / 2;
  1337. if (collapsed) {
  1338. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  1339. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  1340. }
  1341. ctx.stroke();
  1342. ctx.restore();
  1343. },
  1344. drowTriangle:function(ctx,x,y){//画向下三角形
  1345. ctx.save();
  1346. ctx.fillStyle = "black";
  1347. ctx.beginPath();
  1348. ctx.moveTo(x, y);
  1349. ctx.lineTo(x-3, y -6);
  1350. ctx.lineTo(x+3, y -6);
  1351. ctx.fill();
  1352. ctx.restore();
  1353. },
  1354. drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
  1355. let t_step = step?step:6;
  1356. offset += t_step;
  1357. ctx.save();
  1358. ctx.strokeStyle = "gray";
  1359. ctx.translate(0.5, 0.5);
  1360. ctx.beginPath();
  1361. ctx.moveTo(x + offset, y);
  1362. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1363. offset += 9;
  1364. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1365. if (hasNext) {
  1366. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  1367. ctx.lineTo(x + offset - 9, y + h);
  1368. }
  1369. ctx.stroke();
  1370. ctx.restore();
  1371. return offset;
  1372. },
  1373. checkData:function(col,setting, value) {
  1374. let result = true;
  1375. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  1376. if (validator === null) {
  1377. return result;
  1378. }
  1379. switch (validator) {
  1380. case 'number':
  1381. let regular = /^\d+(\.\d+)?$/;
  1382. result = regular.test(value);
  1383. break;
  1384. case 'boolean':
  1385. let booleanValue = [true, false];
  1386. result = booleanValue.indexOf(value) >= 0;
  1387. break;
  1388. }
  1389. return result;
  1390. }
  1391. }