0

我想清理表格的列标题,以便我的列标题具有如下名称:

  • [空格][空格][空格]名字[空格][空格]
  • [空格]MaintActType[空格]
  • 东元日期[空格]
  • FIN 日期
  • ABC指标
  • 销货成本
  • 创建于

我想要的列标题名称如下:

  • 主要行为类型
  • 东元日期
  • FIN 日期
  • ABC指标
  • 销货成本
  • 创建于

我的代码如下:

let
    Source = Excel.Workbook(File.Contents("C:\RawData\sample.xlsx"), null, true),
    #"sample_Sheet" = Source{[Item="sample",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(#"sample_Sheet", [PromoteAllScalars=true]),
    #"Trim ColumnSpace" = Table.TransformColumnNames(#"Promoted Headers", Text.Trim),
    #"Split CapitalLetter" = Table.TransformColumnNames(#"Trim ColumnSpace", each Text.Combine(Splitter.SplitTextByPositions(Text.PositionOfAny(_, {"A".."Z"},2)) (_), " ")),
    #"Remove DoubleSpace" = Table.TransformColumnNames(#"Split CapitalLetter", each Replacer.ReplaceText(_, "  ", " ")),
    #"Capitalise FirstLetter" = Table.TransformColumnNames(#"Remove DoubleSpace", Text.Proper),
    #"Remove Space" = Table.TransformColumnNames(#"Capitalise FirstLetter", each Text.Remove(_, {" "})),
    #"Separate ColumnName" = Table.TransformColumnNames(#"Remove Space", each Text.Combine(Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}) (_), " "))
in
    #"Separate ColumnName"

但是,我得到如下结果。这不是我想要的,因为我们组合在一起的所有大写字母。如何更改代码以便获得所需的结果?我真的很感谢你的帮助,拜托。

  • 主要行为类型
  • TECDate
  • 查找日期
  • ABC指标
  • 销货成本
  • 创建于

或者,我将代码更改为:

let
   Source = Excel.Workbook(File.Contents("C:\RawData\sample.xlsx"), null, true),
   #"sample_Sheet" = Source{[Item="sample",Kind="Sheet"]}[Data],
   #"Promoted Headers" = Table.PromoteHeaders(#"sample_Sheet", [PromoteAllScalars=true]),
   #"Trim ColumnSpace" = Table.TransformColumnNames(Input, Text.Trim),
   #"Separate ColumnName" = Table.TransformColumnNames(#"Trim ColumnSpace", each Text.Combine(Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}) (_), " ")),
   #"Capitalise FirstLetter" = Table.TransformColumnNames(#"Separate ColumnName", Text.Proper)    
in
   #"Capitalise FirstLetter"

不幸的是,它返回的结果如下:

  • 主要行为类型
  • 特科日期
  • 结束日期
  • Abc 指标
  • 销货成本
  • 创建于

我不知道如何玩代码了。

4

1 回答 1

0

一种方法是用一些东西标记现有空间(我使用“ZZZ”)并将它们恢复到最后的空间。这是您的代码,并进行了一些调整。感谢您的代码。我试图做 Text.Proper 并且你的样本帮助了我!

let
    Source = Input,
    #"Replaced Value" = Table.ReplaceValue(Source,"[Space]"," ",Replacer.ReplaceText,{"Headers"}),
    #"Transposed Table" = Table.Transpose(#"Replaced Value"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Trim ColumnSpace" = Table.TransformColumnNames(#"Promoted Headers", Text.Trim),
    #"Change space to ZZZ" = Table.TransformColumnNames(#"Trim ColumnSpace", each Replacer.ReplaceText(_, " ", " ZZZ ")), 
    #"Split CapitalLetter" = Table.TransformColumnNames(#"Change space to ZZZ", each Text.Combine(Splitter.SplitTextByPositions(Text.PositionOfAny(_, {"A".."Z"},2)) (_), " ")),
    #"Capitalise FirstLetter" = Table.TransformColumnNames(#"Split CapitalLetter", Text.Proper),
    #"Remove Space" = Table.TransformColumnNames(#"Capitalise FirstLetter", each Text.Remove(_, {" "})),
    #"Separate ColumnName" = Table.TransformColumnNames(#"Remove Space", each Text.Combine(Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}) (_), " ")),
    #"Change ZZZ to space" = Table.TransformColumnNames(#"Separate ColumnName", each Replacer.ReplaceText(_, "ZZZ", " ")), 
    #"Remove DoubleSpace" = Table.TransformColumnNames(#"Change ZZZ to space", each Replacer.ReplaceText(_, "  ", " "))
in
    #"Remove DoubleSpace"
于 2021-09-15T19:48:07.123 回答