[英]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);
AutoNumberStart
为2020-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.