请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

Dtttax

银牌会员

274

主题

680

帖子

2184

积分

银牌会员

积分
2184
Dtttax
银牌会员   /  发表于:2018-12-10 15:47  /   查看:3874  /  回复:1
  let sheet = spread.getActiveSheet();
   for(let row=0;row<sheet.getRowCount();row++){
    for(let col=0;col<sheet.getColumnCount();col++){
        let cell=sheet.getCell(row,col)
        let style=sheet.getStyle(row,col,GC.Spread.Sheets.SheetArea.viewport, true);
       if(cell.backColor()!=BACKCOLOR){
         if(cell.formula()!=null && cell.formula()!=""){
           style.locked = true;
           sheet.getCell(row, col).locked(true)
           sheet.setStyle(row, col, style);
         }
       }else{
          style.locked = false;
          sheet.getCell(row, col).locked(false)
          sheet.setStyle(row, col, style);
       }
     }
   }
  sheet.options.isProtected = true;

1 个回复

倒序浏览
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2018-12-10 17:34:42
沙发
您好,看了一下您的代码,问题有以下几点:

1、您设置表单保护后,不必给全部的单元格遍历设置style,只需要设置sheet的默认style为非锁定,即可实现所有单元格都可以编辑,当然sheet的默认样式优先级最低,性能最好,这样方便您再给指定单元格进行锁定操作。
示例代码:
  1. var sheetStyle = sheet.getDefaultStyle();
  2. sheetStyle.locked = false;
  3. sheet.setDefaultStyle(sheetStyle);
复制代码


2、您从每个单元格处获取style,但是单元格的style默认是null,因此您需要通过创建新的style,或者调用getActualStyle获取当前单元格的实际样式。API:
http://help.grapecity.com/spread ... sheet~getStyle.html

http://help.grapecity.com/spread ... getActualStyle.html

就您这段代码来看,我这边给您提供一个修改版本的代码,您可以运行试试:


  1.   let sheet = spread.getActiveSheet();
  2.   let sheetStyle = sheet.getDefaultStyle();
  3.   sheetStyle.locked = false;
  4.   sheet.setDefaultStyle(sheetStyle);
  5.    for(let row=0;row<sheet.getRowCount();row++){
  6.     for(let col=0;col<sheet.getColumnCount();col++){
  7.         let cell=sheet.getCell(row,col)
  8.                 // getActualStyle方法是获取当前单元格的实际样式
  9.         let style=sheet.getActualStyle(row,col,GC.Spread.Sheets.SheetArea.viewport, true);
  10.        if(cell.backColor()!=BACKCOLOR){
  11.          if(cell.formula()!=null && cell.formula()!=""){
  12.            style.locked = true;
  13.            sheet.getCell(row, col).locked(true)
  14.            sheet.setStyle(row, col, style);
  15.          }
  16.        }
  17.      }
  18.    }
  19.   sheet.options.isProtected = true;
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部