Select “Time Description” based on current date and time in Google Sheets (VLookup, Query)

时光毁灭记忆、已成空白 提交于 2020-01-22 02:09:32

问题


I am trying to use VLookup or QUERY to extract a day and time description based on the current date and time. I thought QUERY may be the best here, but VLOOKUP seems to be the best solution perhaps.

Is it possible to extract the date from between date and time ranges? The descriptions may change frequently so they can't be hardcoded into the formula.

Here is the Google Sheet, which can also be copied. https://docs.google.com/spreadsheets/d/1efQo1t9FJbItYdf1OQJ4vzeG1bUaQ1R0vbFGsgIjzOI/edit?usp=sharing

This was the current formula, which I hoped would extract the correct date and timeslot description

=ARRAYFORMULA(IFERROR(TEXT(A2:A, "dddd ")&VLOOKUP(TIMEVALUE(A2:A), {
 TIMEVALUE(IFERROR(REGEXEXTRACT(TimeDescription!A2:A, "(.*) -"))), TimeDescription!B2:B}, 2, 1)))

回答1:


based on your time data logic try:

=ARRAYFORMULA(IFERROR(TEXT(A2:A, "dddd ")&VLOOKUP(TIMEVALUE(A2:A), {
 TIMEVALUE("00:00"), "Evening";
 TIMEVALUE("07:00"), "Breakfast";
 TIMEVALUE("09:00"), "Morning";
 TIMEVALUE("16:00"), "Daytime";
 TIMEVALUE("20:00"), "Evening"}, 2, 1)))


for non-hardcoded references use:

=ARRAYFORMULA(IFERROR(TEXT(A2:A, "dddd ")&VLOOKUP(TIMEVALUE(A2:A), 
 SORT({TIMEVALUE(REGEXEXTRACT(TimeDescription!A2:A, "(.*) -")), 
 TimeDescription!B2:B}, 1, 1), 2, 1)))

spreadsheet demo




回答2:


If you have =NOW() in A2, this formula should work without any additional tabs or descriptions...

Apologies, i'd originally Missed the necessity of the day of the week as well. This should work. Again, just with NOW() in A2 and nothing else on the sheet.

=ARRAYFORMULA(JOIN(" ",VLOOKUP(MOD(A2,{7,1}),SORT({{{7;9;16;20}/24;SEQUENCE(7,1,0)},{"Breakfast";"Morning";"Daytime";"Evening";TEXT(SEQUENCE(7,1,0),"dddd")}}),2)))


来源:https://stackoverflow.com/questions/59724955/select-time-description-based-on-current-date-and-time-in-google-sheets-vlook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!