簡體   English   中英

使用 Apps 腳本在 Google 表格中自動編號

[英]Autonumbering in Google Sheets Using Apps Script

我目前正在與 Google Forms 和 Appsheet 合作,為我的公司創建一個投訴跟蹤應用程序。 此項目要求通過 Google 表單提交的每個投訴都有一個 ComplaintID。 此 ComplaintID 的格式是當年,然后是投訴編號(例如,2020-001 將是 2020 年收到的第一個投訴)。 根據這個項目的規范,前導零是必需的。 時間戳捕獲年份,因此所有必要的數據都存在。

這是我在 howtogoogleapps.com 上找到的一個腳本,它似乎幾乎可以工作:

 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="2020-001"; //---- 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 "ComplaintID" in the first row sheet.insertColumnBefore(1); sheet.getRange(1, 1).setValue("ComplaintID"); // 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="2020-"+ (parseInt(AutoNumberStart.substr(5,3)+1)); } } } // ---- 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("2020-"+ (parseInt(LastAutoNumber.substr(5,3)+1))); } } }

但是,返回的是2020-001、2020-11、2020-111、2020-1111、2020-1111等,不完全是我需要的。 如果你能幫我解決這個問題,我將永遠感激不盡!

在這種情況下,我認為parseInt需要用於AutoNumberStart.substr(5, 3) 因為AutoNumberStart.substr(5, 3)是字符串類型。 因此,當使用parseInt(AutoNumberStart.substr(5,3)+1)時,會將1添加到AutoNumberStart.substr(5,3)作為字符串。 我認為這就是你的問題的原因。 所以為了避免這種情況,我想提出以下修改。

從:

AutoNumberStart="2020-"+ (parseInt(AutoNumberStart.substr(5,3)+1));

到:

AutoNumberStart = "2020-"+ ("00" + (parseInt(AutoNumberStart.substr(5, 3), 10) + 1)).slice(-3);
  • AutoNumberStart2020-001時,在本次修改中, parseInt(AutoNumberStart.substr(5, 3), 10)1 of number。 並且("00" + (parseInt(AutoNumberStart.substr(5, 3), 10) + 1)).slice(-3)002

測試:

 const AutoNumberStart1 = "2020-001"; const res1 = "2020-"+ ("00" + (parseInt(AutoNumberStart1.substr(5, 3), 10) + 1)).slice(-3); console.log(res1) const AutoNumberStart2 = "2020-099"; const res2 = "2020-"+ ("00" + (parseInt(AutoNumberStart2.substr(5, 3), 10) + 1)).slice(-3); console.log(res2)

筆記:

  • 當你要使用超過1000(超過4位)的數字時,下面的修改如何? 在這種情況下,返回2020-1000

     const AutoNumberStart = "2020-999"; const value = (parseInt(AutoNumberStart.substr(5, 3), 10) + 1).toString(); const res = "2020-"+ ("00" + value).slice(-(value.length > 3? value.length: 3)); console.log(res)

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM