Javascript 如何使用GAS创建带前导零的序列编号?

Javascript 如何使用GAS创建带前导零的序列编号?,javascript,google-apps-script,Javascript,Google Apps Script,大家好,GAS/Javascript专家 我发现自动生成顺序ID的脚本从变量中定义的数字开始: 但是,我需要添加3个前导零,以确保位数将标准化为总共6位,包括生成的ID。例如000100000101000102000103。。。。 如果数据行超过9999999,则子序列数字应自动添加为7位,以此类推 任何人都可以帮助修改脚本以实现上述目标,请?Tqvm 当前脚本如下所示:- function addAutoNumber() { var sheet = SpreadsheetApp.getA

大家好,GAS/Javascript专家

我发现自动生成顺序ID的脚本从变量中定义的数字开始: 但是,我需要添加3个前导零,以确保位数将标准化为总共6位,包括生成的ID。例如000100000101000102000103。。。。 如果数据行超过9999999,则子序列数字应自动添加为7位,以此类推

任何人都可以帮助修改脚本以实现上述目标,请?Tqvm

当前脚本如下所示:-


function addAutoNumber() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Form responses 1");
  // Get the last row that has content.
  var LastRow = sheet.getLastRow();
  // Set the first Auto Number
  var AutoNumberStart=000100;  
  
  //---- First run ------------
  
  //Check if the first column is Timestamp
  if (sheet.getRange(1, 1).getValue() == "Timestamp") {
    // insert a column to the left and the text "Auto Number" in the first row
    sheet.insertColumnBefore(1);
    sheet.getRange(1, 1).setValue("Auto Number");
    // Fix for (probably) a bug that formats the new column 
    // with the same format of the column used to insert it,
    // in this case the column gets the date format like the Timestamp column.
    // So we set the format to number
    sheet.getRange("A2:A").setNumberFormat(0); 
    // check if there are already form responses and add numbers for them
    if (LastRow>1) {
      for(var ii=2; ii <= LastRow; ii++) {
        sheet.getRange(ii, 1).setValue(AutoNumberStart);
        AutoNumberStart++;
      }
    }
  }
  
  // ---- Add new Auto Number ----------
  
  // Check if there is a Number in the AutoNumber column 
  // for the last Form Submission
  if (sheet.getRange(LastRow, 1).isBlank()) {
  // Check if it is the first Form submission and set the AutoNumberStart
    if (LastRow == 2) {
      sheet.getRange(LastRow, 1).setValue(AutoNumberStart);
    } else {
      // Get the Last AutoNumber from the previous row
      var LastAutoNumber = sheet.getRange(LastRow-1, 1).getValue();
      // Set the next AutoNumber
      sheet.getRange(LastRow, 1).setValue(LastAutoNumber+1);
    }
  }
}

函数addAutoNumber(){
var sheet=SpreadsheetApp.getActive().getSheetByName(“表单响应1”);
//获取包含内容的最后一行。
var LastRow=sheet.getLastRow();
//设置第一个自动编号
var AutoNumberStart=000100;
//----首轮------------
//检查第一列是否为Timestamp
if(sheet.getRange(1,1).getValue()=“Timestamp”){
//在左侧插入一列,并在第一行中插入文本“自动编号”
第1页前插入列;
sheet.getRange(1,1).setValue(“自动编号”);
//修复(可能)格式化新列的错误
//使用用于插入的列的相同格式,
//在本例中,该列获取的日期格式与时间戳列类似。
//所以我们将格式设置为number
sheet.getRange(“A2:A”).setNumberFormat(0);
//检查是否已经有表单响应,并为其添加编号
如果(最后一行>1){
对于(var ii=2;ii
一定是

sheet.getRange("A2:A").setNumberFormat("000000"); 
对于较高条目上的自动更新,您必须添加一个侦听器,以便在该列发生更改时对其进行更新,或者将numberFormat值更新为新条目上的最大数字长度

可在支票上添加新行时执行此操作:

const highestLen = (LastAutoNumber+1).toFixed(0).length;
if (highestLen > sheet.getRange("A2:A").getNumberFormat().length) {
  // depending on the length, this returns "000000", "0000000" and so on.
  const format = new Array(highestLen).fill("0").join("");
  sheet.getRange("A2:A").setNumberFormat(format);
}
一定是

sheet.getRange("A2:A").setNumberFormat("000000"); 
对于较高条目上的自动更新,您必须添加一个侦听器,以便在该列发生更改时对其进行更新,或者将numberFormat值更新为新条目上的最大数字长度

可在支票上添加新行时执行此操作:

const highestLen = (LastAutoNumber+1).toFixed(0).length;
if (highestLen > sheet.getRange("A2:A").getNumberFormat().length) {
  // depending on the length, this returns "000000", "0000000" and so on.
  const format = new Array(highestLen).fill("0").join("");
  sheet.getRange("A2:A").setNumberFormat(format);
}

Tqvm,@Christopher!。它像一个符咒一样工作!只是我把你代码的最后一行从
“A2:a”
改为
“a”+LastRow+:a“
,因为我不想更改前面的数字。;)Tqvm,@Christopher!。它像一个符咒只是我把你代码的最后一行从
“A2:a”
改为
“a”+LastRow+“:A”
,因为我不想更改以前的位数。;)