Excel将单元格中的json本文格式化

打开Excel文件并按下ALT + F11打开Visual Basic for Applications(VBA)编辑器。

输入下面的代码

vbnet 复制代码
Sub FormatJSONCells()
    Dim cell As Range
    Dim jsonString As String
    Dim json As Object
    Dim formattedJSON As String

    ' 循环遍历选定的单元格范围
    For Each cell In Selection
        jsonString = cell.Value
        ' 检查单元格中的内容是否为JSON数据
        If IsValidJSON(jsonString) Then
            ' 使用VBA内置JSON解析功能将JSON数据解析为对象
            Set json = JsonConverter.ParseJson(jsonString)
            ' 将JSON数据重新格式化为易于阅读的形式
            formattedJSON = JsonConverter.ConvertToJson(json, Whitespace:=2)
            ' 将格式化后的JSON数据写回到单元格中
            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

进入https://github.com/VBA-tools/VBA-JSON,下载release的最新代码

VBA编辑器,"文件"->"导入", 导入文件 JsonConverter.bas

点击VBA编辑器的菜单栏上的"工具"。

选择"引用",然后在弹出的对话框中找到并勾选"Microsoft Scripting Runtime",以及"Microsoft XML 6.0"。

点击"确定"保存更改。

保存并关闭VBA编辑器。

相关推荐
Ashley_Amanda5 小时前
JavaScript 中 JSON 的处理方法
前端·javascript·json
Hello.Reader7 小时前
Flink 系统内置函数(Built-in Functions)分类、典型用法与选型建议
大数据·flink·excel
Lxinccode7 小时前
python(67) : json解析异常json.decoder.JSONDecodeError
json·json解析·json解析报错·jsondecodeerror
Violet_YSWY8 小时前
我要生成上边是表头,左边是平均值、方差等的表格如何json
json
小北方城市网12 小时前
第7课:Vue 3应用性能优化与进阶实战——让你的应用更快、更流畅
前端·javascript·vue.js·ai·性能优化·正则表达式·json
彭于晏Yan14 小时前
excel导入导出
spring boot·excel
吃喝不愁霸王餐APP开发者14 小时前
使用Mockito与WireMock对美团霸王餐接口进行契约测试与集成验证
java·json
chilavert31815 小时前
技术演进中的开发沉思-268 Ajax:JSON
javascript·ajax·okhttp·json
lbb 小魔仙16 小时前
Python 读取 Excel 文件:openpyxl 与 pandas 实战对比
python·excel·pandas
徐赛俊16 小时前
# Excel 图表自动变色教程(基于控制单元格)
excel