gpt4 book ai didi

excel - Shell 脚本 - 将 Excel (xlsx) 转换为 CSV - 删除空格/制表符空间

转载 作者:行者123 更新时间:2023-12-04 18:34:16 25 4
gpt4 key购买 nike

我为我的项目收到带有多张工作表的 excel 文件 (xslx)。这些工作表上的记录数量从每张工作表 15k 到 70k 不等。我需要对此数据执行以下任务,然后将其转换为 CSV。或转换为 CSV,然后以任何一种方式处理数据。

输入示例:

call_no  uniq_no  Type  Strength    Description
2456 15 TX SomeSting SomeSting
5263 15 BLL SomeSting SomeSting
4263 162 TX SomeSting
2369 215 LH SomeSting
4269 426 BLL SomeSting SomeSting
7412 162 TX SomeSting SomeSting

根据我需要的要求
  • 在“uniq_no”列中查找重复值,并删除除原始记录(第一条记录)之外的所有重复记录。
  • 用数据替换空白。 (只需简单查找空白并用值逻辑替换)
  • 删除任何单元格中的空格/制表符空格。 (这点不重要,就像一个支线任务)

  • 输出示例:
    call_no  uniq_no  Type  Strength    Description
    2456 15 TX SomeSting SomeSting
    4263 162 TX **NewDATA** SomeSting
    2369 215 LH SomeSting **NewDATA**
    4269 426 BLL SomeSting SomeSting

    这对我来说是例行任务。我对 shell 脚本有相当的了解。因此,如果有人可以指导我,即使是粗略的脚本大纲,我也可以在最后进行调整。请帮忙。

    最佳答案

    Update: the desired platform for the script has been clarified and a response is no longer applicable. However, I will leave this response here in case a future viewer of this question stumbles upon it and finds it useful. Anyone writing a shell script in a Ubuntu language may be able to port over some aspects of this vbscript as well.



    这里有一些东西可以帮助您入门。如果您使用 Excel 的宏记录器记录操作,请记住在 VBS 中使用相同的命令意味着您必须摆脱所有命名参数。

    prep_xlsx.vbs
    Set objExcel = WScript.CreateObject ("Excel.Application")

    objExcel.Visible = true 'False 'True for testing

    strFileName = "c:\tmp\vbs_test.xlsx"

    set objWb = objExcel.WorkBooks.open(strFileName)
    set objWs = objWb.Worksheets(1)

    with objWs
    with .cells(1, 1).CurrentRegion
    .Cells.SpecialCells(4) = "**NewDATA**" ' 4 is xlCellTypeBlanks
    .Cells.RemoveDuplicates 2, 1 ' Columns:=2, Header:=xlYes
    for c = 1 to .Columns.Count
    with .columns(c)
    .TextToColumns .Cells(1), 2 ', Array(0, 1) 'Range("C1"), DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
    end with
    next 'next c
    end with
    end with

    objWb.Close True 'save on close

    objExcel.Quit
    Set objExcel = Nothing

    应该注意的是,使用 Range.TextToColumns method 删除前导/尾随空格。如果前导空格太多,使用 xlFixedWidth 可以尝试将列拆分为两列。目前,这将停止该过程,因为它将要求确认覆盖下一列的值(您不想这样做)。必须有大量空格才能让 Excel 猜测它属于两列,因此除非空格比典型单词多,否则无需担心;只是需要注意的事情。例如如果 D6 中有两倍多的前导空格,它可能希望拆分为两列。

    vbscript_before prep_xlsx.vbs 之前的 vbs_test.xlsx

    vbscript_after vbs_test.xlsx 之后 prep_xlsx.vbs

    关于excel - Shell 脚本 - 将 Excel (xlsx) 转换为 CSV - 删除空格/制表符空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35601120/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com