open_lark/service/cloud_docs/sheets/v3/data_operation/
split_cells.rs

1use reqwest::Method;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    core::{
6        api_req::ApiRequest,
7        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
8        constants::AccessTokenType,
9        http::Transport,
10        req_option::RequestOption,
11        SDKResult,
12    },
13    impl_executable_builder_owned,
14    service::sheets::v3::DataOperationService,
15};
16
17impl DataOperationService {
18    /// 拆分单元格
19    pub async fn split_cells(
20        &self,
21        request: SplitCellsRequest,
22        option: Option<RequestOption>,
23    ) -> SDKResult<BaseResponse<SplitCellsResponseData>> {
24        let mut api_req = request.api_request;
25        api_req.http_method = Method::POST;
26        api_req.api_path = format!(
27            "/open-apis/sheets/v3/spreadsheets/{}/sheets/{}/unmerge_cells",
28            request.spreadsheet_token, request.sheet_id
29        );
30        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
31
32        let api_resp = Transport::request(api_req, &self.config, option).await?;
33
34        Ok(api_resp)
35    }
36}
37
38/// 拆分单元格请求
39#[derive(Default, Debug, Serialize, Deserialize)]
40pub struct SplitCellsRequest {
41    #[serde(skip)]
42    api_request: ApiRequest,
43    /// spreadsheet 的 token
44    spreadsheet_token: String,
45    /// sheet 的 id
46    sheet_id: String,
47    /// 拆分范围
48    range: String,
49}
50
51impl SplitCellsRequest {
52    pub fn builder() -> SplitCellsRequestBuilder {
53        SplitCellsRequestBuilder::default()
54    }
55}
56
57#[derive(Default)]
58pub struct SplitCellsRequestBuilder {
59    request: SplitCellsRequest,
60}
61
62impl SplitCellsRequestBuilder {
63    pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
64        self.request.spreadsheet_token = spreadsheet_token.to_string();
65        self
66    }
67
68    pub fn sheet_id(mut self, sheet_id: impl ToString) -> Self {
69        self.request.sheet_id = sheet_id.to_string();
70        self
71    }
72
73    pub fn range(mut self, range: impl ToString) -> Self {
74        self.request.range = range.to_string();
75        self
76    }
77
78    pub fn build(mut self) -> SplitCellsRequest {
79        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
80        self.request
81    }
82}
83
84/// 拆分单元格响应体最外层
85#[derive(Deserialize, Debug)]
86pub struct SplitCellsResponseData {
87    /// 拆分后的范围
88    pub unmerged_range: String,
89}
90
91impl ApiResponseTrait for SplitCellsResponseData {
92    fn data_format() -> ResponseFormat {
93        ResponseFormat::Data
94    }
95}
96
97impl_executable_builder_owned!(
98    SplitCellsRequestBuilder,
99    DataOperationService,
100    SplitCellsRequest,
101    BaseResponse<SplitCellsResponseData>,
102    split_cells
103);
104
105#[cfg(test)]
106mod test {
107    use serde_json::json;
108
109    use super::SplitCellsResponseData;
110
111    #[test]
112    fn test_split_cells_response() {
113        let json = json!({
114            "unmerged_range": "A1:C3"
115        });
116
117        let response: SplitCellsResponseData = serde_json::from_value(json).unwrap();
118        assert_eq!(response.unmerged_range, "A1:C3");
119    }
120}