nanashi
nanashi

Reputation: 43

Google Sheet app script timestamp in a button

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);
  };

Upvotes: 0

Views: 760

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15357

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().

Upvotes: 0

Related Questions