Trying to to Study Google App script: Random Numbers.

that took forever to figure out: how to generate a random number

CUT BELOW

// creates the definition for getRndNumber
function getRndNumber(min, max){
//  uses the math.floor class to create a random number. 
  return Math.floor(Math.random() * (max – min + 1) + min);
}

function RndNumGen(){
  var rndNum = getRndNumber(1, 99999);
  return rndNum;
}
// tests the RndNumGen, use View>Log to see if it works. 
// you may need to make more of these scripts (copying everything and making a 01, 02, 03…)
// so that you can make all the number of random number generating scripts
// because if you call on the same script it will copy so if you have 5 random numbers then you need
// 5 copies of this script, all named differently
function test(){
  Logger.log(RndNumGen());
}

END

I got do the The 5-Min Quickstarts already, tried the Manage Google Form Responses.., but didnt get it to work as much as I’d like.

I’ve tried to study: References (top Purple Heading) > Spreadsheet

Note that I’m not going through this linearly, instead jumping back and forth. Like I dont know basics like Script Properties or what the “.” means.

I’m trying to Study Writing Data Part

the problem is that the Codeacademy Training module for arrays featured in the links is gone. Still checking if this has anything to do with it https://www.youtube.com/watch?v=yu6zQNU5Wv4

This is Ashton’s Fei’s code I’m trying to figure out.

function add_uid2(){
  var uid = create_id();
//  this is the code that determines where the Random Number Appears. 
//  ws.getRange sets the location, but we are declaring what the rowNUm and colNum to be data.length
//  but what is data.length? I know data = ss.getSheet. Its an integer. but what integer. 
  var ss = SpreadsheetApp.getActive();
  var ws = ss.getSheetByName(“Auto ID”);
  var data = ws.getDataRange().getValues();
  var rowNum = data.length;
  var colNum = data[0].length;
//  the script below is what allows the random number to appear where it appears. 
  ws.getRange(rowNum,colNum).setValue(uid);
 
  auto_fill(ws,rowNum,colNum);
}

//  One of the other things I need to study.
//  I guess this makes the create id uid 
function create_id(){
  var script_properties = PropertiesService.getScriptProperties();
  var keys = script_properties.getKeys();
 
  var uid = new_id();
 
  while (keys.indexOf(uid) >= 0) {
    uid = new_id();
  }
 
  script_properties.setProperty(uid, new Date());
 
  return uid;
}

// creates the random number generator. by making getRndNumber generate a number between min and max. 
function getRndNumber(min, max){
  return Math.floor(Math.random() * (max – min + 1) + min);
}
// adds the prefix to the random number generator
function new_id(){
  var rndNum = getRndNumber(1, 99999);
  var id = (‘0’ + rndNum).slice();
  id = ‘0’ + id;
  return id;
}
//fills the columns before the random number. 
function auto_fill(ws,rowNum,colNum){
    var uid = create_id();
  for (var colIndex = 1; colIndex <= colNum; colIndex++){
    if (ws.getRange(rowNum, colIndex).isBlank()){
      ws.getRange(rowNum, colIndex).setValue(uid);
      Logger.log(“Empty”);
    }
  }
}

Leave a Reply

More Articles & Posts