背景:客户希望在表单中空单元格上显示"-",以表示该单元格无值且不能修改。原理:自定义单元格的paint方法中判断是否为空,如为空则渲染为"-",这样不会更改单元格的真实值。
核心代码(完整示例参考附件):
- var spreadNS = GC.Spread.Sheets;
- window.onload = function () {
- var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
- initSpread(spread);
- };
- function ShowValueCellType() {
- }
- ShowValueCellType.prototype = new spreadNS.CellTypes.Text();
- ShowValueCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
- if (value === null || value === undefined) {
- value = "-";
- }
- spreadNS.CellTypes.Base.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, options]);
- };
-
- function initSpread(spread) {
- var sheet = spread.getSheet(0);
- sheet.suspendPaint();
- sheet.setRowCount(8);
- sheet.setColumnCount(10);
- sheet.setValue(0,0, "值1", GC.Spread.Sheets.SheetArea.colHeader);
- sheet.setValue(0,1, "值2", GC.Spread.Sheets.SheetArea.colHeader);
- sheet.setValue(0,2, "值1+值2", GC.Spread.Sheets.SheetArea.colHeader);
- for(var i=0; i<sheet.getRowCount()-1; i++){
- sheet.setValue(i, 0, i);
- sheet.setValue(i, 1, sheet.getRowCount() - i);
- var j = i+1;
- sheet.setFormula(i, 2, "=A"+j+"+B"+j);
- sheet.setValue(i, 2, i+1);
- }
- sheet.getRange(0,0,sheet.getRowCount(),sheet.getColumnCount()).cellType(new ShowValueCellType());
- sheet.setFormula(7, 2, "=A8+B8");
- sheet.resumePaint();
- };
复制代码
|
|