Search code examples
regexsortinggoogle-sheetsgoogle-sheets-formulaflatten

How to customize order of header values that are joined when answer is yes in Google Sheets


How can I modify the equation in this Google Spreadsheet which I found as an answer to another question so that it can allow a user to customize the order of replaced values?

Goal Replace "yes" answers in a range in a given row (ie A2:D4), with a header values separated by commas in E4 in an order that is desired.

Preliminary approach The preliminary approach uses "helper" cells in the spreadsheet (more below image), but I would appreciate help in combining two independent formulas.

enter image description here

Specify order The headers are labeled with a number in the desired order. The original formula will look for a "yes" and replace that with the header value for any questions in a range and outputs as an index

The second formula will then split and sort the values in alphabetical order and then REGEXREPLACE the numbers.

Any assitance to combine these two formulas is much appreciated!

SET #1:

=index(substitute(substitute(trim(transpose(query(transpose(if(B2:E<>"yes",,substitute(B1:E1," ","❄️"))),,9^9)))," ",", "),"❄️"," "))

AND

=TRIM(REGEXREPLACE(JOIN(",",SORT(TRANSPOSE(SPLIT(E2,",")))),"\b\d_",""))

ALTERNATIVE SET #2

INDEX(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE( IF(B2:E="yes", B1:E1&",", )),,9^9))), ",$", ))

AND

=TRIM(REGEXREPLACE(JOIN(",",SORT(TRANSPOSE(SPLIT(E2,",")))),"\b\d_",""))

Thank you in advance for your help!

The shared google sheet is here


Solution

  • use:

    =INDEX(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
     IF(TRANSPOSE(SORT(TRANSPOSE(A2:D), FLATTEN(A1:D1), 1))="yes", 
     REGEXREPLACE(TRANSPOSE(SORT(FLATTEN(A1:D1))), "(^\d+_)", )&",", )),,9^9))), ",$", ))
    

    enter image description here