Home > Mobile >  Hide rows based on week's date
Hide rows based on week's date

Time:10-27

I tried to simplify the thread - I have a Google sheet with 5 named columns.
Under "Recursive_Requests" column, it's shown (randomly) 1 of the 8 possible options (every_day , every_monday , every_tuesday , every_wednesday , every_thursday , every_friday , every_saturday , No).


Every 24h from the row's "Timestamp", the sheet would check if the option under "Recursive_Requests" column matches with today's date. If it doesn't match, it'd hide the row until the requirement is met.
With "every_day" and "no", no action.


I've read and tried other threads, but none are completely similar (this, this and this, for example).


Tried also to filter and show (but doesn't work with dates):

//@OnlyCurrentDoc

function onOpen() {
  SpreadsheetApp.getUi().createMenu("filter")
    .addItem("rows_filter", "filter_rows")
    .addItem("rows_show", "show_rows")
    .addToUi();
}

function filter_rows() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Form responses 1");
  var data = sheet.getDataRange().getValues();
  for(var i = 2; i < data.length; i  ) {
    if(row_name[2] === "every_monday") {
      sheet.hideRows(i   1);
    }
  }
}

function show_rows() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Form responses 1");
  sheet.show_rows(1, sheet.getMaxRows());
}


e.g.:
Before any edit:

Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.05.38 Alexander third_department second_request every_monday
26/10/2022 19.07.56 Wayne second_department second_request every_tuesday
26/10/2022 19.09.36 Robert first_department first_request every_wednesday
26/10/2022 19.11.19 Larry first_department third_request every_thursday
26/10/2022 19.11.51 Jared third_department third_request every_friday
26/10/2022 19.13.41 Peter second_department third_request every_saturday
26/10/2022 19.14.58 Tom third_department first_request every_day


If it's Monday (after editing):
Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.05.38 Alexander third_department second_request every_monday
26/10/2022 19.14.58 Tom third_department first_request every_day

If it's Tuesday (after editing):
Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.07.56 Wayne second_department second_request every_tuesday
26/10/2022 19.14.58 Tom third_department first_request every_day


If it's Wednesday (after editing):

Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.09.36 Robert first_department first_request every_wednesday
26/10/2022 19.14.58 Tom third_department first_request every_day


If it's Thursday (after editing):

Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.11.19 Larry first_department third_request every_thursday
26/10/2022 19.14.58 Tom third_department first_request every_day


If it's Friday (after editing):

Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.11.51 Jared third_department third_request every_friday
26/10/2022 19.14.58 Tom third_department first_request every_day


If it's Saturday (after editing):

Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.13.41 Peter second_department third_request every_saturday
26/10/2022 19.14.58 Tom third_department first_request every_day


If it's Sunday (after editing):

Timestamp Name Department Request Recursive_Requests
26/10/2022 19.05.24 John first_department second_request No
26/10/2022 19.14.58 Tom third_department first_request every_day


Currently it isn't able to discriminate between different days and hide cells accordingly, as well as it can't check this condition every 24h based on the "Timestamp" value.

CodePudding user response:

Show Hide:

function showhide00() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
  const dt = new Date();
  const dtday = dt.getDay();
  const aObj = { "every_day": { "v": "show" }, "every_monday": { "day": 1 }, "every_tuesday": { "day": 2 }, "every_wednesday": { "day": 3 }, "every_thursday": { "day": 4 }, "every_friday": { "day": 5 }, "every_saturday": { "day": 6 }, "No": { "v": "show" } }
  vs.forEach((r, i) => {
    let day = new Date(r[0]).getDay();
    if (aObj[r[4]].hasOwnProperty("v") && aObj[r[4]].v == "show" || aObj[r[4]].hasOwnProperty("day") && aObj[r[4]]["day"] == dtday) {
      sh.showRows(i   2)
    } else {
      sh.hideRows(i   2)
    }
  })

}

SheetO (before):

Timestamp Name Department Request Recursive_Requests
17/10/2022 17.05.24 John first_department second_request No
17/10/2022 17.05.38 Alexander third_department second_request every_monday
17/10/2022 17.05.58 Tom third_department first_request every_day
every_tuesday
every_wednesday
every_thursday
every_friday
every_saturday

Sheet0 (After): today is a Wednesday

Timestamp Name Department Request Recursive_Requests
17/10/2022 17.05.24 John first_department second_request No
17/10/2022 17.05.38 Alexander third_department second_request every_monday
17/10/2022 17.05.58 Tom third_department first_request every_day
every_tuesday
every_wednesday

CodePudding user response:

This problem can easily be resolved with a query formula:

=Query({A:E}, "select * where Col5 = 'No' or Col5 = 'every_day' or Col5 = '"& switch(weekday(today()),2,"every_monday",3,"every_tuesday",4,"every_wednesday",5,"every_thursday",6,"every_friday",7,"every_saturday","every_sunday")&"'",1)

Replace A:E with the reference to your original table and you can replace today() with a cell reference containing a date if you don’t want to use today’s date. Make sure this query formula has enough space to the right and bottom to populate.

  • Related