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

相关推荐
程序猿多布1 小时前
Unity Excel导表工具转Lua文件
unity·excel
The god of big data5 小时前
Excel核心函数VLOOKUP全解析:从入门到精通
excel
专注VB编程开发20年14 小时前
除了 EasyXLS,加载和显示.xlsx 格式的excel表格,并支持单元格背景色、边框线颜色和粗细等格式化特性
c++·windows·excel·mfc·xlsx
诸神缄默不语20 小时前
Excel如何给单元格填色,以及如何用Python 3实现单元格填色
开发语言·python·excel·openpyxl·patternfill
程序员小续1 天前
Excel 表格和 Node.js 实现数据转换工具
前端·javascript·react.js·前端框架·vue·excel·reactjs
jiemidashi1 天前
WPS 中 Excel 密码取消方法大解密
经验分享·excel·wps
林的快手2 天前
伪类选择器
android·前端·css·chrome·ajax·html·json
赵域Phoenix2 天前
挑选出行数足够的excel文件
开发语言·python·excel
oh,huoyuyan2 天前
火语言RPA--Excel读取内容
excel·rpa
css趣多多2 天前
初步安装和使用vant组件库,使用css变量定制vant主题样式 ,小程序的API Promise化,调用promise化之API
json