此其他Power Query question and answer提供了一种解决方案,可根据列字符计数宽度将字符分隔的文本文件拆分为多个列。
但它没有考虑空值。遇到null值时,它会在右侧的一列中显示错误。我不能确切地说出发生了什么。错误是
An error occurred in the ‘SplitText’ query. Expression.Error: The 'count' argument is out of range.
分割功能的代码是:
let
SplitText = (text, lengths) =>
let
LengthsCount = List.Count(lengths),
// Keep track of the index in the lengths list and the position in the text to take the next characters from. Use this information to get the next segment and put it into a list.
Split = List.Generate(() => {0, 0}, each _{0} < LengthsCount, each {_{0} + 1, _{1} + lengths{_{0}}}, each Text.Range(text, _{1}, lengths{_{0}}))
in
Split,
// Convert the list to a record to
ListToRecord = (text, lengths) =>
let
List = SplitText(text, lengths),
Record = Record.FromList(List, List.Transform({1 .. List.Count(List)}, each Number.ToText(_)))
in
Record
in
ListToRecord
然后,在表格中添加使用此公式的自定义列:
each SplitText([Column1], {4, 2, 5, 3})
我正在使用Excel 2010 64位和Power Query版本:2.29.4217.1861
如何修改此项以说明空值?
答案 0 :(得分:1)
Split = List.Generate(() => {0, 0}, each _{0} < LengthsCount, each {_{0} + 1, _{1} + lengths{_{0}}}, each try Text.Range(text, _{1}, lengths{_{0}}) otherwise null)