smozgur
BatCoder
- Joined
- Feb 28, 2002
- Messages
- 2,656
TOC2HTML creates the table of contents HTML code by transforming the provided list to an HTML list element to be used in the book's HTML page.
I usually create the HTML codes for the table of contents of the book pages to be published in the MrExcel Bookstore by using the list that @MrExcel sends me in a worksheet. Although it is possible to use regex101.com online tool with a few simple patterns, it takes a few steps to complete the transformation with Regex. Therefore, I wrote a user-defined function (UDF) in VBA to create the HTML code for the table of contents in a single step.
The following shows how I converted the UDF named TOC to a recursive Lambda function named TOC2HTML. You can find the UDF at the bottom of this post to compare the processes.
The following shows the result of the produced code in the browser.
The VBA user-defined function. Although it is not necessary, I used indents and line breaks in the UDF version. It could be easily implemented in the Lambda version as well.
I usually create the HTML codes for the table of contents of the book pages to be published in the MrExcel Bookstore by using the list that @MrExcel sends me in a worksheet. Although it is possible to use regex101.com online tool with a few simple patterns, it takes a few steps to complete the transformation with Regex. Therefore, I wrote a user-defined function (UDF) in VBA to create the HTML code for the table of contents in a single step.
The following shows how I converted the UDF named TOC to a recursive Lambda function named TOC2HTML. You can find the UDF at the bottom of this post to compare the processes.
Excel Formula:
=LAMBDA(rng,chapterKeyword,i,
LET(rows, ROWS(rng),
IF(i = rows,
"",
LET(i, i+1,
cll, INDEX(rng,i),
isFirstLevel, IFERROR(FIND(chapterKeyword, cll),0),
IF(isFirstLevel,
IF(i=1,
"<ul>","</ul></li>") & "<li><p>" & cll & "</p><ul>",
"<li>" & cll & "</li>"
)
& TOC2HTML(rng, chapterKeyword, i)
& IF(i = rows - 1,
"</ul></li></ul>",
""
)
)
)
)
)
Cell Formulas | ||
---|---|---|
Range | Formula | |
B2 | B2 | =TOC2HTML(toc_list,"Chapter ", ) |
Named Ranges | ||
---|---|---|
Name | Refers To | Cells |
toc_list | ='Continuing Financial Modelling'!$A$3:$A$56 | B2 |
The following shows the result of the produced code in the browser.
HTML result
The VBA user-defined function. Although it is not necessary, I used indents and line breaks in the UDF version. It could be easily implemented in the Lambda version as well.
VBA Code:
Function TOC(rng As Range, firstLevel As String)
Dim cll As Range
Dim i As Integer
For i = 1 To rng.Rows.Count
Set cll = rng.Cells(i)
If InStr(cll.Value, "Chapter ") Then
If i = 1 Then
TOC = "<ul>"
Else
TOC = TOC & vbCrLf & String(2, vbTab) & "</ul>" & vbCrLf & vbTab & "</li>"
End If
TOC = TOC & vbCrLf & vbTab & "<li>" & vbCrLf & String(2, vbTab) & "<p>" & cll.Value & "</p>" & vbCrLf & String(2, vbTab) & "<ul>"
Else
TOC = TOC & vbCrLf & String(3, vbTab) & "<li>" & cll.Value & "</li>"
End If
If i = rng.Rows.Count Then
TOC = TOC & vbCrLf & String(2, vbTab) & "</ul>" & vbCrLf & vbTab & "</li>" & vbCrLf & "</ul>"
End If
Next i
End Function
Upvote
0