Google-sheets – run a Google Sheets function on the first Monday of every month

google sheetsgoogle-apps-script

I'm using Google Sheets functions. How do I run a function only on every first Monday of the month?

function HideH() {
  var ss = SpreadsheetApp.getActive(),
    target = ss.getSheetByName('Raspored'),
    hsh = ss.getSheetByName('5. tjedan');
  if (ss.getSheetByName('Ne Dirati')
    .getRange(4, 6)
    .getValue() === 1) {
      target.hideColumns(8);
      hsh.hideSheet();
      var width = 165;
  } else {
    target.showColumns(8);
    hsh.showSheet();
    width = 143
  }
  for (var i = 4; i < 8; i++)
    target.setColumnWidth(i, width);
}

Best Answer

Use a time-driven trigger that runs every day including a comparison of the current day to check if its the first Monday of the month, if it isn't, do nothing, if it is, then do whatever should be done.

  • Use JavaScript's new Date() to create a date object with the current date.
  • Use getDay() on the date object to get a serial number representing the day of the week, 0 for Sunday, 1 for Monday and so on.
  • Use getDate() on the date object to get the day of the Month.