I hope someone can help me with my struggle. I need to get the time slots after each "Text_Text" from this one sentence that is in a single cell:
Sth 03/30/2020 1:15 PM-03/30/2020 1:30 PM;"Text_Text" 03/30/2020 1:30 PM-03/30/2020 3:30 PM;"Sth" 03/30/2020 3:30 PM-03/30/2020 4:00 PM;"Text_Text" 03/30/2020 4:00 PM-03/30/2020 4:30 PM;"Sthh" 03/30/2020 6:00 PM-03/30/2020 6:15 PM;
Desired outcome:
start time and end time in different cells: 1:30 PM 3:30 PM 4:00 PM 4:30 PM
The pattern and length of the sentence are not consistent. But time always comes after "Text_Text" and the date.
I am working in Google Sheets . I know that this could be done with regex but it's too confusing for me and I can't get the syntax right. So far i was only able to get only 1 slot with split function but this is far way from what I need.
Best Answer
Your have data in a single Column spread over many rows. Each cell contains a string comprising multiples of records that follow a given pattern. The pattern is:
For example:
Sth 03/30/2020 1:15 PM-03/30/2020 1:30 PM;
.Each cell value contains many contiguous records, but there is no consistency for the number of records that comprise a cell value.
One of the identifiers is called "Text_text".
You wish to recover the time values from each "Text_text" record in every cell, in every row of data.
There may be many ways to achieve this outcome, including built-in functions. The following answer is a script solution. The solution is not as efficient as it might be since the times are written row-by-row rather than at the end of processing. However, some further user development would enable this barrier to be overcome.
The key aspects of the script are:
var count = values[i][0].split(searchTerm).length-1;
: this counts the number of instances of the search term ("Text_text") that occur in a cell.var rowtimes=[];
: a temporary array is created to store the relevant time values as they are extracted from the cell value. Later this array can be updated to the spreadsheet.var searchcounter = 0;
: a counter to enable progressive searches for the search term within a given cell value.var indexOfn = val.indexOf(searchTerm,searchcounter);
: returns the index number of the search term. Note that the the starting number for the search is set by thesearchcounter
.var indexofdash = val.indexOf(searchTermD, indexOfn);
: searches for the first dash that follows the index number of the search termvar indexofsc = val.indexOf(searchTermSC, indexOfn);
: searches for the first semicolon that follows the index number of the search termif(indexofdash-indexOfn == 29){
andif(indexofsc-indexofdash == 19){
: methods of determining whether the hour is between "1" and "9", or between "10" and "12"var timestr1 = val.substring(indexofdash-7, indexofdash);
var timestr1 = val.substring(indexofdash-8, indexofdash);
var timestr2 = val.substring(indexofsc-7, indexofsc);
var timestr2 = val.substring(indexofsc-8, indexofsc);
sheet.getRange(+i+1,2,1,+count*2).setValues([rowtimes]);
: updating the hour values to the spreadsheet by rowSample data