[{"id":"b66f8b1e-cc14-4e89-9679-abd5687d283d","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"569bb90a-ea68-46c6-96f1-ab151c120714","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5eb52f08-2d1a-4362-9ffc-4871bdc10f3f","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"341dd607-b97d-4d70-bde2-53acda6b6c95","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"05349273-414f-4208-9ea2-c4fc8f4ea2cb","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86089f76-b778-4d52-821e-6f27de3df613","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b81e4fd6-1fc5-43a0-a258-b6e16a5cbec6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8aa8ce31-43e4-438e-951f-241608435260","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"37343f41-6ec2-4c7e-b21d-2cc18d5ce1e0","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5915e52f-64f8-4146-b8bd-81bead6324a3","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"836ba889-af9e-460d-a4cc-c24d922795f2","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4ae14b06-bb68-4394-a210-a46b8f028346","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d8f42066-e9dc-4411-bdcf-43b1a203370c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ef86f16-b1a0-49f7-9592-612b9be02b25","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b29c7775-a9a4-451e-a1b5-01d19ed5ca5e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"e56f3989-8f81-46af-90fa-a4813eeb976f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2526c963-f170-45a8-923e-91b0712a9810","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24575cf0-501a-44f9-8426-c40f8f4b5552","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1a6f8d7f-acd6-42be-8c4f-f464c6218381","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"82b176fd-5cab-498c-909e-8fa7d29c38d8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1708b3a9-4f37-44a8-8f0e-f9a2d2e5d940","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c9ac246e-29fb-4bc4-8231-8439795bb590","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"94f2a5c3-2539-436a-af75-23fbbd1a3957","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d99594f4-2d40-4df4-9419-ba2ca6aa3f7f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"fd66e72d-0f10-4f57-9807-6db26290ab2e","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8f344863-503d-4bc3-a594-3815e7d55f5c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"9be82601-de9d-4c18-948a-23ab6f4dd431","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6495f3c0-b463-47e0-b08a-ca949672211e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b4bede08-3f08-4839-ba4a-abc7ac195bde","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4afcdfc0-3ff5-4f2b-a223-f8fc042a5bbe","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"422062c4-fa40-4771-a86f-008efe6d86e5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d87e8013-86a7-4840-8d25-6f62e14eb4ac","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a6acedfd-4043-4c64-a5d1-aec3326df9e7","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dbdc0b24-06c4-48b9-8d6c-7455119dc773","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a3856849-954a-4cfc-96a6-382e530d3638","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a39306b-ffa5-433d-80a3-28e41f929b72","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]}]
        
(Showing Draft Content)

实现单元格编辑可控

问题:如何用自定义单元格实现编辑可控


背景

在某些情况下,用户需要通过按钮实现某个单元格是否可编辑(每一个单元格都有对应的一个按钮来控制这个单元格是否可编辑)。

当用户点击按钮后,单元格变为可编辑状态,按钮变成另一个样式作为提示。之后用户在编辑完成之后再次点击按钮,单元格变为不可编辑状态。

实现思路

对于这样的需求,首先要知道编辑状态的控制可以通过单元格更改单元格锁定状态结合表单保护来实现。

那么这个需求就被分解成了如何做一个带有按钮的单元格,并将上述更改锁定状态的控制集成到单元格本身的生命周期中,这样的需求就非常符合自定义单元格的功能。

所以,首先定义一个自定义单元格的对象:

function ImageCellType() {
    this.iconFlag = true;
}

这里设置了一个iconFlag的属性,起一个标记的作用,用于判断按钮图片的切换。

接下来,指定自定义单元格的继承关系,将他的原型设置为GC.Spread.Sheets.CellTypes.Text:

ImageCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();

下面我们需要重写里面的一些方法,paint是首当其冲要重写的。paint本身控制着单元格绘制的相关逻辑,并且在每次重绘的时候都会被调用。所以我们需要在paint中加入绘制按钮的相关逻辑,paint中会提供canvas的底层对象,通过底层canvas对象我们可以自由的在单元格上绘制各种内容:文字,图片,图形等。

ImageCellType.prototype.paint = function (ctx, value, x, y, width, height, style, context) {
  spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, width - 20, height, style, context]);
  ctx.save();
  ctx.beginPath();
  ctx.moveTo(x, y);
  if (this.iconFlag) {
    ctx.drawImage(rightImg, x + width - 20, y, 20, 20);
  } else {
    ctx.drawImage(errorImg, x + width - 20, y, 20, 20);
  }
  ctx.restore();
  return;
};

这里主要是根据iconFlag来绘制不同的图片的方法逻辑。

这里的两张图片必须在初始化的时候提前加载好,如果在paint方法中加载会造成死循环(因为如果是paint方法中加载图片,图片本身是一个异步加载,加载好之后又会触发画布的重绘,那么会继续调用paint方法,如此往复会造成死循环,所以图片需要在外部提前加载好缓存起来)。

外部加载图片的代码如下:

rightImg = new Image(); //指定图片的URL
rightImg.src = "img/right.ico";
errorImg = new Image(); //指定图片的URL
errorImg.src = "img/error.ico";
rightImg.onload = function () {
  sheet.repaint();
};
errorImg.onload = function () {
  sheet.repaint();
};

之后要重写getHitInfo方法,getHitInfo方法用于获取点击之后的位置信息。因为需要判断单元格的点击位置,从而获取鼠标单击的位置是否是在按钮上,所以需要重新改写该方法:

ImageCellType.prototype.getHitInfo = function (
  x,
  y,
  cellStyle,
  cellRect,
  context
) {
  var info = {
    x: x,
    y: y,
    row: context.row,
    col: context.col,
    cellStyle: cellStyle,
    cellRect: cellRect,
    sheetArea: context.sheetArea,
  };

  if (
    cellRect.x + cellRect.width - 20 < x &&
    x < cellRect.x + cellRect.width &&
    cellRect.y < y &&
    y < cellRect.y + 20
  ) {
    info.isReservedLocation = true;
  }

  return info;
};

接下来,可以根据上述信息重写processMouseMove方法,在方法中通过getHitInfo方法返回的信息,更改鼠标指针的状态(当鼠标悬浮在按钮之上时,可以调整指针为手形)。

ImageCellType.prototype.processMouseMove = function (hitInfo) {
  var sheet = hitInfo.sheet;
  var div = sheet.getParent().getHost();
  var canvasId = div.id + "vp_vp";
  var canvas = $("#" + canvasId)[0];
  if (sheet && hitInfo.isReservedLocation) {
    canvas.style.cursor = "pointer";
    return true;
  } else {
    canvas.style.cursor = "default";
  }
  return false;
};

之后,重写processMouseUp方法,processMouseUp方法在鼠标点击之后向上弹时触发。在方法中我们可以根据之前的判断,定位鼠标点击的是否是按钮单元格,如果是按钮单元格就需要调整该单元格的locked状态,用于该单元格只读还是可编辑的控制。

ImageCellType.prototype.processMouseUp = function (hitInfo) {
  var sheet = hitInfo.sheet;
  var that = this;
  if (sheet && hitInfo.isReservedLocation) {
    if (that.iconFlag) {
      that.iconFlag = false;
    } else {
      that.iconFlag = true;
    }
    setTimeout(function () {
      var cell = hitInfo.sheet.getCell(hitInfo.row, hitInfo.col);
      cell.locked(that.iconFlag);
    }, 0);
    sheet.repaint();
    return true;
  }
  return false;
};

这样,我们就完成了自定义单元格的设置,接下来就是给单元格设置celltype为ImageCellType来测试一下实际的效果。

sheet.setCellType(0, 0, new ImageCellType());
sheet.options.isProtected = true;
sheet.setValue(0, 0, "葡萄城");

这里额外设置了sheet.options.isProtected = true; 即为表单保护,因为单元格锁定需要配合表单保护才能控制单元格的只读状态。实际效果如下图所示:

image

默认加载后单元格不可编辑,按钮为“√”。点击按钮后单元格可编辑,按钮变为“×”。

image

作者: GrapeCity China | 审核:AlexZ | 更新时间:2023.09.26