自动递增作业参考 [英] Auto incrementing Job Reference

查看:82
本文介绍了自动递增作业参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写代码方面很新颖,理解有限,请温和!
我一直在尝试扩展这个问题



我的成功有限,现在卡住了,我很希望有人会友好地指点我在正确的方向或告诉我我做错了什么。

因此,情景:需要有一个自动递增的工作参考预订上网本维修的IT部门我工作。此预订是通过Google表单完成的,我可以在链接中获得完美的代码,但是!我希望有一个简单的数字 - 1,2,3,4,5等等。理想情况下,作业参考将显示为JR0001,JR0002等。



所以,我尝试了代码!



用户自己提交的代码完美地工作。函数onFormSubmit(e){
//获取活动工作表
var sheet = SpreadsheetApp.getActiveSheet() ;
//获取活动行
var row = sheet.getActiveCell()。getRowIndex();
//获取下一个ID值。注意:此单元格应设置为:= MAX(A:A)+1
var id = sheet.getRange(P1)。getValue();
//检查ID列是否为空
if(sheet.getRange(row,1).getValue()==){
//设置新的ID值
sheet.getRange(row,1).setValue(id);
}
}

我尝试的第一件事是简单地添加另一个变量并将其添加到.setValue中。
$ b $ pre $ 函数onFormSubmit(e){
//获取活动工作表
var sheet = SpreadsheetApp.getActiveSheet();
//获取活动行
var row = sheet.getActiveCell()。getRowIndex();
//获取下一个ID值。注意:此单元格应设置为:= MAX(A:A)+1
var id = sheet.getRange(X1)。getValue();
//设置作业参考
var jobref =JR;
//检查ID列是否为空
if(sheet.getRange(row,1).getValue()==){
//设置新的ID值
sheet.getRange(row,1).setValue(jobref + id);


$ / code $ / pre
$ b $ p

这个工作得到JR1而不是1但自动递增停止工作,因此对于提交的每个表单,我仍然有JR1 - 不是真正的自动递增!

然后我尝试设置另一个.getValue表格作为作业参考

 函数onFormSubmit(e){
//获取活动工作表
var sheet = SpreadsheetApp.getActiveSheet();
//获取活动行
var row = sheet.getActiveCell()。getRowIndex();
//获取下一个ID值。注意:此单元格应设置为:= MAX(A:A)+1
var id = sheet.getRange(X1)。getValue();
//设置作业参考
var jobref = sheet.getRange(X2)。getValue();
//检查ID列是否为空
if(sheet.getRange(row,1).getValue()==){
//设置新的ID值
sheet.getRange(row,1).setValue(jobref + id);


$ / code>

同样的结果 - 一个非递增的JR1 p>

然后我尝试连接工作递增数与工作参考单元格,然后在脚本中调用它。

 函数onFormSubmit(e){
//获取活动工作表
var sheet = SpreadsheetApp.getActiveSheet();
//设置作业参考
var jobref = sheet.getRange(X2)。getValue();
//获取活动行
var row = sheet.getActiveCell()。getRowIndex();
//获取下一个ID值。注意:此单元格应设置为:= MAX(A:A)+1
var id = sheet.getRange(X1)。getValue();
//检查ID列是否为空
if(sheet.getRange(row,1).getValue()==){
//设置新的ID值
sheet.getRange(row,1).setValue(jobref);




$ b $ p
$ b

相同的结果值不会增加



我不明白为什么如果我添加其他变量并且不知道如何将前导零添加到递增数字,数字停止递增。我感觉到我正试图让事情复杂化!

所以总结一下,有一种获得6个字符长的自动递增引用的方法 - in这种情况下,第一次形式JR0001第二次提交JR0002等。



我真的很喜欢一些指针,如果可能的话,我会错误的,我想学习,但我是显然缺少一些关键原则。

解决方案

这是一个使用全新实用工具.formatString()表示GAS团队几天前刚刚添加的; - )

 函数OnForm (){
var sh = SpreadsheetApp.getActiveSheet()
var startcell = sh.getRange('A1')。getValue();
if(!startcell){sh.getRange('A1')。setValue(1); return}; //这只是在A1空时处理初始情况。
var colValues = sh.getRange('A1:A')。getValues(); //获得数组中列A中的所有值
var max = 0; //定义最大变量为(var var in colValues){//迭代数组
var vv = colValues [r] [0] .toString()。replace(/ [^ 0-9] / g ,''); //删除字符串中的字母以转换为数字
if(Number(vv)> max){max = vv}; //获得列中最高的数字值,无论在列中发生了什么...这在数组级别运行,所以它非常快
}
max ++; //增加到最大值1以上
sh.getRange(sh.getLastRow()+ 1,1).setValue(Utilities.formatString('JR%06d',max)); //并将其写回到工作表的最后一行。
}


I am very new at writing code and have a limited understanding so please be gentle! I have been trying to expand on the code made on this question.

I have had limited success and am now stuck, I was hoping someone would be kind enough to point me in the right direction or tell me what I am doing wrong.

So, the scenario: A need to have an auto-incrementing "Job Reference" for booking in netbook repairs to the IT dept I work for. This booking is made via a Google Form and I can get the code in the link to work perfectly but! I was hoping to have a little more than a simple count - 1,2,3,4,5 and so on. Ideally the job ref would be displayed as JR0001, JR0002 and so on.

So, my attempt at code!

The code which user: oneself submitted which works perfectly.

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("P1").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
    // Set new ID value
    sheet.getRange(row, 1).setValue(id);
  }
}

The first thing I tried was to simply add another variable and add it to the .setValue

function onFormSubmit(e) {
   // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("X1").getValue();
  // Set Job Reference
  var jobref = "JR";
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
  // Set new ID value
    sheet.getRange(row, 1).setValue(jobref+id);
  }
}

This worked as far as getting "JR1" instead of 1 but the auto-increment stopped working so for every form submitted I still had "JR1" - not really auto-increment!

I then tried setting up another .getValue from the sheet as the Job Ref

function onFormSubmit(e) {
   // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("X1").getValue();
  // Set Job Reference
  var jobref = sheet.getRange("X2").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
  // Set new ID value
    sheet.getRange(row, 1).setValue(jobref+id);
  }
}

Same result - a non incrementing "JR1"

I then tried concatenating the working incrementing number with my job ref cell and then calling that in the script.

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Set Job Reference
  var jobref = sheet.getRange("X2").getValue();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("X1").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
  // Set new ID value
    sheet.getRange(row, 1).setValue(jobref);
  }
}

Same result the value doesn't increment

I don't understand why the number stops incrementing if I add other variables and don't understand how to add the leading zeros to the incrementing number. I get the feeling that I am trying to over complicate things!

So to sum up is there a way of getting an auto-incrementing ref that is 6 characters long - in this scenario first form JR0001 second submit JR0002 and so on.

I would really like some pointers on where I am going wrong if possible, I do want to learn but I am obviously missing some key principles.

解决方案

here is a working solution that uses the "brand new" Utilities.formatString() that the GAS team just added a few days ago ;-)

function OnForm(){
var sh = SpreadsheetApp.getActiveSheet()
var startcell = sh.getRange('A1').getValue();
if(! startcell){sh.getRange('A1').setValue(1);return}; // this is only to handle initial situation when A1 is empty.
var colValues = sh.getRange('A1:A').getValues();// get all the values in column A in an array
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, 1).setValue(Utilities.formatString('JR%06d',max));// and write it back to sheet's last row.
}

这篇关于自动递增作业参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆