独特的ID计数器

时间:2013-03-13 21:38:52

标签: javascript google-apps-script google-sheets google-apps

关于我正在开发的Google Apps脚本网络应用,我在过去几天发布了几个问题。到目前为止,Serge在这里一直非常有帮助。该脚本上的帖子为here

我希望在该脚本中添加一列来生成uniqueID计数器,以便在提交新表单时,计数器将看到以前的唯一ID并增加1.想法将是带有标题行的空白电子表格。脚本在表单提交上运行,参见A2为空白并插入1.下一个提交的表格将在A2中看到2添加1并在A3中添加3,依此类推。

现在我正在研究计数器将1添加到单元格中的数字。我知道,当我开发这篇文章时,我需要抓住最后一行,然后找到单元格值并添加,但是因为我是GAS的新手,我正在教我自己,所以我想要慢慢地建立嗯,你明白了。

这是一个基本的开始,如果这是一个正确的方向,我想得到一些反馈..

function counter() {
   //Grabs the range and pinpoint cell
   var ssRange = SpreadsheetApp.getActiveSheet().getRange(1,1,1,1);
   //Grab the cell value of A1
   var value = ssRange.getValue();
   //if statement to check value in A1 and if it is null, undefined, false, 0, NAN
if (!value) {
  //if the cell is null, undefined, false, 0, NAN sets value to 1
   var setCell = ssRange.setValue(1);
}
  //if not updates the cell by getting the value and adding 1 to it
 else {
   var updateCell = ssRange.setValue(value + 1);  
}
}

编辑(更新代码使用.setProperty& .getProperty

的Phil Bozak提示

如果您提到的手动更改插入电子表格的MYID,我仍在测试此代码的处理方式。我需要确保MYID保持独特。不得不考虑..这是代码..感谢任何反馈!

function counter2() {
  //Get the spreadsheet, range, and value of first cell in range
  var sSheet = SpreadsheetApp.getActiveSheet();
  var ssRange = sSheet.getRange(1,1,1,1);
  var ssValue = ssRange.getValue();
  var setUniqueID = ScriptProperties.setProperty("MYID",1);
  var getUniqueID = ScriptProperties.getProperty("MYID");

  //will set value of first cell to 1 if null, undefined, false, 0, NAN etc.. (runs in if //statement)
  var setOne = ssRange.setValue(getUniqueID);  

  if (!ssValue) {
  setOne;
  }
  else {

  //This goes back and get the range, lastrow of range, and value of first cell in           range of last row & adds 1 to that value

  var setLast = sSheet.getRange(lastRow+1,1,1,1).setValue(getUniqueID + 1); 
    setLast;
  }
  }

3 个答案:

答案 0 :(得分:5)

确保获得一个始终是最后一个计数值(最高值)作为列中计数器的另一种方法是:(代码中的注释)

当你说A列中的值可以手动修改时,我有了这个想法......在这种情况下,它是避免可能重复的唯一方法(它不会阻止使用未使用的值...(但是这个)如有必要,也可以实施)

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
    if(Number(colValues[r][0]>max)){max=colValues[r][0]};// 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(max);// and write it back to sheet's last row.
}

答案 1 :(得分:0)

好的..我已经更新了我的代码abit并遇到了一个我希望有人可以提供帮助的问题。该脚本将看到电子表格为空白并在A1中插入1。在下一次运行时,它将看到1在A1中并且添加1并在A2中插入2。从那时起,它只是一遍又一遍地在每行中放置2个..这是代码..不知道为什么它这样做而不是每个都增加1并增加计数。

function counter2() {
  var sSheet = SpreadsheetApp.getActiveSheet();
  var ssRange = sSheet.getRange(1,1,1,1);
  var ssValue = ssRange.getValue();
  var setOne = ssRange.setValue(1);  
  var lastRow = sSheet.getLastRow();  
  var setLast = sSheet.getRange(lastRow+1,1,1,1).setValue(ssValue + 1);

  if (!ssValue) {
  setOne;
  }
  else {
  setLast;
  }
  }

编辑1

更新的代码...空白电子表格上的第一次运行填写A1 = 1和A2 = 2.它应该只放入单元格A1中的1,然后再增加。第二次运行时它可以正常工作。想法?我错过了什么? function counter2(){       //获取范围内第一个单元格的电子表格,范围和值       var sSheet = SpreadsheetApp.getActiveSheet();       var ssRange = sSheet.getRange(1,1,1,1);       var ssValue = ssRange.getValue();

  //will set value of first cell to 1 if null, undefined, false, 0, NAN etc.. (runs in if statement)
  var setOne = ssRange.setValue(1);

  //This goes back and get the range, lastrow of range, and value of first cell in range of last row & adds 1 to that value
  var lastRow = sSheet.getLastRow();
  var startValue = sSheet.getRange(lastRow,1,1,1).getValue();
  var setLast = sSheet.getRange(lastRow+1,1,1,1).setValue(startValue + 1);      

  if (!ssValue) {
  setOne;
  }
  else {
  setLast;
  }
  }

编辑2 - 修正

这是现在看起来工作正常的更新代码。我移动了变量" lastRow,startValue,setLast"进入第二个陈述的else语句。为什么会这么重要?是因为它在变量到达时运行变量并通过包装在if / else {}括号中,只有在运行该语句时才会执行?

function counter2() {
  //Get the spreadsheet, range, and value of first cell in range
  var sSheet = SpreadsheetApp.getActiveSheet();
  var ssRange = sSheet.getRange(1,1,1,1);
  var ssValue = ssRange.getValue();

  //will set value of first cell to 1 if null, undefined, false, 0, NAN etc.. (runs in if statement)
  var setOne = ssRange.setValue(1);  

  if (!ssValue) {
  setOne;
  }
  else {

  //This goes back and get the range, lastrow of range, and value of first cell in range of last row & adds 1 to that value
  var lastRow = sSheet.getLastRow();
  var startValue = sSheet.getRange(lastRow,1,1,1).getValue();
  var setLast = sSheet.getRange(lastRow+1,1,1,1).setValue(startValue + 1); 
    setLast;
  }
  }

答案 2 :(得分:0)

您也可以使用ScriptProperties来管理唯一ID。它更可靠,更容易使用。

  

可靠性说明:假设您的电子表格出现故障,并且您的ID为[1,2,3,5,4]。然后,您的脚本将插入5作为下一个ID。 ScriptProperties可以更好地跟踪最大数量。

>样本用法

var uid = ScriptProperties.getProperty("last_unique_id")+1;
ScriptProperties.setProperty("last_unique_id",uid); //the current UID is now the "last UID"
//do something with the unique_id

当然,您最初必须设置该属性。我建议您使用从菜单运行的一个小功能来完成此操作。

function setUIDOnce() {
  ScriptProperties.setProperty("last_unique_id",1);
}
相关问题