Search code examples
if-statementgoogle-sheetsgoogle-sheets-formulaarray-formulastextjoin

A custom function for joining cells and an optional value from an adjacent cell in Google spreadsheets?


I have a sheet that looks like this:

    --------------------------------------------------------------------------------
    Item     |   Count   |
    -----------------------------------------------------------------------------------
    Milk     |     1     |
    -----------------------------------------------------------------------------------
    Cereal   |     3     |
    -------------------------------------------------------------------------------
   Bread     |     2      |

When I use the JOIN function, I would get something like Milk, Cereal, Bread. What I want to do is add the value from the count column, but only if the value is greater than 1. So a custom function would print something like Milk, Cereal x3, Bread x2 How could I achieve this in Google Spreadsheets?


Solution

  • try:

    =ARRAYFORMULA(TEXTJOIN(", ", 1, IF(B2:B*1>1, A2:A&" x"&B2:B, A2:A)))
    

    0