|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
各位大神,请帮忙简化一下,或者提供新思路
产品行会复制,复制后重新编号,我的代码如下,感觉就是VBA
var ids = []; //商品序号
var exist = 0;
const idRange = ActiveSheet.Range("B1").End(xlDown);
const rowsCount = idRange.Row;
if(Selection.Cells(1,1).Column != 1) return;
if(Selection.Cells(1,1).Row <= rowsCount) {
var idBefore = Selection.Cells(1,1).Value2;
if(idBefore != undefined) { //如果有编号不为空,判断重不重复
for(let i = 0; i < rowsCount; i ++) {
ids[i] = ActiveSheet.Cells(i+1,1).Value2;
if(ActiveSheet.Cells(i+1,1).Value2 == idBefore) {
exist ++;
}
}
ids[0] = 0;
} else { //否则,exist=0,把当前数组元素赋值0(当前为空格)
for(let i = 0; i < rowsCount; i ++) {
ids[i] = ActiveSheet.Cells(i+1,1).Value2;
}
ids[0] = 0;
ids[Selection.Cells(1,1).Row-1] = 0;
}
if(exist == 0 || exist > 1) {
var id = Math.max(...ids); //展开数组,取最大值
Selection.Cells(1,1).Value2 = id + 1;
}
}
|
|