Commit 21be7ae8 authored by fangxinjiang's avatar fangxinjiang

企微智能表格相关-fxj

parent 3828d26b
...@@ -70,4 +70,10 @@ public class EkpAlertTableController { ...@@ -70,4 +70,10 @@ public class EkpAlertTableController {
ekpAlertTableService.pushBudgetAlertInfoTomanager(0); ekpAlertTableService.pushBudgetAlertInfoTomanager(0);
} }
@PostMapping("/tesxWxExcelApi")
public void tesxWxExcelApi() {
ekpAlertTableService.tesxWxExcelApi();
}
} }
...@@ -30,4 +30,6 @@ public interface EkpAlertTableService extends IService<EkpAlertTable> { ...@@ -30,4 +30,6 @@ public interface EkpAlertTableService extends IService<EkpAlertTable> {
void pushBudgetAlertInfoTomanager(int type); void pushBudgetAlertInfoTomanager(int type);
void tesxWxExcelApi();
} }
...@@ -23,6 +23,8 @@ import com.yifu.cloud.plus.v1.ekp.entity.EkpBudgetWarningInfo; ...@@ -23,6 +23,8 @@ import com.yifu.cloud.plus.v1.ekp.entity.EkpBudgetWarningInfo;
import com.yifu.cloud.plus.v1.ekp.mapper.EkpAlertTableMapper; import com.yifu.cloud.plus.v1.ekp.mapper.EkpAlertTableMapper;
import com.yifu.cloud.plus.v1.ekp.mapper.EkpBudgetWarningInfoMapper; import com.yifu.cloud.plus.v1.ekp.mapper.EkpBudgetWarningInfoMapper;
import com.yifu.cloud.plus.v1.ekp.service.EkpAlertTableService; import com.yifu.cloud.plus.v1.ekp.service.EkpAlertTableService;
import com.yifu.cloud.plus.v1.ekp.util.SmartSheetDebugService;
import com.yifu.cloud.plus.v1.ekp.util.WeComSmartSheetManager;
import com.yifu.cloud.plus.v1.ekp.vo.BudgetWarningVo; import com.yifu.cloud.plus.v1.ekp.vo.BudgetWarningVo;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUser; import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUser;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysUserListVo; import com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysUserListVo;
...@@ -57,6 +59,15 @@ public class EkpAlertTableServiceImpl extends ServiceImpl<EkpAlertTableMapper, E ...@@ -57,6 +59,15 @@ public class EkpAlertTableServiceImpl extends ServiceImpl<EkpAlertTableMapper, E
@Autowired @Autowired
private EkpBudgetWarningInfoMapper warningInfoMapper; private EkpBudgetWarningInfoMapper warningInfoMapper;
@Autowired
private WeComSmartSheetManager smartSheetManager;
@Autowired
private SmartSheetDebugService debugService;
/** /**
...@@ -243,6 +254,7 @@ public class EkpAlertTableServiceImpl extends ServiceImpl<EkpAlertTableMapper, E ...@@ -243,6 +254,7 @@ public class EkpAlertTableServiceImpl extends ServiceImpl<EkpAlertTableMapper, E
log.error("执行预算预警任务时发生异常:", e); log.error("执行预算预警任务时发生异常:", e);
} }
} }
/** /**
* @param config 预警配置 * @param config 预警配置
* @return 是否超额 * @return 是否超额
...@@ -296,4 +308,27 @@ public class EkpAlertTableServiceImpl extends ServiceImpl<EkpAlertTableMapper, E ...@@ -296,4 +308,27 @@ public class EkpAlertTableServiceImpl extends ServiceImpl<EkpAlertTableMapper, E
} }
return false; return false;
} }
@Override
public void tesxWxExcelApi() {
/*RestTemplate restTemplate = new RestTemplate();
String accessToken = wxConfig.getAccessToken(restTemplate);
Map<String, Object> requestBody = new HashMap<>();
//requestBody.put("spaceid", ""); // 非必填,空间spaceid。若指定spaceid,则fatherid也要同时指定
//requestBody.put("fatherid", "Sheet1"); // 非必填,父目录fileid, 在根目录时为空间spaceid
requestBody.put("doc_type", 10); // 文档类型, 3:文档 4:表格 10:智能表格
requestBody.put("doc_name", "发票基础信息"); // 文档名字(注意:文件名最多填255个字符, 超过255个字符会被截断)
requestBody.put("admin_users", new String[]{"FeiTaiLang_Fang"}); // 非必填,文档管理员userid
// 发送POST请求(示例使用Spring的RestTemplate)
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
restTemplate = new RestTemplate();
String createDocUrl = "https://qyapi.weixin.qq.com/cgi-bin/wedoc/create_doc?access_token="+accessToken;
ResponseEntity<String> response = restTemplate.postForEntity(createDocUrl,requestEntity, String.class);*/
//smartSheetManager.test(wxConfig);
debugService.debugInsertIssue(wxConfig);
}
} }
package com.yifu.cloud.plus.v1.ekp.util;
import lombok.Data;
/**
* @Author fxj
* @Date 2025/9/18
* @Description
* @Version 1.0
*/
@Data
public class FieldInfo {
private String fieldId;
private String title;
private String type;
@Override
public String toString() {
return "FieldInfo{" +
"fieldId='" + fieldId + '\'' +
", title='" + title + '\'' +
", type='" + type + '\'' +
'}';
}
}
package com.yifu.cloud.plus.v1.ekp.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yifu.cloud.plus.v1.ekp.config.WxConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author fxj
* @Date 2025/9/18
* @Description
* @Version 1.0
*/
@Service
public class SmartSheetDebugService {
@Autowired
private WeComSmartSheetDebugger debugger;
/**
* 调试企业微信智能表格记录添加问题
*
*/
public void debugInsertIssue(WxConfig wxConfig) {
String corpId = "wwbcb090af0dfe50e5";
String corpSecret = "16kqEL_eU-ARwYyqLgEBWHgxm8gXVnkzv_eJMLy9Np";
String docId = "dcTSMCj4lWV9MEP7lquZacOY8WS4g-iHxP_jVYA02tdYwb2YYc3BpDR8NrFWkEngHUg8abET4eTIpo9Zv-571NEA";
String sheetId = "q979lj";
System.out.println("=== 开始调试企业微信智能表格记录添加问题 ===");
// 1. 获取并打印表格字段信息
System.out.println("=== 表格字段信息 ===");
List<WeComSmartSheetDebugger.FieldInfo> fields = debugger.getSheetFields(wxConfig,corpId, corpSecret, docId, sheetId);
if (fields.isEmpty()) {
System.out.println("未能获取到表格字段信息,请检查参数是否正确");
return;
}
for (WeComSmartSheetDebugger.FieldInfo field : fields) {
System.out.println("字段ID: " + field.getFieldId() +
", 字段标题: '" + field.getTitle() + "'" +
", 字段类型: " + field.getType());
}
// 创建数据
List<Map<String, Object>> records = new ArrayList<>();
// 第一条记录
Map<String, Object> record1 = new HashMap<>();
record1.put("姓名", "文本内容1");
record1.put("部门", "文本内容2");
record1.put("年龄", "文本内容3");
records.add(record1);
// 第二条记录
Map<String, Object> record2 = new HashMap<>();
record2.put("姓名", "文本内容1");
record2.put("部门", "文本内容2");
record2.put("年龄", "文本内容3");
records.add(record2);
/* // 2. 准备要插入的数据(示例数据,请根据实际情况修改)
List<Map<String, String>> records = new ArrayList<>();
Map<String, Object> record = new HashMap<>();
Map<String, Object> values = new HashMap<>();
List<Map<String, String>> fieldContent = new ArrayList<>();
Map<String, String> textContent = new HashMap<>();
textContent.put("type", "text");
textContent.put("text", "张三");
fieldContent.add(textContent);
textContent = new HashMap<>();
textContent.put("type", "text");
textContent.put("text", "李四");
fieldContent.add(textContent);
// 注意:这里的字段名称必须与表格中的完全一致(包括大小写和空格)
values.put("姓名", fieldContent);
fieldContent = new ArrayList<>();
record = new HashMap<>();
values = new HashMap<>();
textContent = new HashMap<>();
textContent.put("type", "text");
textContent.put("text", "技术部");
fieldContent.add(textContent);
textContent = new HashMap<>();
textContent.put("type", "text");
textContent.put("text", "产品部");
fieldContent.add(textContent);
values.put("部门", fieldContent);
record.put("values", values);
records.add(record);*/
/*fieldContent = new ArrayList<>();
record = new HashMap<>();
values = new HashMap<>();
textContent = new HashMap<>();
textContent.put("type", "text");
textContent.put("text", "技术部");
fieldContent.add(textContent);
textContent = new HashMap<>();
textContent.put("type", "text");
textContent.put("text", "产品部");
fieldContent.add(textContent);
values.put("部门", fieldContent);
record.put("values", values);
records.add(record);*/
// 3. 验证字段匹配性
System.out.println("\n=== 字段匹配性验证 ===");
List<String> mismatches = debugger.validateFields(wxConfig,corpId, corpSecret, docId, sheetId, records);
if (mismatches.isEmpty()) {
System.out.println("所有字段都匹配");
} else {
System.out.println("以下字段不匹配:");
for (String mismatch : mismatches) {
System.out.println("- " + mismatch);
}
System.out.println("请根据以上信息修正数据后重试");
return;
}
// 4. 测试插入数据并查看详细响应
System.out.println("\n=== 测试数据插入 ===");
String response = debugger.testInsertData(wxConfig,corpId, corpSecret, docId, sheetId, records);
System.out.println("API响应: " + response);
System.out.println("\n=== 调试完成 ===");
}
}
\ No newline at end of file
package com.yifu.cloud.plus.v1.ekp.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* 企业微信智能表格字段信息获取工具类
* 用于获取智能表格的字段信息,帮助诊断"字段不存在"错误
*/
@Component
public class WeComSheetFieldFetcher {
/**
* 企业微信API基础URL
*/
private static final String WE_COM_API_BASE_URL = "https://qyapi.weixin.qq.com/cgi-bin";
/**
* 获取access_token的接口路径
*/
private static final String GET_TOKEN_PATH = "/gettoken";
/**
* 获取智能表格字段信息的接口路径
*/
private static final String GET_FIELDS_PATH = "/wedoc/smartsheet/get_fields";
/**
* 获取智能表格字段信息
*
* @param corpId 企业ID
* @param corpSecret 应用的密钥
* @param docId 文档ID
* @param sheetId 工作表ID
* @return 字段信息列表
*/
public List<FieldInfo> getSheetFields(String corpId, String corpSecret, String docId, String sheetId) {
List<FieldInfo> fieldInfos = new ArrayList<>();
try {
// 1. 获取access_token
String accessToken = getAccessToken(corpId, corpSecret);
if (accessToken == null || accessToken.isEmpty()) {
System.err.println("获取access_token失败");
return fieldInfos;
}
// 2. 获取字段信息
fieldInfos = fetchFields(accessToken, docId, sheetId);
} catch (Exception e) {
System.err.println("获取表格字段信息时发生异常: " + e.getMessage());
e.printStackTrace();
}
return fieldInfos;
}
/**
* 获取企业微信access_token
*
* @param corpId 企业ID
* @param corpSecret 应用密钥
* @return access_token
*/
private String getAccessToken(String corpId, String corpSecret) {
try {
String url = WE_COM_API_BASE_URL + GET_TOKEN_PATH + "?corpid=" + corpId + "&corpsecret=" + corpSecret;
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
JSONObject jsonObject = JSON.parseObject(response.getBody());
if (jsonObject.getInteger("errcode") == 0) {
return jsonObject.getString("access_token");
} else {
System.err.println("获取access_token失败,错误代码:" + jsonObject.getInteger("errcode")
+ ",错误信息:" + jsonObject.getString("errmsg"));
return null;
}
}
} catch (Exception e) {
System.err.println("获取access_token时发生异常: " + e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 获取字段信息
*
* @param accessToken 访问令牌
* @param docId 文档ID
* @param sheetId 工作表ID
* @return 字段信息列表
*/
private List<FieldInfo> fetchFields(String accessToken, String docId, String sheetId) {
List<FieldInfo> fieldInfos = new ArrayList<>();
try {
String url = WE_COM_API_BASE_URL + GET_FIELDS_PATH + "?access_token=" + accessToken;
RestTemplate restTemplate = new RestTemplate();
// 构造请求体
JSONObject requestBody = new JSONObject();
requestBody.put("docid", docId);
requestBody.put("sheet_id", sheetId);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(requestBody.toJSONString(), headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
JSONObject jsonObject = JSON.parseObject(response.getBody());
if (jsonObject.getInteger("errcode") == 0) {
JSONArray fields = jsonObject.getJSONArray("fields");
if (fields != null && !fields.isEmpty()) {
for (int i = 0; i < fields.size(); i++) {
JSONObject field = fields.getJSONObject(i);
FieldInfo fieldInfo = new FieldInfo();
fieldInfo.setFieldId(field.getString("field_id"));
fieldInfo.setTitle(field.getString("title"));
fieldInfo.setType(field.getString("type"));
fieldInfos.add(fieldInfo);
}
}
} else {
System.err.println("获取字段信息失败,错误代码:" + jsonObject.getInteger("errcode")
+ ",错误信息:" + jsonObject.getString("errmsg"));
}
}
} catch (Exception e) {
System.err.println("获取字段信息时发生异常: " + e.getMessage());
e.printStackTrace();
}
return fieldInfos;
}
/**
* 使用示例
*
* @param args 命令行参数
*/
public static void main(String[] args) {
WeComSheetFieldFetcher fetcher = new WeComSheetFieldFetcher();
// 示例数据
String corpId = "your_corp_id";
String corpSecret = "your_corp_secret";
String docId = "your_doc_id";
String sheetId = "your_sheet_id";
// 获取字段信息列表
List<FieldInfo> fields = fetcher.getSheetFields(corpId, corpSecret, docId, sheetId);
System.out.println("获取到的字段信息列表: " + fields);
// 打印每个字段的详细信息
for (FieldInfo field : fields) {
System.out.println("字段ID: " + field.getFieldId() +
", 字段标题: " + field.getTitle() +
", 字段类型: " + field.getType());
}
}
}
\ No newline at end of file
package com.yifu.cloud.plus.v1.ekp.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yifu.cloud.plus.v1.ekp.config.WxConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* 企业微信智能表格SheetId获取工具类
* 用于通过文档ID获取表格ID
*/
@Data
@Component
public class WeComSheetIdFetcher {
/**
* 企业微信API基础URL
*/
private static final String WE_COM_API_BASE_URL = "https://qyapi.weixin.qq.com/cgi-bin";
/**
* 获取access_token的接口路径
*/
private static final String GET_TOKEN_PATH = "/gettoken";
/**
* 查询子表的接口路径
*/
private static final String GET_SHEET_PATH = "/wedoc/smartsheet/get_sheet";
/**
* 通过文档ID获取表格ID列表
*
* @param docId 文档ID
* @return 表格ID列表
*/
public List<String> getSheetIds(WxConfig wxConfig,String docId) {
List<String> sheetIds = new ArrayList<>();
try {
RestTemplate restTemplate = new RestTemplate();
// 1. 获取access_token
String accessToken = wxConfig.getAccessToken(restTemplate);
//getAccessToken(corpId, corpSecret);
if (accessToken == null || accessToken.isEmpty()) {
System.err.println("获取access_token失败");
return sheetIds;
}
// 2. 查询子表信息
List<SheetInfo> sheetInfos = getSheetInfo(accessToken, docId);
for (SheetInfo sheetInfo : sheetInfos) {
// 只返回智能表类型的sheet_id
if ("smartsheet".equals(sheetInfo.getType())) {
sheetIds.add(sheetInfo.getSheetId());
}
}
} catch (Exception e) {
System.err.println("获取表格ID时发生异常: " + e.getMessage());
e.printStackTrace();
}
return sheetIds;
}
/**
* 获取企业微信access_token
*
* @param corpId 企业ID
* @param corpSecret 应用密钥
* @return access_token
*/
private String getAccessToken(String corpId, String corpSecret) {
try {
String url = WE_COM_API_BASE_URL + GET_TOKEN_PATH + "?corpid=" + corpId + "&corpsecret=" + corpSecret;
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
JSONObject jsonObject = JSON.parseObject(response.getBody());
if (jsonObject.getInteger("errcode") == 0) {
return jsonObject.getString("access_token");
} else {
System.err.println("获取access_token失败,错误代码:" + jsonObject.getInteger("errcode")
+ ",错误信息:" + jsonObject.getString("errmsg"));
return null;
}
}
} catch (Exception e) {
System.err.println("获取access_token时发生异常: " + e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 查询子表信息
*
* @param accessToken 访问令牌
* @param docId 文档ID
* @return 子表信息列表
*/
private List<SheetInfo> getSheetInfo(String accessToken, String docId) {
List<SheetInfo> sheetInfos = new ArrayList<>();
try {
String url = WE_COM_API_BASE_URL + GET_SHEET_PATH + "?access_token=" + accessToken;
RestTemplate restTemplate = new RestTemplate();
// 构造请求体
JSONObject requestBody = new JSONObject();
requestBody.put("docid", docId);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(requestBody.toJSONString(), headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
JSONObject jsonObject = JSON.parseObject(response.getBody());
if (jsonObject.getInteger("errcode") == 0) {
JSONArray sheets = jsonObject.getJSONArray("sheet_list");
if (sheets != null && !sheets.isEmpty()) {
for (int i = 0; i < sheets.size(); i++) {
JSONObject sheet = sheets.getJSONObject(i);
SheetInfo sheetInfo = new SheetInfo();
sheetInfo.setSheetId(sheet.getString("sheet_id"));
sheetInfo.setTitle(sheet.getString("title"));
sheetInfo.setType(sheet.getString("type"));
sheetInfos.add(sheetInfo);
}
}
} else {
System.err.println("查询子表信息失败,错误代码:" + jsonObject.getInteger("errcode")
+ ",错误信息:" + jsonObject.getString("errmsg"));
}
}
} catch (Exception e) {
System.err.println("查询子表信息时发生异常: " + e.getMessage());
e.printStackTrace();
}
return sheetInfos;
}
/**
* 子表信息内部类
*/
public static class SheetInfo {
private String sheetId;
private String title;
private String type; // smartsheet: 智能表, dashboard: 仪表盘, external: 说明页
public String getSheetId() {
return sheetId;
}
public void setSheetId(String sheetId) {
this.sheetId = sheetId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "SheetInfo{" +
"sheetId='" + sheetId + '\'' +
", title='" + title + '\'' +
", type='" + type + '\'' +
'}';
}
}
/**
* 使用示例
*
* @param args 命令行参数
*/
public static void main(String[] args) {
WeComSheetIdFetcher fetcher = new WeComSheetIdFetcher();
// 示例数据
String corpId = "your_corp_id";
String corpSecret = "your_corp_secret";
String docId = "your_doc_id";
// 获取表格ID列表
/* List<String> sheetIds = fetcher.getSheetIds(docId);
System.out.println("获取到的表格ID列表: " + sheetIds);*/
}
}
\ No newline at end of file
package com.yifu.cloud.plus.v1.ekp.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yifu.cloud.plus.v1.ekp.config.WxConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
/**
* 企业微信智能表格数据插入工具类
* 用于向已有的企业微信智能表格中插入数据
*/
@Component
public class WeComSmartSheetDataInserter {
@Autowired
private WxConfig wxConfig;
/**
* 企业微信API基础URL
*/
private static final String WE_COM_API_BASE_URL = "https://qyapi.weixin.qq.com/cgi-bin";
/**
* 获取access_token的接口路径
*/
private static final String GET_TOKEN_PATH = "/gettoken";
/**
* 智能表格添加记录的接口路径
*/
private static final String ADD_RECORDS_PATH = "/wedoc/smartsheet/add_records";
/**
* 向企业微信智能表格中插入数据
*
* @param docId 文档ID
* @param sheetId 表格ID
* @param records 要插入的记录列表
* @return 插入结果
*/
public boolean insertData(String docId, String sheetId, List<Map<String, Object>> records) {
try {
RestTemplate restTemplate = new RestTemplate();
// 1. 获取access_token
String accessToken = wxConfig.getAccessToken(restTemplate);
//getAccessToken(corpId, corpSecret);
if (accessToken == null || accessToken.isEmpty()) {
System.err.println("获取access_token失败");
return false;
}
// 2. 构造请求体
JSONObject requestBody = new JSONObject();
requestBody.put("docid", docId);
requestBody.put("sheet_id", sheetId);
requestBody.put("key_type", "CELL_VALUE_KEY_TYPE_FIELD_TITLE");
// 构造records数组
new com.alibaba.fastjson.JSONArray();
requestBody.put("records", records);
// 3. 发送请求插入数据
boolean result = addRecords(accessToken, requestBody);
if (result) {
System.out.println("数据插入成功");
} else {
System.err.println("数据插入失败");
}
return result;
} catch (Exception e) {
System.err.println("插入数据时发生异常: " + e.getMessage());
e.printStackTrace();
return false;
}
}
/**
* 获取企业微信access_token
*
* @param corpId 企业ID
* @param corpSecret 应用密钥
* @return access_token
*/
private String getAccessToken(String corpId, String corpSecret) {
try {
String url = WE_COM_API_BASE_URL + GET_TOKEN_PATH + "?corpid=" + corpId + "&corpsecret=" + corpSecret;
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
JSONObject jsonObject = JSON.parseObject(response.getBody());
if (jsonObject.getInteger("errcode") == 0) {
return jsonObject.getString("access_token");
} else {
System.err.println("获取access_token失败,错误代码:" + jsonObject.getInteger("errcode")
+ ",错误信息:" + jsonObject.getString("errmsg"));
return null;
}
}
} catch (Exception e) {
System.err.println("获取access_token时发生异常: " + e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 向智能表格添加记录
*
* @param accessToken 访问令牌
* @param requestBody 请求体
* @return 是否成功
*/
private boolean addRecords(String accessToken, JSONObject requestBody) {
try {
String url = WE_COM_API_BASE_URL + ADD_RECORDS_PATH + "?access_token=" + accessToken;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(requestBody.toJSONString(), headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
JSONObject jsonObject = JSON.parseObject(response.getBody());
if (jsonObject.getInteger("errcode") == 0) {
return true;
} else {
System.err.println("添加记录失败,错误代码:" + jsonObject.getInteger("errcode")
+ ",错误信息:" + jsonObject.getString("errmsg"));
return false;
}
}
} catch (Exception e) {
System.err.println("添加记录时发生异常: " + e.getMessage());
e.printStackTrace();
}
return false;
}
/**
* 使用示例
*
* @param args 命令行参数
*/
public static void main(String[] args) {
WeComSmartSheetDataInserter inserter = new WeComSmartSheetDataInserter();
// 示例数据
String corpId = "your_corp_id";
String corpSecret = "your_corp_secret";
String docId = "your_doc_id";
String sheetId = "your_sheet_id";
// 构造要插入的数据
new java.util.ArrayList<Map<String, Object>>();
/*
// 示例记录
Map<String, Object> record1 = new HashMap<>();
Map<String, Object> values1 = new HashMap<>();
values1.put("姓名", "张三");
values1.put("年龄", 25);
values1.put("部门", "技术部");
record1.put("values", values1);
Map<String, Object> record2 = new HashMap<>();
Map<String, Object> values2 = new HashMap<>();
values2.put("姓名", "李四");
values2.put("年龄", 30);
values2.put("部门", "市场部");
record2.put("values", values2);
records.add(record1);
records.add(record2);
// 执行插入
boolean success = inserter.insertData(corpId, corpSecret, docId, sheetId, records);
if (success) {
System.out.println("数据插入成功");
} else {
System.out.println("数据插入失败");
}
*/
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment