0

I'm looking to add random numbers automatically function

  var row = e.range.getRow();
  var col = e.range.getColumn();
  
  if (col === 2 && row > 1){
    e.source.getActiveSheet().getRange(row,1).setValue(new Rand());

if I write instead of new rand() - new Date() , it's work.

Marios
  • 26,333
  • 8
  • 32
  • 52

3 Answers3

2

Use Math.random() instead to get a random number between 0 and 1:

function onEdit(e) {
  var row = e.range.getRow();
  var col = e.range.getColumn();
  
  if (col === 2 && row > 1){
    e.source.getActiveSheet().getRange(row,1).setValue(Math.random()); 
}
}

If you want to get a random number between 1 and 10 use that inside the setValue() method:

Math.ceil(Math.random() * 10)
Marios
  • 26,333
  • 8
  • 32
  • 52
1
function onEdit(e) {
  var sh=e.range.getSheet();
  if(sh.getName()=="Sheet1" && e.range.columnStart==2 && e.range.rowStart>1) {
    e.range.setValue(Math.floor(Math.random()*1000));
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
0

From the question

if I write instead of new rand() - new Date() , it's work.

Date() is a JavaScript Constructor but Rand() isn't. To generate a random number you could use Math.random()

Resources

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166