0
我正在嘗試爲每個表單提交添加一個帶兩位數年份前綴的自動遞增唯一ID。我已經複製了Serge Insas的回答in this post的代碼,並試圖實現請求的兩位數年份here。表單提交時自動遞增
function OnForm(){
var sh = SpreadsheetApp.getActiveSheet()
var startcell = sh.getRange('A2').getValue();
if(! startcell){sh.getRange('A2').setValue('PQOT-14-0001');return}; // this is only to handle initial situation when A1 is empty.
var colValues = sh.getRange('A2:A').getValues();// get all the values in column A in an array
var year = Utilities.formatDate(new Date, "PST", "yy")
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,'');// remove the letters from the string to convert to number
if(Number(vv)>max){max=vv};// get the highest numeric value in th column, no matter what happens in the column... this runs at array level so it is very fast
}
max++ ; // increment to be 1 above max value
sh.getRange(sh.getLastRow(), 1).setValue(Utilities.formatString('PQOT-'+year+'-%04d',max));// and write it back to sheet's last row.
}
上述代碼返回「PQOT-14-000X」。我需要對它進行編輯以去除多餘的文字和年份,以便它能夠正確增加。有什麼建議麼?