Forum Discussion
Getting "The name 'Html.Table' does not exist in the current context" in my custom connector project
- 5 years ago
I looked into the power bi code (ILSpy is great 🙂 and figured out that Html.Tables is a built in function, not a Power Query function exactly. It uses AngleSharp.net under the covers to do the css selectors ect. basically the heavy lifting of the html manipulation / searching.
The following was useful for me, and could be extended to do all the html.table function - it's pretty hacky but got the job done 🙂
MyHtml.Table = (html as text, selectorRecord as any, optional three as any) as any => let columnHeader = selectorRecord{0}{0}, selectorList = List.Transform(Text.Split(selectorRecord{0}{1}, ">"), each Text.Trim(_)), eachSelect = List.Accumulate(selectorList, html, (htmlstring, c) => let selector = if Text.Contains(c, ":") then Text.BeforeDelimiter(c, ":") else c, hasColonSelector = Text.Contains(c, ":"), startTag = "<" & selector, endTag = "</" & selector in if (List.Last(selectorList) = c) then List.Transform(List.RemoveFirstN(Text.Split(htmlstring, startTag), 1), each Text.BeforeDelimiter(Text.AfterDelimiter(_, ">"), endTag)) else if Text.Contains(htmlstring, startTag) then Text.AfterDelimiter( if hasColonSelector then if Text.Contains(c, ":nth-of-type") then if Text.Contains(c, ":not") then let // hacky nthOfTypeNumber = try Number.FromText(Text.BetweenDelimiters(Text.Replace(c, ":not(", ""), "(", ")")) otherwise 0, notValue = Text.BetweenDelimiters(htmlstring, startTag, endTag, if Number.IsNaN(nthOfTypeNumber) then 0 else nthOfTypeNumber - 1), thisValue = Text.Combine( Text.Split(Text.Replace(htmlstring, notValue, ""), startTag) ) in startTag & thisValue else let nthOfTypeNumber = try Number.FromText(Text.BetweenDelimiters(c, "(", ")")) otherwise 0, notValue = Text.BetweenDelimiters(htmlstring, startTag, endTag, if Number.IsNaN(nthOfTypeNumber) then 0 else nthOfTypeNumber - 1) in startTag & notValue else Text.BetweenDelimiters(htmlstring, startTag, endTag) else Text.AfterDelimiter(Text.BeforeDelimiter(htmlstring, endTag), startTag) , ">") else htmlstring ) in #table({columnHeader}, List.Transform(eachSelect, each let htmlReplacements = { { "<br />", Character.FromNumber(13) }, { " ", " " } }, value = List.Accumulate(htmlReplacements, _, (s, c) => Text.Trim(Text.Replace(s, c{0}, c{1}))) in { value } ));
Hi!
No, I did not find out why and could go on without that code.
BR
I looked into the power bi code (ILSpy is great 🙂 and figured out that Html.Tables is a built in function, not a Power Query function exactly. It uses AngleSharp.net under the covers to do the css selectors ect. basically the heavy lifting of the html manipulation / searching.
The following was useful for me, and could be extended to do all the html.table function - it's pretty hacky but got the job done 🙂
MyHtml.Table = (html as text, selectorRecord as any, optional three as any) as any =>
let
columnHeader = selectorRecord{0}{0},
selectorList = List.Transform(Text.Split(selectorRecord{0}{1}, ">"), each Text.Trim(_)),
eachSelect = List.Accumulate(selectorList, html, (htmlstring, c) =>
let
selector = if Text.Contains(c, ":") then Text.BeforeDelimiter(c, ":") else c,
hasColonSelector = Text.Contains(c, ":"),
startTag = "<" & selector,
endTag = "</" & selector
in
if (List.Last(selectorList) = c) then
List.Transform(List.RemoveFirstN(Text.Split(htmlstring, startTag), 1), each Text.BeforeDelimiter(Text.AfterDelimiter(_, ">"), endTag))
else
if Text.Contains(htmlstring, startTag) then
Text.AfterDelimiter(
if hasColonSelector then
if Text.Contains(c, ":nth-of-type") then
if Text.Contains(c, ":not") then
let
// hacky
nthOfTypeNumber = try Number.FromText(Text.BetweenDelimiters(Text.Replace(c, ":not(", ""), "(", ")")) otherwise 0,
notValue = Text.BetweenDelimiters(htmlstring, startTag, endTag, if Number.IsNaN(nthOfTypeNumber) then 0 else nthOfTypeNumber - 1),
thisValue = Text.Combine( Text.Split(Text.Replace(htmlstring, notValue, ""), startTag) )
in
startTag & thisValue
else
let
nthOfTypeNumber = try Number.FromText(Text.BetweenDelimiters(c, "(", ")")) otherwise 0,
notValue = Text.BetweenDelimiters(htmlstring, startTag, endTag, if Number.IsNaN(nthOfTypeNumber) then 0 else nthOfTypeNumber - 1)
in
startTag & notValue
else
Text.BetweenDelimiters(htmlstring, startTag, endTag)
else
Text.AfterDelimiter(Text.BeforeDelimiter(htmlstring, endTag), startTag)
, ">")
else
htmlstring
)
in
#table({columnHeader}, List.Transform(eachSelect, each
let
htmlReplacements = {
{ "<br />", Character.FromNumber(13) },
{ " ", " " }
},
value = List.Accumulate(htmlReplacements, _, (s, c) => Text.Trim(Text.Replace(s, c{0}, c{1})))
in
{ value }
));
- Anonymous5 years agoNot applicable
Awesome! Thanks for the insights! 😀