My apologies if I don't word this perfectly as I am a novice coder. I am Using Googles Apps Script I am trying to setup a system to conditionally format cells within Google Sheets. This is for a system that will check admission dates of physicians from a .CSV and check the most recent MD or NP treatment date. For the first 90 days since admission a visit needs to occur in the 0-30 range, the 30-60 range, and the 60-90 range.
My problem is with writing code that will check whether the last MD treatment date is within a range of 30-60 days from the admission.
For the example code before I am attempting to look at a line item where a the admission date is between 40-50 days old. This would place it within the 30-60 range. I want to verify if the LastMDTreatment date is outside (admissiondate+30days , admissiondate+60days). If there is no visit between 30-60 range on day 40-50 I would like to highlight the cell yellow.
function applyFilter(p, data) {
//setting up the admission days by pulling from the .csv and then calculating how many days since
let dateAdmissionDate = new Date(p.admissionDate);
console.log('dateAdmissionDate:', dateAdmissionDate);
let today = new Date();
let AdmissionDays = Math.floor((today.getTime() - dateAdmissionDate.getTime()) / (24 * 60 * 60 * 1000));
//setting up the MDtreatmentdays and NP treatment days by pulling the last treatment dates from the .csv and then seeing how many days from today its been
let dateLastMDTreatmentDate = new Date(p.lastMDTreatmentDate);
let dateLastNPTreatmentDate = new Date(p.lastNPTreatmentDate ? p.lastNPTreatmentDate : p.admissionDate);
var MDTreatmentDays = Math.floor((today.getTime() - dateLastMDTreatmentDate.getTime()) / (24 * 60 * 60 * 1000));
var NPTreatmentDays = Math.floor((today.getTime() - dateLastNPTreatmentDate.getTime()) / (24 * 60 * 60 * 1000));
//if its past admission day 40 and before day 50 and there is a there is only an MD treatment day in the admission days 0-30 then its time to warn the MD cell so the MD will know to start a visit
if (AdmissionDays >= 40 && AdmissionDays <= 50 && MDTreatmentDays >= 10 && MDTreatmentDays <= 20) {
console.log('filter rule 4 activated');
out.colors[p.lastMDTreatmentDateCol] = color_warn;
out.notes[p.lastMDTreatmentDateCol] = 'A visit needs to occur every 30 days for the initial 90 days. Admission ' + AdmissionDays + ' days ago. It has been ' + MDTreatmentDays + ' days since the last MD visit';
out.skip = false;
}
My issue with the code as it stands is if its admission day 45 and a MD visit occurred 11 days ago on day 34 this code would erroneously highlight it. Or if its admission day 40 and there was a MD visit 10 days ago it would erroneously highlight it. I figure I need some sort of iterative way of going about this but again given my coding novice I cant figure out a solution. The code does run in its current aspect I'm just finding conditional formatting logic rules to be beyond my understanding.
For the example assume todays date is December 1st, 2023.
To allow better understanding of how this would work here is the csv that will be parsed by the tool would look. To be clear all information in this is fictitious.
Last | First | Admit | Admission Days | MD | MD Treatment | MD Visit Days |
---|---|---|---|---|---|---|
Pulliam | Brooklynn | 10/17/2023 | 45 | Deborah Anne | 11/20/2023 | 11 |
Way | Sofia | 11/7/2023 | 24 | John Smith | 11/12/2023 | 19 |
Brothers | Dayna | 10/21/2023 | 41 | John Smith | 11/8/2023 | 23 |
Bailey | Emani | 10/26/2023 | 36 | Dave Ramsey | 11/3/2023 | 28 |
McGinnis | Brielle | 10/21/2023 | 41 | Mark Wahlberg | 11/17/2023 | 14 |
Everett | Jaya | 10/21/2023 | 41 | John Smith | 11/24/2023 | 7 |
Knox | Joaquin | 10/29/2023 | 33 | John Smith | 11/30/2023 | 1 |
Melvin | Eli | 10/20/2023 | 42 | Mark Wahlberg | 11/27/2023 | 4 |
And ideally the output would be formatted and look like this. Yellow is the cells that would be highlighted yellow by properly working code .
Last | First | Admit | Admission Days | MD | MD Treatment | MD Visit Days |
---|---|---|---|---|---|---|
Pulliam | Brooklynn | 10/17/2023 | 45 | Deborah Anne | 11/20/2023 | 11 |
Way | Sofia | 11/7/2023 | 24 | John Smith | 11/12/2023 | 19 |
Brothers | Dayna | 10/21/2023 | 41 | John Smith | 11/8/2023 yellow | 23 |
Bailey | Emani | 10/26/2023 | 36 | Dave Ramsey | 11/3/2023 | 28 |
McGinnis | Brielle | 10/21/2023 | 41 | Mark Wahlberg | 11/17/2023 yellow | 14 |
Everett | Jaya | 10/21/2023 | 41 | John Smith | 11/24/2023 | 7 |
Knox | Joaquin | 10/29/2023 | 33 | John Smith | 11/30/2023 | 1 |
Melvin | Eli | 10/20/2023 | 42 | Mark Wahlberg | 11/27/2023 | 4 |
But I believe my code as currently written would be too "dumb" to catch this. As on admission day 45 with a MD visit 11 days ago it would highlight the cell.
To have it as simple as possible this is the list of conditions I want to have highlighted.
Admission Day | MD Visit Day |
---|---|
40 | greater than 10 and less than 40 |
41 | greater than 11 and less than 41 |
42 | greater than 12 and less than 42 |
43 | greater than 13 and less than 43 |
44 | greater than 14 and less than 44 |
45 | greater than 15 and less than 45 |
46 | greater than 16 and less than 46 |
47 | greater than 17 and less than 47 |
48 | greater than 18 and less than 48 |
49 | greater than 19 and less than 49 |
50 | greater than 20 and less than 50 |
Where given the admission day it changes the range I should be using for MD Visit Days. Do we think I should rewrite this following line of code
if (AdmissionDays >= 40 && AdmissionDays <= 50 && MDTreatmentDays >= 10 && MDTreatmentDays <= 20) {
To look like this following code
if (AdmissionDays >= 40 && AdmissionDays <= 50 && MDTreatmentDays > (AdmissionDays - 30) && MDTreatmentDays <
AdmissionDays) {
It feels like I should be setting a date range as admission date until admission date + 30 days and if the MD treatment Date is within that range highlight the cell.
This code should solve the specific issue of how to determine whether a date occurred within a set range of time after a prior date. For alternative solutions to this coding problem click this link to the Stack Overflow forum post here: Check if one date is between two dates.
if (AdmissionDays >= 40 && AdmissionDays <= 50 && MDTreatmentDays > (AdmissionDays - 30) && MDTreatmentDays <
AdmissionDays) {