فهرست منبع

excel第一列禁止编辑但是允许增加

xujunjie 2 سال پیش
والد
کامیت
2c476b4dad
1فایلهای تغییر یافته به همراه108 افزوده شده و 66 حذف شده
  1. 108 66
      src/core.js

+ 108 - 66
src/core.js

@@ -47,7 +47,7 @@ let luckysheet = {};
 luckysheet = common_extend(api, luckysheet);
 
 //创建luckysheet表格
-luckysheet.create = function (setting) {
+luckysheet.create = function(setting) {
   method.destroy();
   server.websocket && server.websocket.close(1000, "Work complete");
   // Store original parameters for api: toJson
@@ -178,77 +178,17 @@ luckysheet.create = function (setting) {
     // luckysheetsizeauto();
     initialWorkBook();
   } else {
-    $.get(authorityUrl, function (res) {
+    $.get(authorityUrl, function(res) {
       let arr = res.data.filter(
         (item) => item.template?.template_id == templateId
       );
       console.log(arr);
-      let allowRangeList = [],
-        colhidden = {};
 
-      $.post(loadurl, { gridKey: server.gridKey }, function (d) {
+      $.post(loadurl, { gridKey: server.gridKey }, function(d) {
         let data = new Function("return " + d)();
-        let sheet = data[0];
-        let cache = {};
-        if (sheet.config) {
-          delete sheet.config.authority
-        }
-        sheet.celldata.forEach((item) => {
-          if (!cache[item.c]) cache[item.c] = [];
-          cache[item.c].push(item);
-        });
-        arr.forEach((item) => {
-          // 将A B C D....转换为0 1 2 3...
-          let col_idx = item.template.col_idx;
-          let col = col_idx.charCodeAt(0) - 65;
-          if (!item.can_read) {
-            colhidden[col] = 0;
-          }
-          if (item.can_write) {
-            // 区域保护
-            allowRangeList.push({
-              hintText: "",
-              sqref: `$${col_idx}$0:$${col_idx}$99999`,
-            });
-          }
+        data.forEach((sheet) => {
+          initSheet(sheet, arr, authority);
         });
-        // let celldata = []
-        // Object.values(cache).forEach(colItem => {
-        //     celldata = [...celldata,...colItem]
-        // })
-        // sheet.celldata = celldata
-        sheet.config = sheet.config || {};
-        // 处于审批时  所有人都无法修改
-        if (authority) {
-          sheet.config.authority = authority;
-        } else if (arr.length > 0) {
-          let newAuthority = {
-            sheet: true,
-            hintText: "没有编辑权限!",
-          };
-          if (allowRangeList.length > 0) {
-            // 可编辑内容
-            newAuthority.allowRangeList = allowRangeList
-          }
-
-          sheet.config.authority = newAuthority
-        }
-
-        sheet.config.colhidden = colhidden;
-
-        // 公式计算
-        let calcChain = [];
-        let index = sheet.index;
-        sheet.celldata.forEach(item => {
-          if (typeof (item.v.f) !== 'undefined') {
-            calcChain.push({
-              r: item.r,
-              c: item.c,
-              index,
-            })
-          }
-        })
-        sheet.calcChain = calcChain;
 
         console.log(data);
         Store.luckysheetfile = data;
@@ -317,7 +257,7 @@ luckysheet.getdatabyselection = getdatabyselection;
 luckysheet.sheetmanage = sheetmanage;
 
 // Data of the current table
-luckysheet.flowdata = function () {
+luckysheet.flowdata = function() {
   return Store.flowdata;
 };
 
@@ -332,3 +272,105 @@ luckysheet.hideLoadingProgress = hideloading;
 luckysheet.luckysheetextendData = luckysheetextendData;
 
 export { luckysheet };
+
+function initSheet(sheet, cellAuthority, authority) {
+  let allowRangeList = [],
+    colhidden = {};
+  let cache = {};
+  if (sheet.config) {
+    delete sheet.config.authority;
+  }
+  sheet.celldata.forEach((item) => {
+    if (!cache[item.c]) cache[item.c] = [];
+    cache[item.c].push(item);
+  });
+  cellAuthority.forEach((item) => {
+    // 将A B C D....转换为0 1 2 3...
+    let col_idx = item.template.col_idx;
+    let col = col_idx.charCodeAt(0) - 65;
+    if (!item.can_read) {
+      colhidden[col] = 0;
+    }
+    if (item.can_write) {
+      // 区域保护
+      allowRangeList.push({
+        hintText: "",
+        sqref: `$${col_idx}$0:$${col_idx}$99999`,
+      });
+    }
+  });
+  // let celldata = []
+  // Object.values(cache).forEach(colItem => {
+  //     celldata = [...celldata,...colItem]
+  // })
+  // sheet.celldata = celldata
+  sheet.config = sheet.config || {};
+  // 处于审批时  所有人都无法修改
+  if (authority) {
+    sheet.config.authority = authority;
+  } else if (cellAuthority.length > 0) {
+    let newAuthority = {
+      sheet: true,
+      hintText: "没有编辑权限!",
+    };
+    if (allowRangeList.length > 0) {
+      // 可编辑内容
+      newAuthority.allowRangeList = allowRangeList;
+    }
+
+    sheet.config.authority = newAuthority;
+  } else {
+    let lastCell = sheet.celldata[sheet.celldata.length - 1];
+    // 第一列禁止编辑但是可以增加
+    let canEditCell = sheet.celldata.filter((item) => {
+      if (item.c == 0) {
+        // 判断是否为空
+        if ((item?.v?.v ?? "") === "" && !item?.v?.ct?.s) return true;
+      }
+    });
+    let canEditRangeList = canEditCell.map((item) => {
+      let row_idx = item.r + 1;
+      return {
+        hintText: "",
+        sqref: `$a$${row_idx}:$a$${row_idx}`,
+      };
+    });
+    // 设置保护区域,lucksheet不能直接设置不允许编辑的区域
+    // 只能禁止整体编辑,设置例外区域
+    sheet.config.authority = {
+      sheet: 1,
+      hintText: "第一列禁止编辑!",
+      allowRangeList: [
+        // 第一列以外都能编辑
+        {
+          hintText: "",
+          sqref: `$b$1:$z$99999`,
+        },
+
+        // 第一列未加载出来的row也允许编辑
+        {
+          hintText: "",
+          sqref: `$a$${lastCell.r + 2}:$a$999999`,
+        },
+        // 第一列没有value的也允许编辑
+        ...canEditRangeList,
+      ],
+    };
+  }
+
+  sheet.config.colhidden = colhidden;
+
+  // 公式计算
+  let calcChain = [];
+  let index = sheet.index;
+  sheet.celldata.forEach((item) => {
+    if (typeof item.v.f !== "undefined") {
+      calcChain.push({
+        r: item.r,
+        c: item.c,
+        index,
+      });
+    }
+  });
+  sheet.calcChain = calcChain;
+}