简体   繁体   English

Excel 到 TimeSlips 导入 - 用 CR 替换 CRLF 然后用 CRLF 替换 LF 的脚本

[英]Excel to TimeSlips import - Script to replace CRLF with CR then LF with CRLF

Summary: Unless there is a way to 'Save as' from Excel to this format, I want to click on a script that modifies a text file by replacing all CRLF (Carriage Return Line Feeds) with CR and after that replaces all LF with CRLF.摘要:除非有办法将 Excel 中的“另存为”转换为这种格式,否则我想单击一个脚本,通过将所有 CRLF(回车换行符)替换为 CR 来修改文本文件,然后将所有 LF 替换为 CRLF .

Situation: I have an Excel file that I save as a Tab Delimited Text file [Text (Tab delimited) (*.txt)].情况:我有一个 Excel 文件,我将其另存为制表符分隔的文本文件 [文本(制表符分隔)(*.txt)]。 I then import that text file into TimeSlips.然后我将该文本文件导入 TimeSlips。 Example text in linked image to show line breaks .链接图像中的示例文本以显示换行符

For the import to correctly import the description data with line breaks, I need to first open the text file in Notepad++ and replace all the line breaks as mentioned above.为了导入正确导入带换行符的描述数据,我需要先在 Notepad++ 中打开文本文件并替换上面提到的所有换行符。

I must replace the CRLF with CR before replacing the LF because if I find/replace LF first, it will alter the CRLF to CRCRLF.在替换 LF 之前,我必须用 CR 替换 CRLF,因为如果我先找到/替换 LF,它会将 CRLF 更改为 CRCRLF。

I'm a novice at scripts.我是脚本新手。 I looked at some examples of this and couldn't really wrap my head around what I found.我看了一些这样的例子,并不能真正理解我的发现。

Like:喜欢:

@echo off

setlocal EnableDelayedExpansion

set LF=^
%Don't remove%
%these lines%
set "EOL=!LF!" & set "EOL2=!LF!"

for /F %%a in (test.txt) do (
   if %%a equ PROP-SUMMARY set "EOL=!LF!"
   set /P "=%%a!EOL!" < NUL
   set "EOL0=!EOL!" & set "EOL=!EOL2!" & set "EOL2=!EOL0!"
   if %%a equ PROP-VALUES set "EOL=,"
)

from: I want to replace carriage returns with commas in batch from: 我想批量用逗号替换回车

Test Data: enter image description here测试数据:在此处输入图像描述

I created a PowerShell Script that works nicely.我创建了一个运行良好的 PowerShell 脚本。

$original_file ='C:\Users\ItsMeMario\Downloads\TESTING.txt'
$text = [IO.File]::ReadAllText($original_file) -replace "`r`n", "`r"
[IO.File]::WriteAllText($original_file, $text)
$text = [IO.File]::ReadAllText($original_file) -replace "`n", "`r`n"
[IO.File]::WriteAllText($original_file, $text)

And I'm now working on an Excel VBA macro.我现在正在研究 Excel VBA 宏。 :/ :/

Created an Excel VBA macro script to export the sheet and remove the line breaks.创建了一个 Excel VBA 宏脚本来导出工作表并删除换行符。

I know the code could be consolidated a little bit, but that's beyond my current skill level.我知道代码可以稍微整合一下,但这超出了我目前的技能水平。 Any help would be welcome.欢迎任何帮助。

Sub TSexport()
'
' Macro1 Macro
'
'
        Dim sBuf As String
        Dim sTemp As String
        Dim iFileNum As Integer
        Dim sFileName As String

    ' Edit as needed
    sFileName = "C:\Users\ItsMeMario\Downloads\TSimport.txt"

    'This code exports the 'TSimport' sheet to a tab delimited txt file.
        Sheets("TSimport").Select
        Sheets("TSimport").Copy
        ActiveWorkbook.ServerViewableItems.DeleteAll
        ActiveWorkbook.ServerViewableItems.Add ActiveWorkbook.Sheets("TSimport")
        ActiveWorkbook.SaveAs Filename:=sFileName _
            , FileFormat:=xlText, CreateBackup:=False
        ActiveWindow.Close
        

    'This code replaces the line breaks CRLF (Chr(13) & Chr(10)) with CR (Chr(13)) in the exported txt file.
    iFileNum = FreeFile
    Open sFileName For Input As iFileNum

    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        sTemp = sTemp & sBuf & vbCrLf
    Loop
    Close iFileNum

    sTemp = Replace(sTemp, Chr(13) & Chr(10), Chr(13))

    iFileNum = FreeFile
    Open sFileName For Output As iFileNum
    Print #iFileNum, sTemp
    Close iFileNum


    'This code replaces the line breaks LF (Chr(10)) with CR (Chr(13) & Chr(10)) in the exported txt file.
    iFileNum = FreeFile
    Open sFileName For Input As iFileNum

    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        sTemp = sTemp & sBuf & vbCrLf
    Loop
    Close iFileNum

    sTemp = Replace(sTemp, Chr(10), Chr(13) & Chr(10))

    iFileNum = FreeFile
    Open sFileName For Output As iFileNum
    Print #iFileNum, sTemp
    Close iFileNum

    'Add code to update rows from 'ready to export' to 'exported'.

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM