0

I am trying to create a button in google sheet when if pressed it will populate the current cell and the cell to its right with the current date and time the moment the button was pressed, but I am getting this error message when I try to press/run the code.

button error

and this is my code which I have copied and edited from this post Static TIMESTAMP for Google Sheets

    function TIMESTAMP() 
  {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var addedDateAndTime1 = Utilities.formatDate(new Date, 
    ss.getSpreadsheetTimeZone(), "MMMM d, yyyy");
    var addedDateAndTime2 = Utilities.formatDate(new Date, 
    ss.getSpreadsheetTimeZone(), "hh:mm:ss a");
    //Optional if you only want the date: var addedDate = Utilities.formatDate(new Date, ss.getSpreadsheetTimeZone(), "dd/MM/yyyy");
    //Optional if you only want the time: var addedTime = Utilities.formatDate(new Date, ss.getSpreadsheetTimeZone(), "hh:mm:ss");
    //sheet.getRange(1,1).setValue(addedDateAndTime)
    sheet.getCurrentCell().offset(0, 0).activate();
    sheet.getActiveRangeList().setValue(addedDateAndTime1);

    sheet.getCurrentCell().offset(0, 1).activate();
    sheet.getActiveRangeList().setValue(addedDateAndTime2);
  };
nanashi
  • 43
  • 5

1 Answers1

0

You are passing the Date constructor to Utilities.formatDate() instead of initialising:

var addedDateAndTime1 = Utilities.formatDate(new Date(), 
    ss.getSpreadsheetTimeZone(), "MMMM d, yyyy");
var addedDateAndTime2 = Utilities.formatDate(new Date(), 
    ss.getSpreadsheetTimeZone(), "hh:mm:ss a");

Just need to change the new Dates to new Date().

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54
  • I have already changed the `new Date`s to `new Date()` , sadly I still get the same error message – nanashi Aug 10 '22 at 02:59