找回密码
 立即注册

QQ登录

只需一步,快速开始

sean_chen

中级会员

44

主题

121

帖子

849

积分

中级会员

积分
849
sean_chen
中级会员   /  发表于:2019-11-14 13:57  /   查看:2444  /  回复:1
你好,
给某个单元格设置了自定义单元格类型,如果不符合要求,背景色设置为红色,如何获取这个背景色?
getActualStyle,getStyle, getCellType等都无法获取这个颜色,请参考附件中,导出页面的单元格(5,1)


SpreadJSVue.zip

1.48 MB, 下载次数: 26

1 个回复

倒序浏览
Fiooona
论坛元老   /  发表于:2019-11-14 17:41:47
沙发
是这样的,paint方法中给style设置背景色,这时候没有给单元格的Style中改变相关属性,只是在画法的层面让单元格显示了红色,用这个方法来改变单元格背景色:
  options.sheet.getCell(options.row,options.col).backColor("red");

改变单元格背景色会导致页面重绘,所以在paint方法中加了判断:
  if(options.sheet.getCell(options.row,options.col).backColor()!="red")
参考代码:
NegativeValidator.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  if(options.sheet.getCell(options.row,options.col).backColor()!="red"){
          if (this._type === 'negative') {
                if (value > 0) {
                  this._isError = true
                  options.sheet.getCell(options.row,options.col).backColor("red");
                } else {
                  this._isError = false
                }
          } else if (this._type === 'positive') {
                if (value < 0) {
                  this._isError = true
                  options.sheet.getCell(options.row,options.col).backColor("red");
                } else {
                  this._isError = false
                }
          } else if (this._type === 'letterType') {
                const reg = /((?=[\x21-\x7e]+)[^A-Za-z0-9])|([0-9]+)/g
                if (!reg.test(value)) {
                  // 正确的值
                  this._isError = false
                } else {
                  this._isError = true
                  options.sheet.getCell(options.row,options.col).backColor("red");
                }
          } else if (this._type === 'musty') {
                if (!this._result) {
                  this._isError = true
                  options.sheet.getCell(options.row,options.col).backColor("red");
                } else {
                  this._isError = false
                }
          }
         }
        spreadNS.CellTypes.Base.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, options])
}
组件化表格编辑器(预览版)试用进行中,点击了解详情!
请点击评分,对我的服务做出评价!5分为非常满意!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部