[英]Excel to TimeSlips import - Script to replace CRLF with CR then LF with CRLF
摘要:除非有辦法將 Excel 中的“另存為”轉換為這種格式,否則我想單擊一個腳本,通過將所有 CRLF(回車換行符)替換為 CR 來修改文本文件,然后將所有 LF 替換為 CRLF .
情況:我有一個 Excel 文件,我將其另存為制表符分隔的文本文件 [文本(制表符分隔)(*.txt)]。 然后我將該文本文件導入 TimeSlips。 鏈接圖像中的示例文本以顯示換行符。
為了導入正確導入帶換行符的描述數據,我需要先在 Notepad++ 中打開文本文件並替換上面提到的所有換行符。
在替換 LF 之前,我必須用 CR 替換 CRLF,因為如果我先找到/替換 LF,它會將 CRLF 更改為 CRCRLF。
我是腳本新手。 我看了一些這樣的例子,並不能真正理解我的發現。
喜歡:
@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: 我想批量用逗號替換回車
測試數據:在此處輸入圖像描述
我創建了一個運行良好的 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)
我現在正在研究 Excel VBA 宏。 :/
創建了一個 Excel VBA 宏腳本來導出工作表並刪除換行符。
我知道代碼可以稍微整合一下,但這超出了我目前的技能水平。 歡迎任何幫助。
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.