Conditional formatting dependant of value from adjacent cell - google-apps-script

I set up a time recording sheet in Google Spreadsheets. Now I am trying to achieve a conditional formatting of cells depending on the value of other cells in the spreadsheet:
IF value from cell G2 is greater than 06:30:00 (hh:mm:ss)
AND value from cell D2 is smaller than 00:30:00 (hh:mm:ss) OR cell D2 is blank
then set background colour of cell D2 to yellow.
IF value from cell G2 is greater than 08:45:00 (hh:mm:ss)
AND value from cell D2 is smaller than 00:45:00 (hh:mm:ss) OR cell D2 is blank
then set background colour of cell D2 to yellow.
If the conditions aren't met the background colour should be reset to none.
I learned that it is not possible using formulas. Is there an approach doing it in Google Apps Script? I am no scripter but know how to insert snippets and may be able to read and understand if someone would be so nice to explain it to me.
When searching for similar use cases the only reference to Google Apps script I found was this link, a general discussion of its capabilities.

This should be possible by creating an onEdit trigger for a function like this:
function myOnEdit(e) {
var nextColumnCell = e.range.offset(0, 1);
if (e.range.getValue() == 'foo') {
nextColumnCell.setFontWeight('bold');
} else {
nextColumnCell.setFontWeight('normal');
}
}
More information on triggers can be found here.

Somehow, I can't also find for a way to directly compare time in Google Spreadsheet..
But I it is possible to convert from time to number format. By doing so, you will be able to compare the entities.
1 in number format is equal to 00:00:00 in time format because 1 stands for 12:00am
0.5 in number format is equal to 12:00:00 in time format because 0.5 stands for 12:00pm
0.25 in number format is equal to 6:00:00 in time format because 0.5 stands for 6:00am
It doesn't answer the main question but I hope it helps.

It is (now) possible with a formula.
Select ColumnD and clear any existing CF rules from it. Format, Conditional formatting..., Format cells if... Custom formula is and
=and(row()<>1,or(and(G1>6.5/24,D1<0.5/24),and(G1>8.75/24,D1<0.75/24)))
with yellow fill and Done.

Related

How to get the value from another sheet with variable sheet name using Google Sheets?

I need to get the sheet name from a cell.
On image below I'm trying to get '4/2'!$AB60.
need to get the date which is 4/2 (cell D5)
combine it with !$AB60
now the cell D6 will have a formula value of '4/2'!$AB60
it will display the value.
Is there a way to formulate this instead of what I currently be doing, manually updating the each column dates (from C6 to AF6)? Is this even possible?
Formula from the screenshot: =JOIN(,LEFT(D5,LEN(D5),!$AB60)
The problem with this formula is !$AB60 as it is not a valid value, instead use "!$AB60" or use =TO_TEXT(D5)&"!$AB60". To use the result of this formula as a cell reference use INDIRECT, i.e.:
=INDIRECT(TO_TEXT(D5)&"!$AB60")

Google App scripts: Unable to preserve Leading zeros since it is automatically removed

I am new to google script. I have a scenario like the below.
I have Sheet1 with Column A and B. Usually I try to store decimal values in to it. I mean integer part in column-A and decimal part in Column-B
For example:
Scenario-1:
It the value is 23.75 then Column-A should be 23 and ColumnB would be 75
Scenario-2:
If the value is 22.00 then Column-A should be 22 and ColumnB would be 00
I tried like this
sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
column = sheet.getRange("B:B");
column.setNumberFormat("#");
sRange = "O1:O" + 65;
cell = sheet.getRange(sRange);
cell.setValue("00");
This works fine for scenario-1, but for scenario-2 it displays only one zero (leading zero is automatically removed). I tried to change the complete column formatting to plain text but no luck.
I am strugging to achieve this for the past two days. Any help is greatly appreciated.
Adding an apostrophe:
cell.setValue("'00");
might be considered adequate.

How to convert Time into decimal float in Google Sheets using Script?

I want to convert the time HH:MM into H.xx
Like I am getting it in this format: Sat Dec 30 00:00:00 GMT+05:21 1899
But this value is 04:29 in cell. I want it to be 4.5 hours to multiply it to hourly rate.
Google Sheets
In Google Sheets, if you have a date/time value in a cell (e.g. "D9"), then use =HOUR(D9)+(MINUTE(D9)/60).
If the value is stored in the format 04:29, then use =INDEX(SPLIT(D9, ":"), 1) + (INDEX(SPLIT(D9, ":"), 2)/60).
Google Sheets API & Google Apps Script
If you want to use the Google Sheets API or Google Apps Script, then you can use javascript.
You need to use the getMinutes() method and divide by 60, then add that to the hour (using getHours()).
var date = new Date();
var minutes = date.getMinutes();
var output = date.getHours() + (minutes/60);
Be aware that this is ignoring seconds.
If the value in the cell is stored as a string like 04:29, then you'll need to split it.
var time = "04:29";
var hour = Number(time.split(":")[0]);
var minutes = Number(time.split(":")[1]);
var output = hour + (minutes/60);
Just Multiply your Duration by 24.
Example:
Time In | Time out | Duration | Decimal
11:45:00 AM | 3:40:00 PM | 3:55:00 | 3.92
You also have to alter the format of the cell containing the decimal from Automatic to Number in order for this to work in Google Sheets.
All you need is TO_PURE_NUMBER()
https://support.google.com/docs/answer/3094243?hl=en
Example:
// The following
A1=DATEVALUE("2020-05-18 06:09:18.821 ")+TIMEVALUE("2020-05-18 06:09:18.821 ")
A2=TO_PURE_NUMBER(A1)
A3=TO_DATE(A2)
// Results in the following, first result is just a formatted date
2020-05-18 6:09:19
43969.25647
5/18/2020
I tried the above methods with limited success, as I needed to have another cell to convert the time value to after calculation. However, I tried out a modification of this, and to my surprise, it works. I'll share my solution and hope it helps.
I have a Start Time and an End Time. Let's say my Start Time is in D6 and my End Time is in E6.
With the above formulas, I would have to have a cell (let's say F6) which does E6-D6, then another cell (let's say G6) to convert F6 from time to decimals.
I short-cut this by using:
=INDEX(SPLIT(E6-D6, ":"), 1) + (INDEX(SPLIT(E6-D6, ":"), 2)/60)
directly into F6 without the need for a G6 cell to convert.
Hope this helps!
=(INDEX(SPLIT(E17, ":"), 1) &"."& (INDEX(SPLIT(E17, ":"), 2))) * hourlyRate
This seemed to work:
=TEXT(D2-C2,"h:mm")*24
Where D2 is End Time and C2 is Start Time,
The =TEXT(D2-C2,"h:mm") part both subtracts the times and converts the result into time format. The * 24 part makes it into a usable decimal instead of time format. So, in my case, I had Start Time as 12:30 pm and End Time as 1:20 pm. The Sheets was seeing the difference as :50, which it was interpreting as 12:50am. But when you multiply by 24, it then works out, giving you .833333333.
I tested it with Start Time 10am and End Time 12pm and got 2.000000000 which is also correct.
Oops I see now you wanted it using script. Sorry. This is for within sheets.
First of all - this thread is sure useful. It took me a couple of answers to understand what am I supposed to do, So.. I'm sharing my experience + images.. yay
What you should do:
Select format --> number --> automatic (see image below)
Insert your hours values (each value in a different cell)
Subtract time values to represent the difference between them (in time format)
Use timevalue function to represent the the difference as a decimal number
Multiply the value with 24 in order to get the decimal representation in hours
Here's an image that displays the values I've got
And here's the formulas (assuming that time values are in columns A and B)
Column C --> B - A for C1 formula is B1-A1, C2 formula is B2-A2 etc.
Column D --> timevalue(C) for D1 formula is timevalue(C1), D2 formula is timevalue(C2) etc.
Column E --> D * 24 for E1 formula is D1 * 24, E2 formula is D2 * 24 etc.
or if you prefer this as a single formula
timevalue(B - A) * 24
Hope that I've clarified myself enough and that your are going to solve this even if you are not a software developer. Cheers

hh:mm duration format only

I am using Google Sheets to collect some data I need a cell to only allow a duration hh:mm input.
Here is the formula data validation I have right now
=regexmatch(text(E11,"hh:mm"), "[0-1][0-9]:[0-5][0-9]$")
The only thing this is not doing for me is not allowing whole number responses (i.e. 1,3,5..). These values break some formulas that I have as 7 = 168 hours in duration. What can I do to limit only to hh:mm format entry?
I am willing to use a script if this is what is needed.
=REGEXMATCH(TO_TEXT(E11),"\d{1,2}:[0-5]\d?")
TO_TEXT gets display values instead of number.
\d{1,2} adds a condition for a 1 to 2 digit number.

getvalues() returns a complete date in the year 1899 for a cell containing only time

If we have only a time in say cell A1 of Google sheets. For example, I have this:
8:09:00 AM
When I use this to fetch the contents of the cell: sheet.getRange(1, 1).getValue();
this is what it returns:
Sat Dec 30 1899 08:09:00 GMT-0500 (EST)
Why is this happening? Any way for getvalue() to stop interpreting the data and get the raw data instead?
Note: No special formatting has been set by me for the cell. So I guess the formating is the default (automatic), which is not something I want to change for every cell containing such data. The cells containing such times will not be known apriori in any case.
Use range.getDisplayValue() to get the display value as a string.
More details on this can be found here: Difference between getValue() and getDisplayValue() on google app script
In short, getvalues returns an object which can be a number,a date or a string. In this case it returns a date object, which why it get formatted differently.