thwippp
1/12/2017 - 3:35 PM

Best Day Ever-- Google Sheets -- To be Parsed Out

Best Day Ever-- Google Sheets -- To be Parsed Out

function randomObjects() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Stuff");
  
  var number = ss.getRangeByName("theNumberRange");
  
  var lastRow = sheet.getLastRow();
  var lastColumn = sheet.getLastColumn();
  
  var dataRange = ss.getRangeByName("dataRange");
  
  var thousands = ss.getRangeByName("thousandsRange").getValue();
  var hundreds = ss.getRangeByName("hundredsRange").getValue();
  var tens = ss.getRangeByName("tensRange").getValue();
  var ones = ss.getRangeByName("onesRange").getValue();
  
  //Creates a random number between 1 and the value on the spreadsheet--DEPRICATED
  //Put this in the for loops so that a new random number is generated every time
  //var thousandsRand = Math.floor(Math.random() * thousands);
  //var hundredsRand = Math.floor(Math.random() * hundreds);
  //var tensRand = Math.floor(Math.random() * tens);
  //var onesRand = Math.floor(Math.random() * ones);
  

  
  //Get values from a table and iterate them into a 2D Array!!!!!!!!!!!!!!!!!!!!!!!!!!!
  var stuffArray = [];
   
  for (var i = 0; i < dataRange.getNumRows(); i++) //number of rows
    {
      var subStuffArray = [];
      for (var j = 0; j < dataRange.getNumColumns(); j++) //number of columns
      {
        subStuffArray[j] = sheet.getRange(2+i,3+j,lastRow,lastColumn).getValue();
      }
      stuffArray[i] = subStuffArray;
    }
    
    //Logger.log(stuffArray);
    
  // To populate the alert with stuff
  var totalStuffText = [];
  
  /*
  for (var x = 0; x < thousands; x++)
    {
    totalStuffText.push(stuffArray[?][?]);
    }
  */
  
  ////////////////////////////////////////////////////////////////////////////////////
  var hundredsArray = []; //make this the sequential array of the hundreds, tens, ones
  
  for (var x = 0; x <= hundreds; x++)
  {
    hundredsArray.push(x);
  }
  Logger.log(hundredsArray);
  
  var hundredsRanNums = [];
  var i = hundredsArray.length;
  var j = 0;
  
  while (i--) {
    j = Math.floor(Math.random() * (i+1));
    hundredsRanNums.push(hundredsArray[j]);
    hundredsArray.splice(j,1);
  }
  //return hundredsRanNums;
  ////////////////////////////////////////////////////////////////////////////////////
  
  for (var x = 0; x < hundreds; x++)
  {
    var hundredsObject = totalStuffText.push(stuffArray[7][hundredsRanNums[x]]); //YAASSSSSSS
    //Logger.log(hundredsObject[x]);
  }  
    
    ////////////////////////////////////////////////////////////////////////////////////
    var tensArray = []; //make this the sequential array of the hundreds, tens, ones
    
    for (var y = 0; y <= tens; y++)
    {
      tensArray.push(y);
    }
    Logger.log(tensArray);
    
    var tensRanNums = [];
    var i = tensArray.length;
    var j = 0;
    
    while (i--) {
      j = Math.floor(Math.random() * (i+1));
      tensRanNums.push(tensArray[j]);
      tensArray.splice(j,1);
    }
    //return tensRanNums;
    ////////////////////////////////////////////////////////////////////////////////////   
    for (var y = 0; y < tens; y++)
    {
      totalStuffText.push(stuffArray[2][tensRanNums[y]]);
    }
 
 
 ////////////////////////////////////////////////////////////////////////////////////
  var onesArray = []; //make this the sequential array of the hundreds, ones, ones
  
  for (var z = 0; z <= ones; z++)
  {
    onesArray.push(z);
  }
  Logger.log(onesArray);
  
  var onesRanNums = [];
  var i = onesArray.length;
  var j = 0;
  
  while (i--) {
    j = Math.floor(Math.random() * (i+1));
    onesRanNums.push(onesArray[j]);
    onesArray.splice(j,1);
  }
  //return onesRanNums;
  //////////////////////////////////////////////////////////////////////////////////// 
  for (var z = 0; z < ones; z++)
  {
    totalStuffText.push(stuffArray[0][onesRanNums[z]]);
  }
   
   var lastEntry = totalStuffText.length - 1;
   
   var joinText = totalStuffText.slice(0, lastEntry) 
     + ", and" + totalStuffText[lastEntry];
       //Logger.log(joinText);
       
  //SpreadsheetApp.getUi().alert("You have collectively lost as much weight as:" + joinText + ".");
  ss.getRangeByName("weightStatementRange").setValue("You have collectively lost as much weight as:" + joinText + ".");
  
}