Я работаю над подпрограммой VBA для импорта материалов из Excel в Sparx Enterprise Architect.
Одна из проблем заключается в переводе форматированного текста (полужирный, курсив и подчеркивание). В своем тексте советник использует теги форматирования, похожие на html. Так вот:
этот текст имеет полужирный и курсив
должен быть переведен на это:
this text has <b>bold</b> and <i>italic</i>
Если я нашел эту процедуру в другом вопросе, который я немного изменил, чтобы соответствовать моей потребности. Он делает именно то, что мне нужно, но мучительно медленно
'-------------------------------------------------------------
' Author: Geert Bellekens (copied from stackoverflow: https://stackoverflow.com/questions/29916992/extract-text-content-from-cell-with-bold-italic-etc)
' Date: 02/09/2019
' Description: Returns a html formatted string for the (formatted) text in a cell
'-------------------------------------------------------------
Public Function getHTMLFormattedString(r As range) As String
Dim startTimeStamp As Double
startTimeStamp = Timer
Dim isBold As Boolean
Dim isItalic As Boolean
Dim isUnderlined As Boolean
isBold = False
isItalic = False
isUnderlined = False
Dim text As String
text = ""
Dim cCount As Integer
cCount = 0
Dim modifiers As New Collection
On Error Resume Next
cCount = r.Characters.Count
On Error GoTo 0
If cCount > 0 Then
For i = 1 To cCount
Set c = r.Characters(i, 1)
If isBold And Not c.Font.Bold Then
isBold = False
text = removeModifier("b", text, modifiers)
End If
If isItalic And Not c.Font.Italic Then
isItalic = False
text = removeModifier("i", text, modifiers)
End If
If isUnderlined And c.Font.Underline = xlUnderlineStyleNone Then
isUnderlined = False
text = removeModifier("u", text, modifiers)
End If
If c.Font.Bold And Not isBold Then
isBold = True
text = addModifier("b", text, modifiers)
End If
If c.Font.Italic And Not isItalic Then
isItalic = True
text = addModifier("i", text, modifiers)
End If
If Not (c.Font.Underline = xlUnderlineStyleNone) And Not isUnderlined Then
isUnderlined = True
text = addModifier("u", text, modifiers)
End If
text = text & c.text
If i = cCount Then
text = closeAllModifiers(text, modifiers)
End If
Next i
Else
text = r.text
If r.Font.Bold Then
text = "<b>" & text & "</b>"
End If
If r.Font.Italic Then
text = "<i>" & text & "</i>"
End If
If Not (r.Font.Underline = xlUnderlineStyleNone) Then
text = "<u>" & text & "</u>"
End If
End If
'replace newline with CRLF
text = Replace(text, Chr(10), vbNewLine)
'return
getHTMLFormattedString = text
'get processingtime
MsgBox "processed " & Len(text) & " characters in " & Format(Timer - startTimeStamp, "00.00") & " seconds"
End Function
Я тестировал этот код со строкой lorem ipsum из 1000 символов без какого-либо форматирования, и это обрабатывается за 4,89 секунды.
Вопрос : Что я могу сделать, чтобы улучшить производительность?
- Есть ли лучший способ зациклить все символы?
- Можно ли как-то определить, есть ли ячейка? не имеет форматирования вообще (и, следовательно, пропустить всю процедуру)?