Google Sheets - Combine multiple IF Functions into one cell

一笑奈何 提交于 2021-01-28 21:26:07

问题


I'm trying to produce a SKU in Google Sheets for a product using the values of three variants (Title, Colour and Size)

The product is 'Lightweight trainers' with colour variants of 'Red' and 'Blue', and the sizes range from 5 - 12.

Link to spreadsheet

https://docs.google.com/spreadsheets/d/1trq0X3MjR-n2THFnT8gYYlwKscnQavCeeZ8L-ifYaHw/edit?usp=sharing

Aim

I'm hoping to have a SKU that displays the product, the colour variant and the shoes size. Example: LW-1-8 (Lightweight trainer, colour Red, size 8)

Product is Lightweight Trainers with a value of LW.

Colour variant 'Red' with a value of 1 and 'Blue' with a value of 2.

Shoe size variant = number ranging from 5 to 12.

Here's what I have so far, joining the colour and size variants.

=IFS(I2="Red",1,I2="Blue",2)&"-"& IFS(K2="5",5,K2="6",6,K2="7",7,K2="8",8,K2="9",9,K2="10",10,K2="11",11,K2="12",12)

However, I'm getting stuck in joining the data in column B with this function.

Any help with combining this data from multiple cells into one would be greatly appreciated.


回答1:


TL;DR

=ARRAYFORMULA(IF(B2:B<>"", IFS(B2:B="Lightweight Trainers", "LW")&"-"&IFS(I2:I="Blue", 1, I2:I="Red", 2)&"-"&K2:K,))

Answer

What you want is basically:

<title>-<color number>-<shoe size>

To convert this to a function we can split it into each part and take it step by step:

Step 1: Title For the first part -the title- we need to match the value with the shorthand. A simple list in an IFS is enough.

IFS(B2="Lightweight Trainers", "LW")

Obviously for now it only has a single value (Lightweight Trainers) but you could add more:

IFS(B2="Lightweight Trainers", "LW", B2="Heavyweight Trainers", "HW")

Step 2: color number Similar to the previous step, it’s a mapping using ifs:

IFS(I2="Blue", "-1", I2="Red", "-2")

The dash is added so when adding everything it will only have it if

Step 3: shoe size In this case we can simply get the value:

K2

Step 4: Adding everything together We only need to add it with the dashes in between:

=IFS(B2="Lightweight Trainers", "LW")&"-"&IFS(I2="Blue", 1, I2="Red", 2)&"-"&K2

Step 5: Extending for the entire column automatically

We will use ARRAYFORMULA to add a single formula to the first cell and get it automatically extended to the entire column. We first add it to the formula we already have, and then extend the ranges to the entire column:

=ARRAYFORMULA(IFS(B2:B="Lightweight Trainers", "LW")&"-"&IFS(I2:I="Blue", 1, I2:I="Red", 2)&"-"&K2:K)

Remember to remove all the values in the column so array formula doesn’t override them (it would generate an error).

As you can see the formula generates errors for the rows that have no values. A good way of handling this case is to filter the rows without a title. In a single row would be:

=IF(B2<>"", [the entire formula],)

Notice the last comma.

So putting everything together and extending its range to the column, is:

=ARRAYFORMULA(IF(B2:B<>"", IFS(B2:B="Lightweight Trainers", "LW")&"-"&IFS(I2:I="Blue", 1, I2:I="Red", 2)&"-"&K2:K,))

Adding this to N2 should work.

Final notes

It seems that you use 150 when the size it’s not a whole number. If you want to keep that functionality you may use:

IF(K2-int(K2)=0, K2, 150)

On the last component and expand it the same way.

You may also want to prevent having two dashes when a value is missing (LW-5 instead of LW--5). To do so, I’d recommend adding it to each component instead of the formula that adds them together.

References

  • IFS (Docs Editors Help)
  • IF (Docs Editors Help)
  • ARRAYFORMULA (Docs Editors Help)



回答2:


try in N2:

=IFS(I2="Red",1,I2="Blue",2)&"-"& 
 IFS(K2=5,5,K2=6,6,K2=7,7,K2=8,8,K2=9,9,K2=10,10,K2=11,11,K2=12,12)

or use:

=IF(I2="red", 1, IF(I2="blue", 2, )&IF((K5>=5)*(K5<=12), "-"&K5, )


来源:https://stackoverflow.com/questions/65806580/google-sheets-combine-multiple-if-functions-into-one-cell

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