I have made a Google Sheet for handling my medication.
In column A, I have a dropdown of medication that I take. The time I initially take the medication isn't important, but what is important is when I take the next pill and it is different for each one. One pill, after taking it, I need to take the next one 6 hours later while another, I don't take until 24 hours later. I go to sleep at different times every day so when I take the initial pill depends on when I wake up or when I eat breakfast or such. The most important one is the one that I need to wait 6 hours before I can take it again. That is my pain pill.
So far, I have the sheet setup so that I have the drop down list of medication and column 2 inputs the exact date and time that I take the pill. The 3rd column, I have a formula that is set to take column 2 and add 6 hours to it which is not correct for all meds, so that is where I need help. I know it can be done, but I don't know who to ask. If I can get it so that the 3rd column will see what medication I chose then depending on which one it will put the time that I need to take the next one. Here is what I have so far but using a formula to add 6 hours which applies to all and that is why the formula won't work:
Worksheet :
Medication Pill-taken-at +6 hours
----------------------------------------------------------
Mycoxofloppin 2018-03-17 12:44 2018-03-17 18:44
Maryjaneaspliff 2018-03-16 04:20 2018-03-16 10:20
Script :
function onEdit(e) {
var timezone = "GMT-5";
var timestamp_format = "yyyy-MM-dd hh:mm";
var ss = e.source.getActiveSheet();
var start = 2;
var end = 4;
var row = e.range.getRow();
if (e.range.getColumn() == 1){
ss.getRange(row, start).setValue(new Date());
}
}
Range::getValue
gets you the value of a cell in Google Sheets' API. I'm assuming that your cells are typed as DateTime
so getValue()
will give you a JavaScript Date
value that you can easily perform date-calculations on.
You don't need to worry about timezones or date formats if your cells are typed correctly. ("typed" as in "data-type", not "keyboard typing").
function onEdit(event) {
var range = event.range;
if( range.getColumn() != 1 && range.getColumn() != 2 ) {
// Only update when column 1 or 2 are updated.
return;
}
var rowNumber = range.getRow();
var ss = event.source.getActiveSheet();
var drugName = ss.getRange( rowNumber, 1 ).getValue();
var lastDoseTime = ss.getRange( rowNumber, 2 ).getValue();
// Validate info in the edited row:
if( !drugName ) {
console.warn( "No drug name specified in col 1, row %d.", rowNumber );
return;
}
if( !( lastDoseTime instanceof Date ) ) {
console.warn("No valid date value in col 2, row %d.", rowNumber );
return;
}
var hoursUntilNextDose = getTimeBetweenDosesInHours( drugName );
if( !hoursUntilNextDose ) {
console.warn("No dose time available for the drug \"%s\".", drugName );
return;
}
var timeUntilNextDoseInMilliseconds = hoursUntilNextDose * 60 * 60 * 1000;
var nextDoseUnix = lastDoseTime.getTime() + timeUntilNextDoseInMilliseconds;
var nextDoseTime = new Date( nextDoseUnix );
ss.getRange( rowNumber, 3 ).setValue( nextDoseTime );
}
function getTimeBetweenDosesInHours( drugName ) {
// toUpperCase() is more robust than toLowerCase(): https://msdn.microsoft.com/en-us/library/bb386042.aspx
switch( drugName.toUpperCase() ) {
case "MYCOXOFLOPPIN" : return 6;
case "MARYJANEASPLIFF": return 4.20;
case "GYM" : return 365 * 24;
default : return null;
}
}
Use a table: Say
Y1: Pill Name Z1:(Interval(hrs)) Y2: Dumiliyuop Z2: 6 Y3: WhacyHittlir Z3:18
C2:
=B2+VLOOKUP(A2,Y:Z,2,0)/24
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.