[{"id":"ff052704-1389-4029-bcdd-73c6cbe9f807","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8e72e33e-b4ab-4fb3-98fc-a0b148134aed","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c90d5fdf-420f-4978-8bf5-c9a2bb4334b3","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"900e6cab-065d-4f1c-844d-efa3c074e270","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"760b37c7-c713-4b24-b9ba-4bfe7d8437a6","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"89c707ed-9841-4e53-96fb-940cc3214804","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"64e2f4a3-2303-4bfa-8a93-6c23ef01de58","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"aa71a884-c5bc-4842-8d6a-873dfd645167","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2605431b-dc80-491d-886e-28981595d277","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"69e79655-e015-4f9a-a230-2a25c988c926","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a570c8e7-07a2-47da-965b-da44fd1fa5cf","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86662220-9b9e-4940-9ced-d22642ea49a8","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24769ecd-2b08-4a85-b318-4f533bbf8393","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a244ead7-a2c6-47a3-ac17-c5dbfa337362","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"04e73782-aa78-4dfe-a4f9-e72ed4c78a11","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4f41e22e-eb51-49e5-aeae-a42dd6bf352c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"790bc77a-9216-48fd-b8f9-fbc374ebb155","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"21c6131f-0f2d-41d1-9284-6ad9ee803c1f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ded3a22-15eb-49b8-a488-e83c2cd872eb","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"33638a1c-7196-42c1-a96d-38b2d9ba8ac4","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a0842a3-20b1-40c3-8e00-cd5941ffdf53","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6e6ac5b1-1501-4e28-89cc-525139488537","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b0576ca2-cb84-4390-9f95-9354ec20eda5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"55fd3bb8-18d4-4edb-9640-ca3a365b798f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dacc7931-6785-4675-be31-80930403cf7b","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"63c577e6-6cf9-497e-94e1-2307f7d3f498","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1b88a165-2563-437e-99bb-ae30bd4b56db","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"adf3817f-7667-4a4c-8a5f-767b0b7e1e3e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"3a083583-1d5f-492b-b450-34b2b5c775b8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5d37413b-3600-4da9-9700-feea54355f59","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a5a73576-16ea-4cbc-925c-ef547389eaa5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1126cfab-6210-4e28-bee0-02c113fb7a0c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"09a66339-64c0-415c-b142-0691587a8e4a","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"0baaf91d-84f6-404c-a487-735226b6d5b6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5b7aeac5-c755-426a-95c7-1ae8e547179a","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"57c94653-8893-403b-a5b1-0d1e33a0bd0f","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