Google Sheet: How to write data with script to column, every time to new row

nahaku

Board Regular
Joined
Mar 19, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a simple "App" in google sheet when they press button it runs random number, then i want to store time and the value what they got in second sheet, as a log what happend.
I am new with scripting and i cannot find anywhere how to write to columns data from script.
Currently it only writes the data in to specified cell, but i want to put new record to new row.


JavaScript:
function getRandomInt() {
  return Math.floor(Math.random(0) * Math.floor(300));
}

function cleanCellNow() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheets()[0];
   var cell = sheet.getRange("A2");
   cell.setValue("");
  
}

function logTimeRightNow() {
  timezone = "GMT+" + new Date().getTimezoneOffset()/60
  var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd HH:mm");
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var logData = ss.getSheets()[1];
  var logcell = logData.getRange("A2"); 
  logcell.setValue(date);
}



function areYouSearched() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();   
   var sheet = ss.getSheets()[0];
   var cell = sheet.getRange("A2");
   var resultData = ss.getSheets()[1];
   var resultcell = resultData.getRange("B2");
  if (getRandomInt() % 2 == 0)  {
    cell.setValue("Search Now");
    resultcell.setValue("Search Now");
    
    logTimeRightNow()
    SpreadsheetApp.flush();
    Utilities.sleep(5000);
    cleanCellNow();
    }
 
  else {
    cell.setValue("Pass");
    resultcell.setValue("Pass");
    
    logTimeRightNow()
    SpreadsheetApp.flush();
    Utilities.sleep(5000);
    cleanCellNow();
    }
  }
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Watch MrExcel Video

Forum statistics

Threads
1,126,977
Messages
5,621,935
Members
415,867
Latest member
mauroccs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top