摘要:之前跟大家分享了, 如何将Deepseek嵌入Word,有粉丝就问道如何将Deepseek嵌入到Excel呢?这不,今天方法就来了,跟嵌入Word的使用方法类型
之前跟大家分享了, 如何将Deepseek嵌入Word,有粉丝就问道如何将Deepseek嵌入到Excel呢?这不,今天方法就来了,跟嵌入Word的使用方法类型
一、使用方法
先来简单地说下使用的方法,操作非常的简单,跟嵌入Word类似
首先我们需要先选中对应的数据区域,然后在上方点击Deepseek,最后会跳出窗口,在窗口中提出问题,等待一段时间后就能得到对应的结果了,下面来看下如何构建这个效果
二、代码准备
首先需要复制下方的代码,关键点是需要修改API为自己的API,如何获取API的话,大家可以翻下之前的文章,是需要在Deepseek的官网获取的。
api_key = "你的api"
在这里将你的api直接替换为deepseek的api秘钥即可
Function CallDeepSeekAPI(api_key As String, inputText As String) As StringDim API As StringDim SendTxt As StringDim Http As ObjectDim status_code As IntegerDim response As StringAPI = "https://api.deepseek.com/chat/completions"SendTxt = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""You are a Excel assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"Set Http = CreateObject("MSXML2.XMLHTTP")With Http.Open "POST", API, False.setRequestHeader "Content-Type", "application/json".setRequestHeader "Authorization", "Bearer " & api_key.send SendTxtstatus_code = .Statusresponse = .responseTextEnd WithIf status_code = 200 ThenCallDeepSeekAPI = responseElseCallDeepSeekAPI = "Error: " & status_code & " - " & responseEnd IfSet Http = NothingEnd FunctionSub DeepSeekExcelDim api_key As StringDim userQuestion As StringDim selectedRange As RangeDim cell As RangeDim combinedInput As StringDim response As StringDim regex As ObjectDim matches As Objectapi_key = "你的api"If api_key = "" ThenMsgBox "请先设置API密钥", vbExclamationExit SubEnd IfOn Error Resume NextSet selectedRange = SelectionOn Error GoTo 0If selectedRange Is Nothing ThenMsgBox "请先选择要处理的数据区域", vbExclamationExit SubEnd IfuserQuestion = InputBox("请输入您的问题(选中的单元格内容将作为处理数据):", "DeepSeek 提问")If userQuestion = "" Then Exit SubSet regex = CreateObject("VBScript.RegExp")regex.Pattern = """content"":""(.*?)"""regex.Global = Falseregex.MultiLine = TrueApplication.ScreenUpdating = FalseFor Each cell In selectedRangeIf Trim(cell.Value) "" Then' 组合问题和单元格内容combinedInput = userQuestion & vbCrLf & vbCrLf & "数据内容:" & vbCrLf & cell.Value' 转义特殊字符combinedInput = Replace(combinedInput, "\", "\\")combinedInput = Replace(combinedInput, """", "\""")combinedInput = Replace(combinedInput, vbCrLf, "\n")combinedInput = Replace(combinedInput, vbCr, "\n")combinedInput = Replace(combinedInput, vbLf, "\n")' 调用APIresponse = CallDeepSeekAPI(api_key, combinedInput)If Left(response, 5) "Error" ThenSet matches = regex.Execute(response)If matches.Count > 0 ThenDim outputText As StringoutputText = matches(0).SubMatches(0)' 处理转义字符outputText = Replace(outputText, "\""", """")outputText = Replace(outputText, "\\", "\")outputText = Replace(outputText, "\n", vbCrLf)' 写入右侧相邻单元格cell.Offset(0, 1).Value = outputTextElsecell.Offset(0, 1).Value = "解析失败"End IfElsecell.Offset(0, 1).Value = "API错误"End IfEnd IfNext cellApplication.ScreenUpdating = TrueMsgBox "处理完成!", vbInformationSet regex = NothingSet selectedRange = NothingEnd Sub三、代码粘贴
在Excel中点击【开发工具】然后点击【Visiual Basic】进入编辑窗口,在右侧空白区域点击鼠标右键找到插入,找到【模块】,然后在右侧的窗口那里直接粘贴即可
在这里一定记得,API替换为自己的API
四、制作按钮
需要在右侧点击文件,然后最下放找到【选项】来调出Excel选项,在Excel选项中找到【自定义功能区】
五、加载宏
如果想要将这个宏按钮永久的保留在Excel中是需要使用加载宏的,之前发过,大家可以搜一下,下面的就是方法
如果你想要提高工作效率,不想再求同事帮你解决各种Excel问题,可以了解下我的专栏,WPS用户也能使用,讲解了函数、图表、透视表、数据看板等常用功能,带你快速成为Excel高手
来源:Excel从零到一