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编辑器。

相关推荐
小高Baby@13 小时前
JSON、bind、form
数据结构·json
阿蒙Amon15 小时前
TypeScript学习-第11章:配置文件(tsconfig.json)
学习·typescript·json
一起养小猫18 小时前
Flutter for OpenHarmony 进阶:Timer组件与倒计时系统深度解析
android·网络·笔记·flutter·json·harmonyos
女王大人万岁20 小时前
Go语言JSON标准库(encoding/json):功能解析与实战指南
服务器·开发语言·后端·golang·json
开开心心就好20 小时前
图片校正漂白工具永久免费,矫正实时预览
网络·人工智能·windows·计算机视觉·计算机外设·电脑·excel
开开心心_Every20 小时前
音频视频转文字工具,离线语音识别免费
人工智能·游戏·微信·pdf·excel·语音识别·memcache
开开心心_Every20 小时前
电脑网速加速工具,无线有线叠加网络
网络·游戏·微信·pdf·电脑·excel·语音识别
Laughtin21 小时前
终端实现excel转csv的命令
excel
一灰灰blog21 小时前
Jar包会自己消失?Excel会“记忆“数据?我遇到了两个灵异bug
java·spring boot·bug·excel
每天要多喝水1 天前
nlohmann/json 的使用
c++·json