用 HTML 标签替换 Excel 文件中的斜体
Replace italics within Excel file with HTML tags
有没有办法用他们的标签替换斜体等格式,例如?更一般地说,我如何搜索 format:italic 并替换为
<i>foundstring(^&)</i>?
基本上我想要的是:
blabla bla bla
替换为:
blabla <i>bla</i> bla
这是因为我需要在 'preserving' 格式化时将 .xslx 导出为 .csv。 VBA 是否有适用于 Mac 的 MS Office 2011 脚本?还是可以用 Python?
在此先感谢您的帮助。
编辑:这是 vba 我试过但没有用的代码:
Sub Tag_Italic()
Dim oWS As Worksheet
Dim oRng As Range
Dim FirstUL
Set oWS = ActiveSheet
Application.FindFormat.Clear
Application.FindFormat.Font.Italic = True
Set oRng = oWS.Range("A1:A1000").Find(What:="", LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, SearchFormat:=True)
If Not oRng Is Nothing Then
FirstUL = oRng.Row
Do
oRng.Font.Italic = False
oRng.Value2 = "" & oRng.Value2 & ""
Set oRng = oWS.Range("A" & CStr(oRng.Row + 1) & ":A1000").Find(What:="", LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, SearchFormat:=True)
Loop While Not oRng Is Nothing
End If
End Sub
编辑 2:Gary 的学生的回答成功了!
不,CSV 严格删除所有格式。
CSV 代表逗号分隔值,通常用于 importing/exporting 数据库或进行备份。如果您要将 excel 文件转换为 CSV,所有格式标签都将被删除,包括 、table 边框、突出显示或字体颜色及其样式。数据以纯文本形式存储。
考虑以下 UDF():
Public Function TagMaker(r As Range) As String
Dim outpt As String, v As String, C As String
Dim i As Long, boo As Boolean
Dim booNext As Boolean
outpt = ""
v = r.Text
For i = 1 To Len(v)
boo = r.Characters(i, 1).Font.Italic
C = Mid(v, i, 1)
If (i = 1) And boo Then
outpt = "<i>"
End If
If i = Len(v) Then
If boo Then
outpt = outpt & C & "</i>"
Exit For
Else
outpt = outpt & C
Exit For
End If
End If
booNext = r.Characters(i + 1, 1).Font.Italic
If (boo And booNext) Or (Not boo And Not booNext) Then
outpt = outpt & C
End If
If boo And Not booNext Then
outpt = outpt & C & "</i>"
End If
If Not boo And booNext Then
outpt = outpt & C & "<i>"
End If
Next i
TagMaker = outpt
End Function
示例:
有没有办法用他们的标签替换斜体等格式,例如?更一般地说,我如何搜索 format:italic 并替换为
<i>foundstring(^&)</i>?
基本上我想要的是:
blabla bla bla
替换为:
blabla <i>bla</i> bla
这是因为我需要在 'preserving' 格式化时将 .xslx 导出为 .csv。 VBA 是否有适用于 Mac 的 MS Office 2011 脚本?还是可以用 Python?
在此先感谢您的帮助。
编辑:这是 vba 我试过但没有用的代码:
Sub Tag_Italic()
Dim oWS As Worksheet
Dim oRng As Range
Dim FirstUL
Set oWS = ActiveSheet
Application.FindFormat.Clear
Application.FindFormat.Font.Italic = True
Set oRng = oWS.Range("A1:A1000").Find(What:="", LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, SearchFormat:=True)
If Not oRng Is Nothing Then
FirstUL = oRng.Row
Do
oRng.Font.Italic = False
oRng.Value2 = "" & oRng.Value2 & ""
Set oRng = oWS.Range("A" & CStr(oRng.Row + 1) & ":A1000").Find(What:="", LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, SearchFormat:=True)
Loop While Not oRng Is Nothing
End If
End Sub
编辑 2:Gary 的学生的回答成功了!
不,CSV 严格删除所有格式。
CSV 代表逗号分隔值,通常用于 importing/exporting 数据库或进行备份。如果您要将 excel 文件转换为 CSV,所有格式标签都将被删除,包括 、table 边框、突出显示或字体颜色及其样式。数据以纯文本形式存储。
考虑以下 UDF():
Public Function TagMaker(r As Range) As String
Dim outpt As String, v As String, C As String
Dim i As Long, boo As Boolean
Dim booNext As Boolean
outpt = ""
v = r.Text
For i = 1 To Len(v)
boo = r.Characters(i, 1).Font.Italic
C = Mid(v, i, 1)
If (i = 1) And boo Then
outpt = "<i>"
End If
If i = Len(v) Then
If boo Then
outpt = outpt & C & "</i>"
Exit For
Else
outpt = outpt & C
Exit For
End If
End If
booNext = r.Characters(i + 1, 1).Font.Italic
If (boo And booNext) Or (Not boo And Not booNext) Then
outpt = outpt & C
End If
If boo And Not booNext Then
outpt = outpt & C & "</i>"
End If
If Not boo And booNext Then
outpt = outpt & C & "<i>"
End If
Next i
TagMaker = outpt
End Function
示例: