Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

How do I use a macro to name a sheet with yesterday's date?


Here's what I have now that doesn't work,

spreadsheet.getActiveSheet().setName(Today()-1);

I was wondering if I could just use 'Today() - 1' in cell A1 for example and pull the data from that. Any help would be greatly appreciated. Thanks!


Solution

  • To name a sheet with yesterday's date, Use a macro like this -

    function myFunction() {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      var date = new Date()
      var ydate = Utilities.formatDate(new Date(date.getTime()-(24*3600*1000)), "GMT+1", "dd/MM/yyyy")
      spreadsheet.setName(ydate);
    }
    

    This is working perfecly.