Excel接入deepseek

先进入deepseek官网:DeepSeek | 深度求索

点击API开放平台:

确保余额里有钱:

创建APIkey:

复制到.txt文件中储存好


插入VBA代码:

vbnet 复制代码
Function OptimizeEbayTitle(originalTitle As String) As String
    Dim Prompt As String
    Prompt = "作为专业eBay运营人员,请优化以下标题:[[" & originalTitle & "]]" & vbCrLf & _
             "要求:" & vbCrLf & _
             "1. 控制在80个字符以内" & vbCrLf & _
             "2. 保留核心信息" & vbCrLf & _
             "3. 直接输出优化结果,不加额外说明或符号"
             
    OptimizeEbayTitle = AskAI(Prompt)
    
    ' 如果结果包含引号,移除它们
    OptimizeEbayTitle = Replace(Replace(OptimizeEbayTitle, """", ""), """, "")
End Function

Function AskAI(Prompt As String) As String
    Dim jsonResponse As String
    Dim contentStart As Long
    Dim contentEnd As Long
    Dim contentText As String
    
    ' 获取API原始响应
    jsonResponse = DeepSeek_Query(Prompt)
    
    ' 检查是否有错误
    If Left(jsonResponse, 5) = "Error" Or Left(jsonResponse, 5) = "HTTP" Then
        AskAI = jsonResponse ' 直接返回错误信息
        Exit Function
    End If
    
    ' 尝试定位content字段
    contentStart = InStr(1, jsonResponse, """content"":""") + Len("""" & "content" & """:""")
    
    If contentStart > Len("""" & "content" & """:""") Then
        ' 查找content结束位置
        contentEnd = InStr(contentStart, jsonResponse, """")
        
        If contentEnd > contentStart Then
            ' 提取内容
            contentText = Mid(jsonResponse, contentStart, contentEnd - contentStart)
            
            ' 反转义特殊字符
            contentText = Replace(contentText, "\""", """")   ' 双引号
            contentText = Replace(contentText, "\n", vbCrLf)  ' 换行符
            contentText = Replace(contentText, "\\", "\")     ' 反斜杠
            
            AskAI = contentText
            Exit Function
        End If
    End If
    
    ' 如果无法解析,返回原始JSON的前100字符
    AskAI = "无法解析响应: " & Left(jsonResponse, 100)
End Function

' 原始API调用函数(保持不变)
Function DeepSeek_Query(Prompt As String) As String
    Dim Http As Object
    Dim Url As String, APIKey As String
    Dim Body As String
    
    APIKey = "" ' 替换为真实API密钥
    Url = "https://api.deepseek.com/v1/chat/completions"
    
    Set Http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    
    On Error GoTo ErrorHandler
    
    ' 特殊字符转义处理
    Dim SafePrompt As String
    SafePrompt = Replace(Prompt, """", "\""")
    SafePrompt = Replace(SafePrompt, vbCrLf, "\n")
    SafePrompt = Replace(SafePrompt, "\", "\\")
    
    Body = "{""model"":""deepseek-chat"",""messages"":[{""role"":""user"",""content"":""" & SafePrompt & """}]}"
    
    Http.Open "POST", Url, False
    Http.setRequestHeader "Content-Type", "application/json"
    Http.setRequestHeader "Authorization", "Bearer " & APIKey
    Http.send Body
    
    If Http.Status <> 200 Then
        DeepSeek_Query = "HTTP错误 " & Http.Status & ": " & Http.statusText
        Exit Function
    End If
    
    DeepSeek_Query = Http.responseText
    Exit Function
    
ErrorHandler:
    DeepSeek_Query = "VBA错误: " & Err.Description
End Function

效果展示:

相关推荐
开开心心_Every5 小时前
发票批量打印工具支持双面预览页面方向设置
游戏·微信·pdf·华为云·excel·语音识别·googlecloud
xinlianluohan7 小时前
excel筛选背景颜色的值&拼接单元格&判断单元格是否含有中文
excel
骆驼爱记录11 小时前
Word通配符技巧:高效文档处理指南
开发语言·c#·自动化·word·excel·wps·新人首发
AI刀刀11 小时前
豆包怎么生成excel
ai·excel·豆包·deepseek·ds随心转
骆驼爱记录12 小时前
Word表格题注自动设置全攻略
开发语言·c#·自动化·word·excel·wps·新人首发
天荒地老笑话么13 小时前
Vim核心快捷键与运维实战指南
运维·vim·excel
开开心心就好1 天前
键盘改键工具免安装,自定义键位屏蔽误触
java·网络·windows·随机森林·计算机外设·电脑·excel
fqbqrr1 天前
2601Mfc,自动化excel
自动化·excel·mfc
tlwlmy2 天前
python excel图片批量导出
开发语言·python·excel
TracyDemo2 天前
excel 透视图怎么进行删除透视图
excel