【发布时间】:2019-05-28 22:27:27
【问题描述】:
我在编码方面非常糟糕,但一直在尝试拼凑一个脚本来生成一个静态唯一 ID,以便放置在 Google 工作表中。
我希望在 D 列中输入任何数据时,A 列中的等效行会生成一个唯一 ID,该 ID 从上一个 ID 递增。但是,唯一 ID 必须是静态的,因为它不以任何方式链接到行号。唯一 ID 还需要按照 D 列输入数据的顺序递增,不一定按行号顺序输入(即 ID2 可能在第 16 行,ID3 在第 6 行)。
我已经找到并改编了两个脚本,我觉得它们几乎完成了需要完成的工作,但已经达到了我对完成的理解的极限。谁能帮忙解释一下我将如何调整 OnEdit 函数以包含来自 OnForm 函数的 setValue 命令?
function OnForm(){
var sh = SpreadsheetApp.getActiveSheet()
var startcell = sh.getRange('A1').getValue();
if(! startcell){sh.getRange('A1').setValue(1);return};
var colValues = sh.getRange('A1:A').getValues();
var max=0;// define the max variable to a minimal value
for(var r in colValues){ // iterate the array
var vv=colValues[r][0].toString().replace(/[^0-9]/g,'');
if(Number(vv)>max){max=vv};
}
max++ ; // increment to be 1 above max value
sh.getRange(sh.getLastRow()+1, 1).setValue(Utilities.formatString('IG%05d',max));
}
function onEdit(e) {
var ss = e.source.getActiveSheet();
var watchedCols = [4]
if (watchedCols.indexOf(e.range.columnStart) === -1) return;
ss.getRange(e.range.rowStart, 2)
.setValue('unique ID here')
}
【问题讨论】:
标签: google-apps-script google-sheets