之前跟大家分享了, 如何將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 String
Dim API As String
Dim SendTxt As String
Dim Http As Object
Dim status_code As Integer
Dim response As String
API = "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 SendTxt
status_code = .Status
response = .responseText
End With
If status_code = 200 Then
CallDeepSeekAPI = response
Else
CallDeepSeekAPI = "Error: " & status_code & " - " & response
End If
Set Http = Nothing
End Function
Sub DeepSeekExcel()
Dim api_key As String
Dim userQuestion As String
Dim selectedRange As Range
Dim cell As Range
Dim combinedInput As String
Dim response As String
Dim regex As Object
Dim matches As Object
api_key = "你的api"
If api_key = "" Then
MsgBox "請先設置API密鑰", vbExclamation
Exit Sub
End If
On Error Resume Next
Set selectedRange = Selection
On Error GoTo 0
If selectedRange Is Nothing Then
MsgBox "請先選擇要處理的數據區域", vbExclamation
Exit Sub
End If
userQuestion = InputBox("請輸入您的問題(選中的單元格內容將作為處理數據):", "DeepSeek 提問")
If userQuestion = "" Then Exit Sub
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = """content"":""(.*?)"""
regex.Global = False
regex.MultiLine = True
Application.ScreenUpdating = False
For Each cell In selectedRange
If 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")
' 調用API
response = CallDeepSeekAPI(api_key, combinedInput)
If Left(response, 5) <> "Error" Then
Set matches = regex.Execute(response)
If matches.Count > 0 Then
Dim outputText As String
outputText = matches(0).SubMatches(0)
' 處理轉義字符
outputText = Replace(outputText, "\""", """")
outputText = Replace(outputText, "\\", "\")
outputText = Replace(outputText, "\n", vbCrLf)
' 寫入右側相鄰單元格
cell.Offset(0, 1).Value = outputText
Else
cell.Offset(0, 1).Value = "解析失敗"
End If
Else
cell.Offset(0, 1).Value = "API錯誤"
End If
End If
Next cell
Application.ScreenUpdating = True
MsgBox "處理完成!", vbInformation
Set regex = Nothing
Set selectedRange = Nothing
End Sub
Function CallDeepSeekAPI(api_key As String, inputText As String) As String Dim API As String Dim SendTxt As String Dim Http As Object Dim status_code As Integer Dim response As String API = "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 SendTxt status_code = .Status response = .responseText End With If status_code = 200 Then CallDeepSeekAPI = response Else CallDeepSeekAPI = "Error: " & status_code & " - " & response End If Set Http = NothingEnd FunctionSub DeepSeekExcel() Dim api_key As String Dim userQuestion As String Dim selectedRange As Range Dim cell As Range Dim combinedInput As String Dim response As String Dim regex As Object Dim matches As Object api_key = "你的api" If api_key = "" Then MsgBox "請先設置API密鑰", vbExclamation Exit Sub End If On Error Resume Next Set selectedRange = Selection On Error GoTo 0 If selectedRange Is Nothing Then MsgBox "請先選擇要處理的數據區域", vbExclamation Exit Sub End If userQuestion = InputBox("請輸入您的問題(選中的單元格內容將作為處理數據):", "DeepSeek 提問") If userQuestion = "" Then Exit Sub Set regex = CreateObject("VBScript.RegExp") regex.Pattern = """content"":""(.*?)""" regex.Global = False regex.MultiLine = True Application.ScreenUpdating = False For Each cell In selectedRange If 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") ' 調用API response = CallDeepSeekAPI(api_key, combinedInput) If Left(response, 5) <> "Error" Then Set matches = regex.Execute(response) If matches.Count > 0 Then Dim outputText As String outputText = matches(0).SubMatches(0) ' 處理轉義字符 outputText = Replace(outputText, "\""", """") outputText = Replace(outputText, "\\", "\") outputText = Replace(outputText, "\n", vbCrLf) ' 寫入右側相鄰單元格 cell.Offset(0, 1).Value = outputText Else cell.Offset(0, 1).Value = "解析失敗" End If Else cell.Offset(0, 1).Value = "API錯誤" End If End If Next cell Application.ScreenUpdating = True MsgBox "處理完成!", vbInformation Set regex = Nothing Set selectedRange = NothingEnd Sub
三、代碼粘貼
在Excel中點擊【開發工具】然后點擊【Visiual Basic】進入編輯窗口,在右側空白區域點擊鼠標右鍵找到插入,找到【模塊】,然后在右側的窗口那里直接粘貼即可
在這里一定記得,API替換為自己的API
四、制作按鈕
需要在右側點擊文件,然后最下放找到【選項】來調出Excel選項,在Excel選項中找到【自定義功能區】
我們需要在左側將類別設置【宏】選中【DEEPSeekExcel】這個宏,然后在右側的窗口中點擊對應的選項卡,最后點擊添加,即可將宏作為按鈕添加到Excel表格中,至此就設置完畢了
五、加載宏
如果想要將這個宏按鈕永久的保留在Excel中是需要使用加載宏的,之前發過,大家可以搜一下
DeepSeek搭配Excel,制作自定義按鈕,實現辦公自動化!
視頻Excel從零到一
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.