-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateTable2
61 lines (59 loc) · 2.26 KB
/
CreateTable2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(inputTable as table) as text =>
let
source = Table.Schema(inputTable)
,sortRows = Table.Sort(source,{{"Position", Order.Ascending}})
,simplifyTypeNameRec =
[#"Any.Type" = "any"
,#"Binary.Type" = "binary"
,#"Date.Type" = "date"
,#"DateTime.Type" = "datetime"
,#"DateTimeZone.Type" = "datetimezone"
,#"Duration.Type" = "duration"
,#"Function.Type" = "function"
,#"List.Type" = "list"
,#"Logical.Type" = "logical"
,#"None.Type" = "none"
,#"Null.Type" = "null"
,#"Number.Type" = "number"
,#"Record.Type" = "record"
,#"Table.Type" = "table"
,#"Text.Type" = "text"
,#"Time.Type" = "time"
,#"Type.Type" = "type"]
,simplifyTypeNames = Table.TransformColumns(sortRows,{{"TypeName", each Record.FieldOrDefault(simplifyTypeNameRec,_,_)}})
,selectColumns = Table.SelectColumns(simplifyTypeNames,{"Name", "TypeName","IsNullable"})
,addColOfTypeNames =
Table.AddColumn(
selectColumns
,"TypeNames"
,each
Expression.Identifier([Name])
& " = "
& (if [IsNullable] then "nullable " else "")
& [TypeName]
)
,tableTypeRec = "[" & Text.Combine(addColOfTypeNames[TypeNames], ", ") & "]"
//Code above is based on a function by Chris Webb
,tableOfExpressions = Table.TransformColumns(inputTable,{},Expression.Constant)
,listOfCols = Table.ToColumns(tableOfExpressions)
,listOfColsOfPaddedText =
List.Transform(
listOfCols
,(col as list)=>
let
buf = List.Buffer(col),
maxTextLength = List.Max(List.Transform(buf,Text.Length)),
paddedText = List.Transform(buf,each Text.PadEnd(_,maxTextLength))
in
paddedText
)
,listOfRowsOfPaddedText = List.Zip(listOfColsOfPaddedText)
,bodyAsList = List.Transform(listOfRowsOfPaddedText, (row as list)=> "{"&Text.Combine(row,",")&"}")
,bodyAsText = "{"&Text.Combine(bodyAsList,"#(lf),")&"#(lf)}"
,together = "#table(type table "
& tableTypeRec
& ",#(lf)"
& bodyAsText
& ")"
in
together