今天跟大家分享下我们如何自定义一个ExcelAI函数,帮助我们解决各种问题,操作也非常的简单,只需选择单元格,然后提出对应的要求即可,具体的效果如下图所示。 ![]() 一、关于卡的问题 之前将过如何将Deepseek嵌入的Excel表格,不少粉丝反馈太卡了,卡并不是代码卡,而是DeepSeek用的人太多,API调用的比较慢,如果你感觉卡,可以从云服商那里调取deepseek或者别的大模型,今天我们就是在火山调用的豆包的大模型,速度也比较快了 ![]() 二、调用别的模型 我们就以火山引擎为例,跟大家简单的演示下,关键需要或许三个参数:KEY、模型的URL以及模型ID,这些在调用的时候有提示的,根据操作来一步一步的操作即可,具体如下图所示 ![]() 三、更改代码 下面的代码我需要更改3处,具体操作如下所示,大家可直接粘贴代码的时候修改下 Const API_KEY As String = "你的API" Const API_URL As String = "模型的URL地址" BuildSafeInput = "{""model"":""模型的ID"",""messages"" Function ExcelAI(TargetCell As Range, Question As String) As Variant On Error GoTo ErrorHandler Const API_KEY As String = "你的API" ' 需替换有效密钥 Const API_URL As String = "模型的URL地址" ' 构建安全请求 Dim safeInput As String safeInput = BuildSafeInput(TargetCell.Text, Question) ' 发送API请求 Dim response As String response = PostRequest(API_KEY, API_URL, safeInput) ' 解析响应内容 If Left(response, 5) = "Error" Then ExcelAI = response Else ExcelAI = ParseContent(response) End If Exit FunctionErrorHandler: ExcelAI = "Runtime Error: " & Err.DescriptionEnd Function' 构建安全输入内容Private Function BuildSafeInput(Context As String, Question As String) As String Dim sysMsg As String If Len(Context) > 0 Then sysMsg = "{""role"":""system"",""content"":""上下文:" & EscapeJSON(Context) & """}," End If BuildSafeInput = "{""model"":""模型的ID"",""messages"":[" & _ sysMsg & "{""role"":""user"",""content"":""" & EscapeJSON(Question) & """}]}"End Function' 发送POST请求Private Function PostRequest(apiKey As String, url As String, payload As String) As String Dim http As Object Set http = CreateObject("MSXML2.XMLHTTP") On Error Resume Next With http .Open "POST", url, False .setRequestHeader "Content-Type", "application/json" .setRequestHeader "Authorization", "Bearer " & apiKey .send payload If Err.Number <> 0 Then PostRequest = "Error: HTTP Request Failed" Exit Function End If ' 增加10秒超时控制 Dim startTime As Double startTime = Timer Do While .readyState < 4 And Timer - startTime < 10 DoEvents Loop End With If http.Status = 200 Then PostRequest = http.responseText Else PostRequest = "Error " & http.Status & ": " & http.statusText End IfEnd Function' JSON特殊字符转义Private Function EscapeJSON(str As String) As String str = Replace(str, "\", "\\") str = Replace(str, """", "\""") str = Replace(str, vbCr, "\r") str = Replace(str, vbLf, "\n") str = Replace(str, vbTab, "\t") EscapeJSON = strEnd Function' 智能解析响应内容Private Function ParseContent(json As String) As String Dim regex As Object, matches As Object Set regex = CreateObject("VBScript.RegExp") ' 增强版正则表达式 With regex .Pattern = """content"":\s*""((?:\\""|[\s\S])*?)""" .Global = False .MultiLine = True .IgnoreCase = True End With Set matches = regex.Execute(json) If matches.Count > 0 Then Dim rawText As String rawText = matches(0).SubMatches(0) ' 反转义处理 rawText = Replace(rawText, "\""", """") rawText = Replace(rawText, "\\", "\") rawText = Replace(rawText, "\n", vbCrLf) rawText = Replace(rawText, "\r", vbCr) rawText = Replace(rawText, "\t", vbTab) ParseContent = rawText Else ' 错误信息提取 Dim errMatch As Object regex.Pattern = """message"":\s*""(.*?)""" Set errMatch = regex.Execute(json) If errMatch.Count > 0 Then ParseContent = "API Error: " & errMatch(0).SubMatches(0) Else ParseContent = "Invalid Response" End If End IfEnd Function 四、复制代码 打开Excel点击【开发工具】最左侧点击VB的编辑窗口,然后在右侧点击窗口,找到【模块】插入模块后将代码直接粘贴到右侧的空白区域即可,一定需要记得将上面的三处做一下修改,才能正确的调用到这个模型,至此就设置完毕了 用法:=ExcelAI(单元格,”你需要的结果”) ![]() |
|