一、单元格输入单引号后无法删除
问题描述
Excel表格可以在内容前输入英文状态的单引号来实现一些格式转换,Luckysheet也同步了这一功能;但在使用中却发现了一个问题:单元格一旦使用了英文状态的单引号后,不论你手动删除或者修改,还是使用右键的清空内容,都无法去除单引号,甚至清空内容后双击单元格,会显示为'null
,如图:
解决方案
查遍了Luckysheet的官方文档和讨论组,并没有找到相关的问题解决方案;后来通过对比修改前后的表格数据结果,发现其中多了一个参数qp
值为1;查看其源码,初步判断该值就是用来标识单引号的。
因此想要得到我们想要的效果,就需要去抓取到数据发生改变的时刻手动去修改qp
的值,代码如下:
hook: {
cellUpdateBefore: function (r, c, value, isRefresh) {
// 当用户修改单元格里的值,去掉单引号时
// eslint-disable-next-line no-undef
const data = luckysheet.toJson().data[0].data
if (data[r][c] && data[r][c].qp && data[r][c].qp == 1 && value.indexOf("'") < 0) {
// eslint-disable-next-line no-undef
luckysheet.setCellFormat(r, c, 'qp', 0)
}
},
updated: function (operate) {
// 当用户清空单元格里的数据时
if (operate.type === 'datachange') {
const row1 = operate.range[0].row[0]
const row2 = operate.range[0].row[1]
const col1 = operate.range[0].column[0]
const col2 = operate.range[0].column[1]
for (let r = row1; r <= row2; r++) {
for (let c = col1; c <= col2; c++) {
const newValue = operate.curdata[r][c]
if (newValue && !newValue.v && !newValue.m && newValue.qp && newValue.qp == 1) {
// eslint-disable-next-line no-undef
luckysheet.setCellFormat(r, c, 'qp', 0)
}
}
}
}
}
}