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

效果展示:

相关推荐
梦幻通灵16 小时前
Excel的TEXT函数实战【持续更新】
excel
陈奕昆19 小时前
n8n实战营Day2课时2:Loop+Merge节点进阶·Excel批量校验实操
人工智能·python·excel·n8n
诸神缄默不语1 天前
Python 3中的win32com使用教程+示例:从Excel读取数据生成Word格式报告批量发邮件
python·word·excel
_大龄2 天前
前端解析excel
前端·excel
johnny2333 天前
智能电子表格:Airtable、NocoDB、teable、APITable
excel
2501_930707783 天前
如何使用C#代码在Excel 文件中添加工作表
excel
shouchaobao3 天前
免费PDF工具:PDF转Word/Excel/图片+AI总结+合并拆分+OCR识别,多端无广告!
pdf·word·excel
allbs4 天前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
m5655bj4 天前
使用 Python 高效复制 Excel 行、列、单元格
开发语言·python·excel
温轻舟4 天前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟