Power Query - 按可变字段长度拆分列 - 考虑空值

Power Query - split column by variable field lengths - account for null values

另一个 Power Query 提供了一种解决方案,可以根据列字符计数宽度将字符分隔的文本文件拆分成列。

但它不考虑空值。当遇到空值时,它会在右侧的一列中给出错误。我不能确切地说出发生了什么。错误是

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

然后,在您的 table 中添加一个使用此公式的自定义列:

each SplitText([Column1], {4, 2, 5, 3})

我正在使用 Excel 2010 64 位和 Power Query 版本:2.29.4217.1861

如何修改它以解决空值问题?

Split = List.Generate(() => {0, 0}, each _{0} < LengthsCount, each {_{0} + 1, _{1} + lengths{_{0}}}, each try Text.Range(text, _{1}, lengths{_{0}}) otherwise null)