sheet_common.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var sheetCommonObj = {
  5. // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
  6. // buildSheet 在一个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.allowCopyPasteExcelStyle = false;
  11. spreadBook.options.allowExtendPasteRange = true;
  12. spreadBook.options.tabStripVisible = false;
  13. //spreadBook.options.showHorizontalScrollbar = false;
  14. spreadBook.options.allowUserDragDrop = false;
  15. spreadBook.options.scrollbarMaxAlign = true;
  16. return spreadBook;
  17. },
  18. initSheet: function(sheet, setting, rowCount) {
  19. var me = this;
  20. var spreadNS = GC.Spread.Sheets;
  21. sheet.suspendPaint();
  22. sheet.suspendEvent();
  23. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  24. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  25. sheet.options.colHeaderAutoTextIndex = 1;
  26. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  27. sheet.options.protectionOptions = {
  28. allowResizeRows: true,
  29. allowResizeColumns: true
  30. };
  31. sheet.showRowOutline(false);
  32. me.buildHeader(sheet, setting);
  33. if (rowCount > 0) sheet.setRowCount(rowCount);
  34. sheet.resumeEvent();
  35. sheet.resumePaint();
  36. },
  37. buildSheet: function(container, setting, rowCount) {
  38. var me = this;
  39. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: 1 });
  40. spreadBook.options.tabStripVisible = false;
  41. //spreadBook.options.showHorizontalScrollbar = false;
  42. spreadBook.options.scrollbarMaxAlign = true;
  43. spreadBook.options.allowCopyPasteExcelStyle = false;
  44. spreadBook.options.allowExtendPasteRange = true;
  45. spreadBook.options.allowUserDragDrop = false;
  46. var spreadNS = GC.Spread.Sheets;
  47. var sheet = spreadBook.getSheet(0);
  48. sheet.suspendPaint();
  49. sheet.suspendEvent();
  50. //Set rowHeader count and columnHeader count.
  51. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  52. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  53. sheet.options.colHeaderAutoTextIndex = 1;
  54. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  55. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  56. sheet.options.protectionOptions = {
  57. allowResizeRows: true,
  58. allowResizeColumns: true
  59. };
  60. sheet.showRowOutline(false);
  61. //setup column header
  62. me.buildHeader(sheet, setting);
  63. //setup cells
  64. if (rowCount > 0) sheet.setRowCount(rowCount);
  65. sheet.resumeEvent();
  66. sheet.resumePaint();
  67. return spreadBook;
  68. },
  69. buildHeader: function(sheet, setting){
  70. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  71. for (var i = 0; i < setting.header.length; i++) {
  72. sheet.setValue(0, i, setting.header[i].headerName, ch);
  73. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  74. }
  75. },
  76. cleanData: function (sheet, setting, rowCount) {
  77. sheet.suspendPaint();
  78. sheet.suspendEvent();
  79. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  80. if (rowCount > 0) sheet.setRowCount(rowCount);
  81. sheet.resumeEvent();
  82. sheet.resumePaint();
  83. },
  84. cleanSheet: function(sheet, setting, rowCount) {
  85. sheet.suspendPaint();
  86. sheet.suspendEvent();
  87. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  88. if (rowCount > 0) sheet.setRowCount(rowCount);
  89. sheet.clearSelection();
  90. sheet.resumeEvent();
  91. sheet.resumePaint();
  92. },
  93. setAreaAlign: function(area, hAlign, vAlign){
  94. if (!(hAlign) || hAlign === "left") {
  95. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  96. } else if (hAlign === "right") {
  97. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  98. } else if (hAlign === "center") {
  99. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  100. } else {
  101. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  102. }
  103. if (!(vAlign) || vAlign === "center") {
  104. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  105. } else if (vAlign === "top") {
  106. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  107. } else if (vAlign === "bottom") {
  108. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  109. } else {
  110. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  111. }
  112. },
  113. showData: function(sheet, setting, data) {
  114. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  115. sheet.suspendPaint();
  116. sheet.suspendEvent();
  117. //sheet.addRows(row, 1);
  118. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  119. for (var col = 0; col < setting.header.length; col++) {
  120. var hAlign = "left", vAlign = "center";
  121. if (setting.header[col].hAlign) {
  122. hAlign = setting.header[col].hAlign;
  123. } else if (setting.header[col].dataType !== "String"){
  124. hAlign = "right";
  125. }
  126. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  127. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  128. if (setting.header[col].formatter) {
  129. //var style = new GC.Spread.Sheets.Style();
  130. //style.formatter = setting.header[col].formatter;
  131. //sheet.setStyle(row,col,style,GC.Spread.Sheets.SheetArea.viewport);
  132. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  133. }
  134. if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
  135. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  136. sheet.deleteColumns(col,1);
  137. sheet.addColumns(col, 1);
  138. sheet.setValue(0, col, setting.header[col].headerName, header);
  139. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  140. }
  141. for (var row = 0; row < data.length; row++) {
  142. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  143. var val = data[row][setting.header[col].dataCode];
  144. if(val&&setting.header[col].dataType === "Number"){
  145. if(setting.header[col].hasOwnProperty('tofix')){
  146. val =parseFloat(val).toFixed(setting.header[col].tofix);
  147. }else {
  148. val =parseFloat(val).toFixed(2);
  149. }
  150. }
  151. if(val!=null&&setting.header[col].cellType === "checkBox"){
  152. this.setCheckBoxCell(row,col,sheet,val)
  153. }
  154. sheet.setValue(row, col, val, ch);
  155. }
  156. }
  157. this.lockCells(sheet,setting);
  158. sheet.resumeEvent();
  159. sheet.resumePaint();
  160. //me.shieldAllCells(sheet);
  161. },
  162. analyzePasteData: function(setting, pastedInfo) {
  163. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};
  164. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  165. if (pastedInfo.pasteData.text[i] === "\n") {
  166. propId = pastedInfo.cellRange.col;
  167. preStrIdx = i + 1;
  168. rst.push(itemObj);
  169. if (i < pastedInfo.pasteData.text.length - 1) {
  170. itemObj = {};
  171. }
  172. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  173. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  174. propId++;
  175. preStrIdx = i + 1;
  176. //if the last copied-cell were empty, should check whether the end of text
  177. if (i == pastedInfo.pasteData.text.length - 1) {
  178. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  179. rst.push(itemObj);
  180. }
  181. } else if (i == pastedInfo.pasteData.text.length - 1) {
  182. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  183. rst.push(itemObj);
  184. }
  185. }
  186. return rst;
  187. },
  188. combineRowData: function(sheet, setting, row) {
  189. var rst = {};
  190. for (var col = 0; col < setting.header.length; col++) {
  191. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  192. }
  193. return rst;
  194. },
  195. shieldAllCells: function(sheet) {
  196. sheet.options.isProtected = true;
  197. },
  198. unShieldAllCells: function(sheet) {
  199. sheet.options.isProtected = false;
  200. },
  201. unLockAllCells: function (sheet) {
  202. sheet.suspendPaint();
  203. sheet.suspendEvent();
  204. let defaultStyle = new GC.Spread.Sheets.Style();
  205. defaultStyle.locked = false;
  206. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  207. sheet.setStyle(-1, 0, defaultStyle);
  208. sheet.options.isProtected = false;
  209. sheet.resumePaint();
  210. sheet.resumeEvent();
  211. },
  212. lockAllCells: function (sheet) {
  213. sheet.suspendPaint();
  214. sheet.suspendEvent();
  215. let defaultStyle = new GC.Spread.Sheets.Style();
  216. defaultStyle.locked = true;
  217. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  218. for(let i = 0; i < sheet.getRowCount(); i++){
  219. sheet.setStyle(i, 0, defaultStyle);
  220. }
  221. sheet.options.isProtected = true;
  222. sheet.resumePaint();
  223. sheet.resumeEvent();
  224. },
  225. lockCells: function(sheet, setting){
  226. sheet.suspendPaint();
  227. sheet.suspendEvent();
  228. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  229. sheet.options.isProtected = true;
  230. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  231. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  232. sheet.getRange(-1,setting.view.lockColumns[i] , -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  233. }
  234. }
  235. sheet.resumePaint();
  236. sheet.resumeEvent();
  237. },
  238. setLockCol: function (sheet, col, isLocked) {
  239. sheet.suspendPaint();
  240. sheet.suspendEvent();
  241. for(let row = 0, len = sheet.getRowCount(); row < len; row++){
  242. sheet.getCell(row, col).locked(isLocked);
  243. }
  244. sheet.resumePaint();
  245. sheet.resumeEvent();
  246. },
  247. chkIfEmpty: function(rObj, setting) {
  248. var rst = true;
  249. if (rObj) {
  250. for (var i = 0; i < setting.header.length; i++) {
  251. if (rObj[setting.header[i].dataCode]) {
  252. rst = false;
  253. break;
  254. }
  255. }
  256. }
  257. return rst;
  258. },
  259. //add by zhong 2017-10-10
  260. //动态下拉框
  261. getDynamicCombo: function () {
  262. let ComboCellForActiveCell = function () { };
  263. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  264. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  265. let sheet = options.sheet;
  266. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()) {
  267. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  268. } else {
  269. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  270. }
  271. };
  272. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  273. let sheet = options.sheet;
  274. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()) {
  275. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  276. } else {
  277. return GC.Spread.Sheets.CellTypes.Base.prototype.getHitInfo.apply(this, arguments);
  278. }
  279. };
  280. return new ComboCellForActiveCell();
  281. },
  282. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  283. let me = this;
  284. sheet.suspendPaint();
  285. let combo = me.getDynamicCombo();
  286. for(let i = 0, len = rowCount; i < len; i++){
  287. if(itemsHeight) combo.itemHeight(itemsHeight);
  288. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  289. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  290. else combo.items(items);
  291. sheet.getCell(beginRow + i, col).cellType(combo);
  292. }
  293. sheet.resumePaint();
  294. },
  295. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  296. sheet.suspendPaint();
  297. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  298. for(let i = 0, len = rowCount; i < len; i++){
  299. if(itemsHeight) combo.itemHeight(itemsHeight);
  300. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  301. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  302. else combo.items(items);
  303. sheet.getCell(beginRow + i, col).cellType(combo);
  304. }
  305. sheet.resumePaint();
  306. }
  307. }