Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Last active February 12, 2016 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainysmurf/d27e5ae3f24f2b5a3c6f to your computer and use it in GitHub Desktop.
Save brainysmurf/d27e5ae3f24f2b5a3c6f to your computer and use it in GitHub Desktop.
MultipleChoiceToOneRowEach
/*
Summary of the Multiple Choices selected, with a space-bar ' | ' between each item
Removes leading and trailing space-bars (and internally repeating!) with a regexp:
The regexp does the following manipulations:
FROM TO
"A | |" --> "A"
" | | C" --> "C"
" | B | " --> "B"
"A | B | " --> "A | B"
" | | B | C" --> "B | C""
"A | | C" --> "A | C"
*/
=ARRAYFORMULA( // Iterate over the rows (defined in parameters)
if(row(Data!A1:A)=1, // If the first row...
"Concat", // ... just print the column name
REGEXREPLACE( // Every other row...
Data!F1:F&" | "&Data!G1:G&" | "&Data!H1:H&" | "&Data!I1:I, // ... iteratate over each column and concat with space-bar delimiter (' | ')
"^[| ]+()" + // ... case 1: match any leading space-bars, making $1 empty string
"|" + // ... or
"[| ]+()$" // ... case 2: match any trailing space-bars, making $2 empty string
"|" + // ... or
"( \| )[ |]+", // ... case 3: match any repeating space-bars, making $3 a single space-bar
"$3" // ... and replace with $3
) // ... in cases #1 or #2, $3 is emtpy string!
)
)
/*
Where columns F-I in sheet "Data" represent multiple choice information submitted via a Google Form
(and columns A-E are other information submitted)
parse it so that we end up with n rows where n is how many items were selected.
So if user submits a form and answers X, Y, end up with two rows, one containing X and another containing Y.
Creates an embedded array that fills down (semi-colons make new rows)
Each element in the container embedded array consists of an embedded arrays
which iterates through each column F-I and only displays those columns that are not blank.
Also see sort, using =sort({..}, 1, True)
*/
={
FILTER({Data!A1:E, Data!F1:F}, NOT(ISBLANK(Data!F1:F)));
FILTER({Data!A1:E, Data!G1:G}, NOT(ISBLANK(Data!G1:G)));
FILTER({Data!A1:E, Data!H1:H}, NOT(ISBLANK(Data!H1:H)));
FILTER({Data!A1:E, Data!I1:I}, NOT(ISBLANK(Data!I1:I)))
}
=SORT({
FILTER({Data!A1:E, Data!F1:F}, NOT(ISBLANK(Data!F1:F)));
FILTER({Data!A1:E, Data!G1:G}, NOT(ISBLANK(Data!G1:G)));
FILTER({Data!A1:E, Data!H1:H}, NOT(ISBLANK(Data!H1:H)));
FILTER({Data!A1:E, Data!I1:I}, NOT(ISBLANK(Data!I1:I)))
}, 1, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment