This commit is contained in:
van
2026-04-04 16:47:08 +08:00
parent 6fbfecf690
commit c3cafdcbe0

View File

@@ -23,6 +23,12 @@ import java.nio.charset.StandardCharsets;
public class TencentDocApiUtil { public class TencentDocApiUtil {
private static final Logger log = LoggerFactory.getLogger(TencentDocApiUtil.class); private static final Logger log = LoggerFactory.getLogger(TencentDocApiUtil.class);
/**
* 在线表格 batchUpdate 单次请求中操作数上限(官方:单次操作数量小于等于 5
* @see <a href="https://docs.qq.com/open/document/app/openapi/v3/sheet/batchupdate/update.html">批量更新接口</a>
*/
public static final int TENCENT_BATCH_UPDATE_MAX_OPERATIONS = 5;
// 静态初始化块禁用系统代理确保腾讯文档API调用直接连接 // 静态初始化块禁用系统代理确保腾讯文档API调用直接连接
static { static {
@@ -456,12 +462,50 @@ public class TencentDocApiUtil {
*/ */
public static JSONObject batchUpdate(String accessToken, String appId, String openId, String fileId, JSONObject requestBody, String apiBaseUrl) { public static JSONObject batchUpdate(String accessToken, String appId, String openId, String fileId, JSONObject requestBody, String apiBaseUrl) {
String apiUrl = String.format("%s/files/%s/batchUpdate", apiBaseUrl, fileId); String apiUrl = String.format("%s/files/%s/batchUpdate", apiBaseUrl, fileId);
log.info("批量更新表格batchUpdate- fileId: {}, requests数量: {}", JSONArray requests = requestBody != null ? requestBody.getJSONArray("requests") : null;
fileId, requestBody.getJSONArray("requests") != null ? requestBody.getJSONArray("requests").size() : 0); if (requests == null || requests.isEmpty()) {
log.debug("批量更新表格 - 请求体: {}", requestBody.toJSONString()); throw new IllegalArgumentException("batchUpdate 请求体缺少非空 requests 数组");
}
return callApi(accessToken, appId, openId, apiUrl, "POST", requestBody.toJSONString());
int total = requests.size();
log.info("批量更新表格batchUpdate- fileId: {}, 总操作数: {}(每批最多 {} 条)",
fileId, total, TENCENT_BATCH_UPDATE_MAX_OPERATIONS);
JSONObject lastResult = null;
for (int start = 0; start < total; start += TENCENT_BATCH_UPDATE_MAX_OPERATIONS) {
int end = Math.min(start + TENCENT_BATCH_UPDATE_MAX_OPERATIONS, total);
JSONArray chunk = new JSONArray();
for (int i = start; i < end; i++) {
chunk.add(requests.get(i));
}
JSONObject chunkBody = new JSONObject();
chunkBody.put("requests", chunk);
log.info("batchUpdate 分片 {}-{} / {}, 本批 {} 条", start + 1, end, total, chunk.size());
log.debug("批量更新表格 - 分片请求体: {}", chunkBody.toJSONString());
lastResult = callApi(accessToken, appId, openId, apiUrl, "POST", chunkBody.toJSONString());
assertSpreadsheetBatchUpdateSuccess(lastResult);
}
return lastResult != null ? lastResult : new JSONObject();
}
/**
* V3 表格接口在 HTTP 200 时仍可能在 body 内返回 code!=0如单次 batch 超过 5 条)
*/
private static void assertSpreadsheetBatchUpdateSuccess(JSONObject result) {
if (result == null) {
return;
}
if (result.containsKey("code")) {
int code = result.getIntValue("code");
if (code != 0) {
String msg = result.getString("message");
log.error("腾讯文档 batchUpdate 业务失败: code={}, message={}", code, msg);
throw new RuntimeException("腾讯文档 batchUpdate 失败: " + msg + " (code=" + code + ")");
}
}
} }
/** /**