Finding answers on Web Apps SE or Reddit is wonderful, but if you don't understand them you can't modify them.
I recently came across the following as part of an answer:
={"Corrected List 2";ArrayFormula(IF(LEN(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(E2:E,"("&TEXTJOIN("|",1,FILTER("(?i)"&A2:A,LEN(A2:A)))&")"),REGEXREPLACE(E2:E,"("&TEXTJOIN("|",1,FILTER("(?i)"&A2:A,LEN(A2:A)))&")","♥$1♥"),)),,1E+100)))),SUBSTITUTE(SUBSTITUTE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IFERROR(VLOOKUP(IFERROR(SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(E2:E,"("&TEXTJOIN("|",1,FILTER("(?i)"&A2:A,LEN(A2:A)))&")"),SUBSTITUTE(REGEXREPLACE(E2:E,"("&TEXTJOIN("|",1,FILTER("(?i)"&A2:A,LEN(A2:A)))&")","♥$1♥")," ","♦"),)),,1E+100))),"♥",0,0)),{A2:A,B2:B},2,0),IFERROR(SPLIT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(E2:E,"("&TEXTJOIN("|",1,FILTER("(?i)"&A2:A,LEN(A2:A)))&")"),SUBSTITUTE(REGEXREPLACE(E2:E,"("&TEXTJOIN("|",1,FILTER("(?i)"&A2:A,LEN(A2:A)))&")","♥$1♥")," ","♦"),)),,1E+100))),"♥",0,0)))),,1E+100)))," ",""),"♦"," "),E2:E))}
My mental parser is in overload. It doesn't help that is is in effect self writing code, with string versions of what would otherwise be reserved words. and that it uses Google query.
The first step to understanding this is to tease it apart, and that, for me needs some kind of pretty printer. Google wasn't helpful.
One similar here: How can I pretty-print a formula in Google Sheets? is rather simplistic, is in VBA code and I don't thing understands the syntax overload of query.
The sheet was posted on reddit. https://docs.google.com/spreadsheets/d/1iMsMs8mPHFZIcBPV1vGzWEHd9RnO18VbdGwQ5HkR1ak/edit#gid=0
The formula is a green box Corrected List 2.
I asked a question there, that I had asked here.
This sheet was in response to this question:
More compact version of nested substitute function possible?
Best Answer
There isn't a "pretty print" built-in feature for Google Sheets formulas. IMHO opinion the simplest way to improve formula readability are those already described in How can I pretty-print a formula in Google Sheets? and How to build formula in Google Sheets using content of other cells:
If you edit the formula to introduce new breaklines / spaces, you should include and another innocuous change like
+0
*1
,&""
)a1
toA1
A1
to$A$1
or to$A1
or toA$1
If you edit the formula again, you could revert the innocuous change
Another alternative, if you feel confortable with Google Apps Script editor to write long strings, you could write your formulas using it, and then use Google Apps Script function to add the formula to the spreadsheet (
setFormula(formula)
/setFormulas(formula[][])
).One more alternative is to use a "pretty-print" tool for Excel formulas. The best-one will be one that is able to work with custom functions, so a Google Sheets functions that aren't supported by Excel could be treated as custom functions.
Some tools for Excel were suggested in SO: Pretty Print Excel Formulas?. Some time ago I tried at least one of them; I don't remember how many. At that time I didn't find one that fits my needs.
I "ported" the code in an answer from VBA to Google Apps Script. The prettify function returns a formula with a breakline after each curly bracket, parentheses and after operators like
+
,-
,*
,/
,^
,&
. The test function takes the formula of the current cell and add the prettified version to the first cell to the right.NOTE:
tabOffset++
is commented out because it cause that prettified version of the formula in the question exceeds the 50000 character length cell's limit.String.prototype.repeat
is a method introduced in ECMAScript 6 and it's not natively supported but you could add the polyfill in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat1E+10
notation that should be manually fixed.Anyway,
Using very large / convoluted formulas should be avoided whenever it be possible because:
So, if you are really needing a tool for pretty-printing formulas, you should ask yourself if you are using the best approach to do what your formula does.