三 决定使用自定义函数/插件,
通过Java开发自定义函数
使用阿里百炼官方Apache HttpClient封装API调用逻辑。
代码片段:
package com.fr.function;
import com.fr.script.AbstractFunction;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.nio.charset.StandardCharsets;
public class DeepSeekQuery extends AbstractFunction {
private static final String API_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
private static final String API_KEY = "sk-d962d1e47d18c"; // 替换实际API密钥
@Override
public Object run(Object[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(API_URL);
// 构建请求头
httpPost.setHeader("Authorization", "Bearer " + API_KEY);
httpPost.setHeader("Content-Type", "application/json");
// 构建请求体(适配通义千问格式)
String requestBody = String.format("{"
+ "\"model\":\"deepseek-r1-distill-llama-70b\","
+ "\"messages\":[{\"role\":\"user\",\"content\":\"%s\"}],"
+ "\"temperature\":0.7}", args[0].toString());
httpPost.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
// 执行请求并解析响应
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
return extractContent(result); // 提取响应内容
}
} catch (Exception e) {
return "API调用失败: " + e.getMessage();
}
}
private String extractContent(String jsonResponse) {
// 根据阿里响应结构提取内容(参考搜索结果[1][5])
int startIndex = jsonResponse.indexOf("\"content\":\"") + 11;
int endIndex = jsonResponse.indexOf("\"", startIndex);
return jsonResponse.substring(startIndex, endIndex).replace("\\n", "\n");
}
}
在报表中使用公式 =DeepSeekQuery(输入参数) 调用。

结果 公司把api屏蔽了
没法预览效果

哪位公网同志试试 看可行不
用法:
一、基础调用方法
1.单元格直接调用
在需要显示AI结果的单元格输入公式:
=DeepSeekQuery("用户输入内容")
示例:=DeepSeekQuery("分析华北区Q4销售额趋势")
2.参数扩展:支持动态参数传递
=DeepSeekQuery(A1)(A1为包含用户问题的单元格)
参数面板交互
添加文本框控件,绑定参数input_text
目标单元格公式改为:
=DeepSeekQuery($input_text)
二、高级功能实现
1.数据驱动分析
结合SQL数据集结果调用AI:
=DeepSeekQuery("解释以下销售数据:" + C1 + ",并提出改进建议")
2.多步骤对话
通过单元格串联实现上下文记忆:
B1: =DeepSeekQuery("生成华东区产品推广策略")
B2: =DeepSeekQuery(B1 + ",请细化执行步骤")
三、典型应用场景
1.智能报表注释
=DeepSeekQuery("用200字解释图表趋势:" + CHAR(10) + GRAPH_DATA(A1:E10))
2.数据校验自动化
=IF(ISERROR(VLOOKUP(...)),
DeepSeekQuery("校验失败原因:" + ERROR.TEXT + ",建议修正方法"),
"数据正常")