浏览代码

飞书表格api行数限制

wangyunpeng 10 月之前
父节点
当前提交
733d24b30c

+ 66 - 53
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/DataDashboardService.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.google.common.collect.Lists;
 import com.tzld.longarticle.recommend.server.model.NewSortStrategyExport;
 import com.tzld.longarticle.recommend.server.repository.crawler.AccountAvgInfoRepository;
 import com.tzld.longarticle.recommend.server.repository.crawler.ArticleDetailInfoRepository;
@@ -239,34 +240,42 @@ public class DataDashboardService {
             }
         }
         if (count > 0) {
-            // 删除当前日期已存在的旧数据
-            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
-            HttpEntity<Object> deleteEntity = new HttpEntity<>(
-                    String.format("{\n" +
-                            "    \"dimension\": {\n" +
-                            "        \"sheetId\": \"%s\",\n" +
-                            "        \"majorDimension\": \"ROWS\",\n" +
-                            "        \"startIndex\": %s,\n" +
-                            "        \"endIndex\": %s\n" +
-                            "    }\n" +
-                            "}", sheetId, startRowIndex, count + startRowIndex - 1),
-                    httpHeaders);
-            restTemplate.exchange(String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/dimension_range", sheetToken),
-                    HttpMethod.DELETE, deleteEntity, String.class);
+            int delNum = 0;
+            do {
+                // 删除当前日期已存在的旧数据
+                httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+                HttpEntity<Object> deleteEntity = new HttpEntity<>(
+                        String.format("{\n" +
+                                "    \"dimension\": {\n" +
+                                "        \"sheetId\": \"%s\",\n" +
+                                "        \"majorDimension\": \"ROWS\",\n" +
+                                "        \"startIndex\": %s,\n" +
+                                "        \"endIndex\": %s\n" +
+                                "    }\n" +
+                                "}", sheetId, startRowIndex, Math.min(startRowIndex + 4000, count - delNum + startRowIndex) - 1),
+                        httpHeaders);
+                restTemplate.exchange(String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/dimension_range", sheetToken),
+                        HttpMethod.DELETE, deleteEntity, String.class);
+                delNum = Math.min(delNum + 4000, count);
+            } while (delNum < count);
+        }
+        List<List<List<Object>>> partitions = Lists.partition(rows, 4000);
+        int startRow = startRowIndex;
+        for (List<List<Object>> partition : partitions) {
+            // 插入数据
+            HttpEntity<Object> postEntity = new HttpEntity<>(MapBuilder
+                    .builder()
+                    .put("valueRange", MapBuilder
+                            .builder()
+                            .put("range", String.format("%s!A" + startRow + ":W", sheetId) + (partition.size() + startRow - 1))
+                            .put("values", partition)
+                            .build())
+                    .build(), httpHeaders);
+            restTemplate.exchange(String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/values_prepend",
+                            sheetToken),
+                    HttpMethod.POST, postEntity, String.class);
+            startRow += partition.size();
         }
-
-        // 插入数据
-        HttpEntity<Object> postEntity = new HttpEntity<>(MapBuilder
-                .builder()
-                .put("valueRange", MapBuilder
-                        .builder()
-                        .put("range", String.format("%s!A" + startRowIndex + ":W", sheetId) + (rowNum + startRowIndex - 1))
-                        .put("values", rows)
-                        .build())
-                .build(), httpHeaders);
-        restTemplate.exchange(String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/values_prepend",
-                        sheetToken),
-                HttpMethod.POST, postEntity, String.class);
 //        // 合并单元格
 //        if (isMerge) {
 //            HttpEntity<Object> mergeEntity = new HttpEntity<>(MapBuilder
@@ -280,32 +289,36 @@ public class DataDashboardService {
 //        }
         // 此处先简单处理,调整单元格为”百分比小数点“
         if (CollectionUtil.isNotEmpty(styles)) {
-            for (Pair<String, String> style : styles) {
-                HttpEntity<Map<Object, Object>> styleEntity = new HttpEntity<>(MapBuilder
-                        .builder()
-                        .put("appendStyle",
-                                MapBuilder
-                                        .builder()
-                                        .put("range", String.format("%s!%s" + startRowIndex + ":%s", sheetId,
-                                                style.getFirst(), style.getFirst())
-                                                + (rowNum + startRowIndex - 1))
-                                        .put("style",
-                                                MapBuilder
-                                                        .builder()
-                                                        .put("formatter", style.getSecond())
-                                                        .build()
-                                        )
-                                        .build()
-                        )
-                        .build(), httpHeaders);
-                restTemplate.exchange(
-                        String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/style",
-                                sheetToken),
-                        HttpMethod.PUT,
-                        styleEntity,
-                        String.class
-                );
-            }
+            startRow = startRowIndex;
+            do {
+                for (Pair<String, String> style : styles) {
+                    HttpEntity<Map<Object, Object>> styleEntity = new HttpEntity<>(MapBuilder
+                            .builder()
+                            .put("appendStyle",
+                                    MapBuilder
+                                            .builder()
+                                            .put("range", String.format("%s!%s" + startRow + ":%s", sheetId,
+                                                    style.getFirst(), style.getFirst())
+                                                    + (Math.min(startRow + 4000, rowNum + startRowIndex) - 1))
+                                            .put("style",
+                                                    MapBuilder
+                                                            .builder()
+                                                            .put("formatter", style.getSecond())
+                                                            .build()
+                                            )
+                                            .build()
+                            )
+                            .build(), httpHeaders);
+                    restTemplate.exchange(
+                            String.format("https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/%s/style",
+                                    sheetToken),
+                            HttpMethod.PUT,
+                            styleEntity,
+                            String.class
+                    );
+                }
+                startRow += 4000;
+            } while (startRow < rowNum);
         }
     }