简体   繁体   中英

Google sheets, how to change cell based on string from another cell

I have this sheet with a bunch of Ls and Hs that come in 4 combinations.

I want Google Sheets to identify which pattern the Ls and Hs are.

Here are some examples of what the Ls and Hs look like:

HLLL
LH
LHLL
LHHL
LHHH
LHHHH

I want Google Sheets to be able to run a check like:

if cell contains LH, continue, else change cell text to "Pattern 1"

if cell contains HL, continue, else change cell text to "Pattern 2"

if contains LL, change cell text to "Pattern 3", else change cell text to "Pattern 4"

However, each set cannot contain 2 patterns. So HLLL cannot be Pattern 1 and Pattern 3 at the same time. It must be Pattern 1.

Is there any way to do this in Google Sheets?

Thanks.

EDIT**

I was able to color code the patterns with conditional formatting but I'm still unable to solve the original problem. Here's what my conditional formatting looks like:

enter image description here

Red is "Pattern 1", it must start with HL
Blue is "Pattern 2", it must start with LH and stay H, no more Ls
Green is "Pattern 3", it must end with LL
Yellow is "Pattern 4", it must end with HL

The end result is something like this: enter image description here

So that's Row D. I want Row E for example to write down what pattern the Ls and Hs in Row D are.

So Green in D23 would automatically write "Pattern 3" in E23. Blue in D24 would put "Pattern 2" in E24 etc.

Each set of Ls and Hs CANNOT have more than 1 pattern. The checks must be done in the specific order that I put them in to avoid misrecognizing the pattern.

HLLL is not Pattern 3 even though it ends with LL because it starts with HL, which is the first check.

The checks take priority in that, if lands on Pattern 1, it will not get overwritten by Pattern 3. Top to bottom heirarchy.

You could maybe try:

在此处输入图像描述

Formula in B1 :

=INDEX(IF(A1:A<>"",MATCH(REGEXREPLACE(A1:A,"^(?:(HL).*|(LH)H*|.+([HL]L))$","$1$2-$3"),{"HL-","LH-","-LL","-HL"},0),""))

The regular expressions ^(?:(HL).*|(LH)H*|.+([HL]L))$ means to match:

  • ^ - Start string anchor;
  • (?: - Open non-capture group to allow for alternations;
    • (HL).* - A 1st capture group to match the 1st pattern;
    • (LH)H* - A 2nd capture group to match the 2nd pattern;
    • .+([HL]L) - A 3rd capture group to match both the 3rd and 4th subpattern.
  • )$ - Close non-capture group and match end-string anchor.

REGEXREPLACE() - will return any of the following patterns; {"HL-","LH-";"-HL";"-LL"} and MATCH() will return the appropriate number of any of those.

Your description is somewhat inconsistent but I think this is what you are going for. The criteria of your conditional formats can be custom formulas and not just the rigid preset match types:

条件格式规则

Read custom formulas as if you applied them to the top left cell of the formatted range. The references in the formulas are absolute column but relative row reference ( $A2 ). The absolute column reference means both A2 and A3 will look at A2 to determine conditional formatting. The relative row reference means B2 and B3 look at B2 instead of A2.

The second formula (that cuts off in the image) uses a SUBSTITUTE function to remove all the 'H's and confirm that all that remains is the lone 'L'. This means that the presence of any other letter (such as 'G' will cause this pattern to not match):

=AND(LEFT($A2,2)="LH", SUBSTITUTE($A2,"H","")="L")

最后结果

For completeness I am showing the format numbers in column B. The formula in B2 is a concatenation of all the conditions:

=IF(LEFT(A2,2)="HL",1,IF(AND(LEFT(A2,2)="LH",SUBSTITUTE(A2,"H","")="L"), 2, IF(RIGHT(A2,2)="LL",3,IF(RIGHT(A2,2)="HL",4,-1))))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM