首页 > 解决方案 > 在 Excel 中将文本单元格拆分为 70 个字符块

问题描述

我有一个 Excel 文件,其中一个单元格包含记录的键,另一个单元格包含键的文本;信息来自 SQL Server 数据库。

文本单元格包括换行符和空白行,我需要根据需要将此单元格的内容拆分为 70 个字符的行。对于每一行,我需要使用相同的键值以及“行序”编号。关于文本,我需要保留整个单词,并尊重原始单元格中的白线和换行符。

下面是其中一个单元格的示例(A1 是关键单元格,A2 是文本单元格):

A1
ANUAL-LCD-FIX#0
A2
1-Limpieza 通用。
2-Revision de tornilleria en todo elequipo, reapretar de ser necesario。
3-Revision de pines, que no esten danados, reemplazar de ser necesario, (revisar con ingenieria)。
4-Revision de la pantalla, que este funcional y que no este golpeada。

在此处输入图像描述

拆分后,这是我需要得到的;请注意,需要创建 3 列(A、B 和 C)和 6 行(1..6):

A1
ANUAL-LCD-FIX#0
B1
01
C1
1-Limpieza 通用。
A2
ANUAL-LCD-FIX#0
B2
02
C2
2-Revision de tornilleria en todo elequipo,repretar de ser
A3 ANUAL
-LCD-FIX#0
B3
03
C3
necesario。
A4
ANUAL-LCD-FIX#0
B4
04
C4
3-Revision de pines,que no esten danados,reempazar de ser
A5 ANUAL
-LCD-FIX#0
B5
05
C5
necesario,(revisar con ingenieria)。
A6
ANUAL-LCD-FIX#0
B6
06
C6
4-Revision de la pantalla, que este funcional y que no este golpeada。

在此处输入图像描述

我在网上找到了一些拆分单元格的例子,但是被拆分单元格的长度是预先确定的,并且它们没有白线或换行符;就我而言,其中一些单元格少于 70 个字符,而另一些则更长,因此很难提前知道拆分每个文本单元格需要多少行。

谁能建议我如何做到这一点?如果需要更多信息或详细信息,请告诉我。

谢谢。

标签: excelsplitline-breakschunksblank-line

解决方案


您可以使用 Power Query 执行此操作

  • 数据/获取和转换/来自表/范围
  • 在换行符上拆分为行
  • 使用自定义函数添加列以在最多第 70 个字符前的空格处进行拆分。
  • 展开拆分列

M代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    //split by linefeed into new rows
    splitToRows = Table.ExpandListColumn(Table.TransformColumns(
        Source, {{"Text", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), 
        let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Text"),

    //split on maximum 70 characters but only whole words.
    #"Invoked Custom Function" = Table.AddColumn(splitToRows, "fnSplitOnSpace", each fnSplitOnSpace([Text], 70)),

    //remove unneeded column
    #"Removed Columns" = Table.RemoveColumns(#"Invoked Custom Function",{"Text"}),

    //rename column: Text
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"fnSplitOnSpace", "Text"}}),

    //expand list to new rows
    #"Expanded Text" = Table.ExpandListColumn(#"Renamed Columns", "Text")
in
    #"Expanded Text"

需要自定义函数。

  • 创建一个新的查询other sources/Blank Query
  • 重命名查询(属性/名称):fnSplitOnSpace

自定义功能的M 代码

//https://community.powerbi.com/t5/Desktop/Query-Editor-to-Split-the-Text-String/td-p/142110
//Marcel Beug

(TextString as text, LineLength as number) as list =>
    let
    fnWT = List.Generate(() => 
                  [TextPart      = if Text.Length(TextString) <= LineLength
                                   then TextString 
                                   else if Text.PositionOf(Text.Start(TextString,LineLength + 1)," ",Occurrence.Last) > -1
                                        then Text.Start(TextString,List.Min({LineLength + 1,Text.PositionOf(Text.Start(TextString,LineLength + 1)," ",Occurrence.Last)}))
                                        else Text.Start(TextString,List.Min({LineLength,Text.Length(TextString)})),
                   RemainingText = if Text.Length(TextString) <= LineLength
                                   then "" 
                                   else if Text.PositionOf(TextPart," ") > -1 
                                        then Text.Trim(Text.End(TextString,Text.Length(TextString)-Text.Length(TextPart)-1))
                                        else Text.Trim(Text.End(TextString,Text.Length(TextString)-Text.Length(TextPart)))],

                   each Text.Length([TextPart])>0,

                   each [TextPart      = if Text.Length([RemainingText]) <= LineLength
                                         then [RemainingText]
                                         else if Text.PositionOf(Text.Start([RemainingText],LineLength + 1)," ",Occurrence.Last) > -1
                                              then Text.Start([RemainingText],List.Min({LineLength + 1,Text.PositionOf(Text.Start([RemainingText],LineLength + 1)," ",Occurrence.Last)}))
                                              else Text.Start([RemainingText],List.Min({LineLength,Text.Length([RemainingText])})),
                         RemainingText = if Text.Length([RemainingText]) <= LineLength
                                         then ""
                                         else if Text.PositionOf(TextPart," ") > -1
                                              then Text.Trim(Text.End([RemainingText],Text.Length([RemainingText])-Text.Length(TextPart)-1))
                                              else Text.Trim(Text.End([RemainingText],Text.Length([RemainingText])-Text.Length(TextPart)))],

                   each [TextPart])
in
    fnWT

原来的

在此处输入图像描述

结果

在此处输入图像描述


推荐阅读