打開Excel文件并按下ALT + F11打開Visual Basic for Applications(VBA)編輯器。
輸入下面的代碼
Sub FormatJSONCells()
Dim cell As Range
Dim jsonString As String
Dim json As Object
Dim formattedJSON As String
' 循環(huán)遍歷選定的單元格范圍
For Each cell In Selection
jsonString = cell.Value
' 檢查單元格中的內(nèi)容是否為JSON數(shù)據(jù)
If IsValidJSON(jsonString) Then
' 使用VBA內(nèi)置JSON解析功能將JSON數(shù)據(jù)解析為對(duì)象
Set json = JsonConverter.ParseJson(jsonString)
' 將JSON數(shù)據(jù)重新格式化為易于閱讀的形式
formattedJSON = JsonConverter.ConvertToJson(json, Whitespace:=2)
' 將格式化后的JSON數(shù)據(jù)寫回到單元格中
cell.Value = formattedJSON
End If
Next cell
End Sub
Function IsValidJSON(ByVal strJSON As String) As Boolean
On Error Resume Next
Dim json As Object
Set json = JsonConverter.ParseJson(strJSON)
IsValidJSON = (Err.Number = 0)
On Error GoTo 0
End Function
進(jìn)入https://github.com/VBA-tools/VBA-JSON,下載release的最新代碼
VBA編輯器,“文件”->“導(dǎo)入”, 導(dǎo)入文件 JsonConverter.bas
點(diǎn)擊VBA編輯器的菜單欄上的"工具"。
選擇"引用",然后在彈出的對(duì)話框中找到并勾選"Microsoft Scripting Runtime",以及“Microsoft XML 6.0”。
點(diǎn)擊"確定"保存更改。文章來源:http://www.zghlxwxcb.cn/news/detail-621521.html
保存并關(guān)閉VBA編輯器。文章來源地址http://www.zghlxwxcb.cn/news/detail-621521.html
到了這里,關(guān)于Excel將單元格中的json本文格式化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!