我的解释如下。任何评论表示赞赏(甚至是其他想法)。
类型是值的分类。有两种风格:原始类型(数字、文本等)和自定义类型,例如特定的表格类型或记录类型。
例如,表类型是列名、类型和任何键值的集合。可以先指定表类型,然后在创建表时使用:
Tabeltype = type table[Key = number, Value = text],
TabletypeWithKey = Type.AddTableKey(Tabeltype,{"Key"},true),
TableWithKey = #table(TabletypeWithKey, {{1, "A"},{2, "B"}, {3, "C"}})
同样,您可以创建记录类型。
但是,创建记录时不能直接使用记录类型。您可以使用 Value.ReplaceType 将类型“赋予”一个值,例如,将记录类型赋予记录,前提是该记录类型是封闭的并且没有可选字段。下面代码中的示例。
我希望有可能验证一个值是否与特定类型匹配,但这只能用原始类型来完成(使用关键字“is”或“as”或“Type.Is”)。
因此,根据我的解释,我创建了下面的代码来检查记录与记录类型的一致性:我不能保证它是完整的证明。目前它是一个查询,因此您可以查看正在发生的事情,但您可以轻松地将其转换为一个函数,并在当前注释掉的代码的下半部分使用示例。
// Mext 2 lines to be decommented to turn the code into a function
//let
// fnCheckConformity = (Record1 as record, RecordType as type) as logical =>
let
// Next 2 lines to be removed when turning this code into a function
Record1 = [x = 1, A = 3, B = 4],
RecordType = type [x = number, optional y = text,...],
RecordTypeFields = Type.RecordFields(RecordType),
ToTable = Record.ToTable(RecordTypeFields),
RecordTypeTable = Table.ExpandRecordColumn(ToTable, "Value", {"Optional", "Type"}, {"Optional", "Type"}),
RecordTable = Table.FromColumns({Record.FieldNames(Record1),Record.FieldValues(Record1)},{"Record FieldName", "Record FieldValue"}),
JoinedTable = Table.Join(RecordTypeTable, "Name", RecordTable, "Record FieldName", JoinKind.FullOuter),
ConformityCheck = Table.AddColumn(JoinedTable, "Conform",
each if [Optional] = null then Type.IsOpenRecord(RecordType) else
if [Optional] then true else
if [Record FieldValue] <> null then Value.Is([Record FieldValue], [Type]) else
false),
Result = List.AllTrue(ConformityCheck[Conform])
in
Result
// Add a comma after Result when turning the code above into a function
/* The code below can be used when turning the code above into a function.
// Examples:
OpenRecordType = type [x = number, optional y = text,...],
ClosedRecordType = type [x = number, y = text],
RecordA = [x = 1],
RecordB = [x = 1, A = 3, B = 4],
RecordC = [x = 1, y = "MarcelBeug"],
// RecordC is ascribed type ClosedRecordType:
RecordCTyped = Value.ReplaceType(RecordC, ClosedRecordType),
Conformity1 = fnCheckConformity(RecordA, OpenRecordType), // true
Conformity2 = fnCheckConformity(RecordA, ClosedRecordType), // false
Conformity3 = fnCheckConformity(RecordB, OpenRecordType), // true
Conformity4 = fnCheckConformity(RecordB, ClosedRecordType), // false
Conformity5 = fnCheckConformity(RecordC, OpenRecordType) // true
in
Conformity5 */