PRETTYPRINT

=PRETTYPRINT(cellRefOrTxt, indent)

cellRefOrTxt
either a cell reference with a formula or a formula text string
indent
an integer corresponding to indentation spacing

PRETTYPRINT inserts new lines and indents based on depth with respect to opening and closing bracket characters (parentheses () and curly braces {}) and commas.

tboulden

Board Regular
Joined
Jan 14, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
PRETTYPRINT inserts new lines and indents based on depth with respect to opening and closing bracket characters (parentheses () and curly braces {}) and commas.

PrettyPrint's output displays best with a monospace font like Lucida Console in Excel.

Note that this is a naive implementation as I'm not trying to account for whether opening and closing brackets are inside of double-quotes (as happens in the definition of the LAMBDA and can be seen in the XL2BB snippet below). Also note this could all be done in a LET statement; the cumTot LAMBDA is just to save space here. The XL2BB snippet is the output of this code on its own definition.

I have an improvement basically ready to go, however my helper LAMBDA is misbehaving in a yet unaccounted for manner; will include the improvement if I figure out the issue.

Excel Formula:
=LAMBDA(
    cellRefOrTxt,
    indent,
    LET(
        formTxt,
        IF(
            ISTEXT(cellRefOrTxt),
            cellRefOrTxt,
            FORMULATEXT(cellRefOrTxt)
        ),
        strLen,LEN(formTxt),
        seq,SEQUENCE(strLen),
        cumTot,
        LAMBDA(
            list,
            MMULT(
                TRANSPOSE(
                    (seq<=TRANSPOSE(seq))*list
                ),
                SIGN(list)
            )
        ),
        strToArr,MID(formTxt,seq,1),
        isLBr,--((strToArr="("+(strToArr="{")),
        isRBr,--((strToArr=")")+(strToArr="}")),
        isComma,--(strToArr=","),
        newLine,isLBr+isRBr+isComma,
        cumLBr,cumTot(isLBr),
        cumRBr,-cumTot(isRBr),
        tabCt,cumLBr+cumRBr,
        whiteSpace,
        IF(
            newLine,CHAR(10),
            ""
        )&REPT(" ",indent*newLine*tabCt),
        concArr,
        IF(
            isRBr,whiteSpace,
            ""
        )&strToArr&
        IF(
            isLBr+isComma,whiteSpace,
            ""
        ),
        pretty,TEXTJOIN("",FALSE,concArr),
        pretty
    )
)

Cell Formulas
RangeFormula
DS3DS3=LAMBDA(cellRefOrTxt,indent,LET(formTxt,IF(ISTEXT(cellRefOrTxt),cellRefOrTxt,FORMULATEXT(cellRefOrTxt)),strLen,LEN(formTxt),seq,SEQUENCE(strLen),cumTot,LAMBDA(list,MMULT(TRANSPOSE((seq<=TRANSPOSE(seq))*list),SIGN(list))),strToArr,MID(formTxt,seq,1),isLBr,--((strToArr="(")+(strToArr="{")),isRBr,--((strToArr=")")+(strToArr="}")),isComma,--(strToArr=","),newLine,isLBr+isRBr+isComma,cumLBr,cumTot(isLBr),cumRBr,-cumTot(isRBr),tabCt,cumLBr+cumRBr,whiteSpace,IF(newLine,CHAR(10),"")&REPT(" ",indent*newLine*tabCt),concArr,IF(isRBr,whiteSpace,"")&strToArr&IF(isLBr+isComma,whiteSpace,""),pretty,TEXTJOIN("",FALSE,concArr),pretty))
DQ5DQ5=PrettyPrint.Naive(DS3,4)
 
Upvote 0
Re-oriented from cell DS3 to cell A1 per @smozgur suggestion in a different thread. Oops!

Cell Formulas
RangeFormula
C1C1=LAMBDA(cellRefOrTxt,indent,LET(formTxt,IF(ISTEXT(cellRefOrTxt),cellRefOrTxt,FORMULATEXT(cellRefOrTxt)),strLen,LEN(formTxt),seq,SEQUENCE(strLen),cumTot,LAMBDA(list,MMULT(TRANSPOSE((seq<=TRANSPOSE(seq))*list),SIGN(list))),strToArr,MID(formTxt,seq,1),isLBr,--((strToArr="(")+(strToArr="{")),isRBr,--((strToArr=")")+(strToArr="}")),isComma,--(strToArr=","),newLine,isLBr+isRBr+isComma,cumLBr,cumTot(isLBr),cumRBr,-cumTot(isRBr),tabCt,cumLBr+cumRBr,whiteSpace,IF(newLine,CHAR(10),"")&REPT(" ",indent*newLine*tabCt),concArr,IF(isRBr,whiteSpace,"")&strToArr&IF(isLBr+isComma,whiteSpace,""),pretty,TEXTJOIN("",FALSE,concArr),pretty))
A3A3=PrettyPrint.Naive(C1,4)
 
This is a bit more opaque, although not being recursive, so commented lines below:
Excel Formula:
=LAMBDA(
    cellRefOrTxt,
    indent,                                        // # of spaces for each indent
    LET(
        formTxt,                                   // gets ONLY text from cellRefOrTxt
        IF(
            ISTEXT(cellRefOrTxt),
            cellRefOrTxt,
            FORMULATEXT(cellRefOrTxt)
        ),
        strLen,LEN(formTxt),
        seq,SEQUENCE(strLen),
        cumTot,                                   // creates a 1-d array with running total
        LAMBDA(                                   \\ of values from top-to-bottom
            list,
            MMULT(
                TRANSPOSE(
                    (seq<=TRANSPOSE(seq))*list
                ),
                SIGN(list)
            )
        ),
        strToArr,MID(formTxt,seq,1),              // create array to perform logical tests
        isLBr,--((strToArr="("+(strToArr="{")),   // check if char is a left bracket
        isRBr,--((strToArr=")")+(strToArr="}")),  // check if char is a right bracket
        isComma,--(strToArr=","),                 // check if char is a comma
        newLine,isLBr+isRBr+isComma,              // new line if either 3; + works like OR here
        cumLBr,cumTot(isLBr),                     // running count of all opening (+1) brackets
        cumRBr,-cumTot(isRBr),                    // running count of all closing (-1) brackets
        tabCt,cumLBr+cumRBr,                      // opening count - closing count = # of tabs
        whiteSpace,
        IF(
            newLine,CHAR(10),                     // insert new line if bracket or comma
            ""
        )&REPT(" ",indent*newLine*tabCt),         // insert required # of spaces base on depth
        concArr,
        IF(
            isRBr,whiteSpace,                     // whitespace goes BEFORE closing bracket
            ""
        )&strToArr&
        IF(
            isLBr+isComma,whiteSpace,             // whitespace goes AFTER opening bracket or comma
            ""
        ),
        pretty,TEXTJOIN("",FALSE,concArr),
        pretty
    )
)
 

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top