TheErtain
TheErtain

Reputation: 1

Fixing out of range error. Google scripts

I am wondering what is causing my out of range error. It doesn't happen every time, but if I do 100 in my input box, it will eventually happen and then sometimes right on start the script gives me an out of range error.

function guns(){
var gcol = SpreadsheetApp.getActiveSheet().getRange('G2:G').clearContent();
var weapons = SpreadsheetApp.getActiveSheet().getRange('F1');
var amountOfGuns = SpreadsheetApp.getActiveSheet().getRange('F2');
var seth = SpreadsheetApp.getActiveSheet();
var set = SpreadsheetApp.getActiveSheet();
var rand;
var inc = 2;
var acol;
var value = weapons.getValue();
var answer = Browser.inputBox('How many guns?');
amountOfGuns.setValue(answer);

while (inc < answer){

rand = Math.floor(Math.random()*value);  
acol = SpreadsheetApp.getActiveSheet().getRange('A'+rand);  
set.getRange('G'+inc).setValue(acol.getValue());
seth.getRange('H'+inc).setValue(rand);
 inc++;

}

}

enter image description here

F1 is the COUNTA for the rand variable. F2 is the input from the Browser.inputbox

Upvotes: 0

Views: 155

Answers (1)

Cooper
Cooper

Reputation: 64032

It's possible that rand = Math.floor(Math.random()*value); could be zero and then acol = sh.getRange('A'+rand); would become an undefined range because columns start at one not zero.

Upvotes: 1

Related Questions