1
This commit is contained in:
@@ -264,12 +264,26 @@ public class TencentDocServiceImpl implements ITencentDocService {
|
||||
log.info("API调用成功,原始返回结果: {}", result != null ? result.toJSONString() : "null");
|
||||
|
||||
// 检查API响应中的错误码
|
||||
if (result != null && result.containsKey("code")) {
|
||||
Integer code = result.getInteger("code");
|
||||
if (code != null && code != 0) {
|
||||
String message = result.getString("message");
|
||||
log.error("腾讯文档API返回错误 - code: {}, message: {}", code, message);
|
||||
throw new RuntimeException("腾讯文档API错误: " + message + " (code: " + code + ")");
|
||||
// 根据官方文档,成功响应包含 ret=0,错误响应包含 code!=0
|
||||
// 参考:https://docs.qq.com/open/document/app/openapi/v3/sheet/get/get_range.html
|
||||
if (result != null) {
|
||||
// 检查错误码(code字段)
|
||||
if (result.containsKey("code")) {
|
||||
Integer code = result.getInteger("code");
|
||||
if (code != null && code != 0) {
|
||||
String message = result.getString("message");
|
||||
log.error("腾讯文档API返回错误 - code: {}, message: {}", code, message);
|
||||
throw new RuntimeException("腾讯文档API错误: " + message + " (code: " + code + ")");
|
||||
}
|
||||
}
|
||||
// 检查业务返回码(ret字段)
|
||||
if (result.containsKey("ret")) {
|
||||
Integer ret = result.getInteger("ret");
|
||||
if (ret != null && ret != 0) {
|
||||
String msg = result.getString("msg");
|
||||
log.error("腾讯文档API业务错误 - ret: {}, msg: {}", ret, msg);
|
||||
throw new RuntimeException("腾讯文档API业务错误: " + msg + " (ret: " + ret + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -330,19 +330,20 @@ public class TencentDocApiUtil {
|
||||
|
||||
/**
|
||||
* 读取表格数据 - V3 API
|
||||
* 根据官方文档:https://docs.qq.com/open/document/app/openapi/v3/sheet/get/get_range.html
|
||||
*
|
||||
* @param accessToken 访问令牌
|
||||
* @param appId 应用ID
|
||||
* @param openId 开放平台用户ID
|
||||
* @param fileId 文件ID(在线表格的唯一标识)
|
||||
* @param sheetId 工作表ID(可从表格链接中获取,如 ?tab=BB08J2 中的 BB08J2)
|
||||
* @param range 范围,格式:startRow,startColumn,endRow,endColumn(从0开始,例如:"0,0,10,26"表示A1到Z11)
|
||||
* @param range 范围,使用 A1 表示法(如:"A10:D11", "A1:Z100")
|
||||
* @param apiBaseUrl API基础地址(默认:https://docs.qq.com/openapi/spreadsheet/v3)
|
||||
* @return 表格数据(JSON格式,包含gridData)
|
||||
* @return 表格数据(JSON格式,包含 data.gridData)
|
||||
*/
|
||||
public static JSONObject readSheetData(String accessToken, String appId, String openId, String fileId, String sheetId, String range, String apiBaseUrl) {
|
||||
// V3版本API路径格式:/openapi/spreadsheet/v3/files/{fileId}/{sheetId}/{range}
|
||||
// range格式:startRow,startColumn,endRow,endColumn(从0开始的索引)
|
||||
// range格式:A1表示法(Excel格式),如 A10:D11
|
||||
String apiUrl = String.format("%s/files/%s/%s/%s", apiBaseUrl, fileId, sheetId, range);
|
||||
log.info("读取表格数据 - fileId: {}, sheetId: {}, range: {}, apiUrl: {}", fileId, sheetId, range, apiUrl);
|
||||
return callApi(accessToken, appId, openId, apiUrl, "GET", null);
|
||||
|
||||
@@ -17,8 +17,9 @@ public class TencentDocDataParser {
|
||||
|
||||
/**
|
||||
* 解析腾讯文档 V3 API 返回的数据为简单的二维数组格式
|
||||
* 根据官方文档,响应格式为:{ "ret": 0, "msg": "Succeed", "data": { "gridData": {...} } }
|
||||
*
|
||||
* @param apiResponse API响应(可能包含 gridData 或 values 字段)
|
||||
* @param apiResponse API响应(可能包含 data.gridData、gridData 或 values 字段)
|
||||
* @return 二维数组格式的数据,每行是一个JSONArray
|
||||
*/
|
||||
public static JSONArray parseToSimpleArray(JSONObject apiResponse) {
|
||||
@@ -26,20 +27,32 @@ public class TencentDocDataParser {
|
||||
return new JSONArray();
|
||||
}
|
||||
|
||||
// 方式1:检查是否有 gridData 字段(V3 API 新格式)
|
||||
// 方式1:检查是否有 data.gridData 字段(官方V3 API格式)
|
||||
JSONObject data = apiResponse.getJSONObject("data");
|
||||
if (data != null) {
|
||||
JSONObject gridData = data.getJSONObject("gridData");
|
||||
if (gridData != null) {
|
||||
log.debug("使用 data.gridData 格式解析");
|
||||
return parseGridData(gridData);
|
||||
}
|
||||
}
|
||||
|
||||
// 方式2:检查是否有 gridData 字段(直接格式)
|
||||
JSONObject gridData = apiResponse.getJSONObject("gridData");
|
||||
if (gridData != null) {
|
||||
log.debug("使用 gridData 格式解析");
|
||||
return parseGridData(gridData);
|
||||
}
|
||||
|
||||
// 方式2:检查是否有 values 字段(简单格式)
|
||||
// 方式3:检查是否有 values 字段(简单格式)
|
||||
JSONArray values = apiResponse.getJSONArray("values");
|
||||
if (values != null) {
|
||||
log.debug("使用 values 格式解析");
|
||||
return values;
|
||||
}
|
||||
|
||||
// 如果都没有,返回空数组
|
||||
log.warn("API响应中既没有 gridData 也没有 values 字段,返回空数组");
|
||||
log.warn("API响应中既没有 data.gridData、gridData 也没有 values 字段,返回空数组");
|
||||
return new JSONArray();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user