open_lark/service/cloud_docs/sheets/v3/condition_format/
get.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        endpoints::cloud_docs::*,
10        http::Transport,
11        req_option::RequestOption,
12        SDKResult,
13    },
14    service::sheets::v3::SpreadsheetSheetService,
15};
16
17use super::create::ConditionFormatInfo;
18
19impl SpreadsheetSheetService {
20    /// 批量获取条件格式
21    pub async fn get_condition_formats(
22        &self,
23        request: GetConditionFormatsRequest,
24        option: Option<RequestOption>,
25    ) -> SDKResult<BaseResponse<GetConditionFormatsResponseData>> {
26        let mut api_req = request.api_request;
27        api_req.http_method = Method::GET;
28        api_req.api_path = SHEETS_V3_SPREADSHEET_CONDITION_FORMAT
29            .replace("{}", &request.spreadsheet_token)
30            .replace("{}", &request.sheet_id);
31        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
32
33        // 添加查询参数
34        if let Some(range) = &request.range {
35            api_req.query_params.insert("range", range.clone());
36        }
37
38        let api_resp = Transport::request(api_req, &self.config, option).await?;
39
40        Ok(api_resp)
41    }
42}
43
44/// 批量获取条件格式请求
45#[derive(Default, Debug, Serialize, Deserialize)]
46pub struct GetConditionFormatsRequest {
47    #[serde(skip)]
48    api_request: ApiRequest,
49    /// spreadsheet 的 token
50    spreadsheet_token: String,
51    /// sheet 的 id
52    sheet_id: String,
53    /// 可选:查询范围,如果不提供则返回整个工作表的条件格式
54    #[serde(skip_serializing_if = "Option::is_none")]
55    range: Option<String>,
56}
57
58impl GetConditionFormatsRequest {
59    pub fn builder() -> GetConditionFormatsRequestBuilder {
60        GetConditionFormatsRequestBuilder::default()
61    }
62}
63
64#[derive(Default)]
65pub struct GetConditionFormatsRequestBuilder {
66    request: GetConditionFormatsRequest,
67}
68
69impl GetConditionFormatsRequestBuilder {
70    pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
71        self.request.spreadsheet_token = spreadsheet_token.to_string();
72        self
73    }
74
75    pub fn sheet_id(mut self, sheet_id: impl ToString) -> Self {
76        self.request.sheet_id = sheet_id.to_string();
77        self
78    }
79
80    pub fn range(mut self, range: impl ToString) -> Self {
81        self.request.range = Some(range.to_string());
82        self
83    }
84
85    pub fn build(mut self) -> GetConditionFormatsRequest {
86        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
87        self.request
88    }
89}
90
91/// 批量获取条件格式响应体最外层
92#[derive(Deserialize, Debug)]
93pub struct GetConditionFormatsResponseData {
94    /// 条件格式列表
95    pub items: Vec<ConditionFormatInfo>,
96    /// 是否还有更多数据
97    #[serde(default)]
98    pub has_more: bool,
99    /// 下次请求的页面标记
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub page_token: Option<String>,
102}
103
104impl ApiResponseTrait for GetConditionFormatsResponseData {
105    fn data_format() -> ResponseFormat {
106        ResponseFormat::Data
107    }
108}
109
110#[cfg(test)]
111#[allow(unused_variables, unused_unsafe)]
112mod test {
113    use super::*;
114    use serde_json::json;
115
116    #[test]
117    fn test_get_condition_formats_response() {
118        let json = json!({
119            "items": [
120                {
121                    "cf_id": "cf_001",
122                    "range": "A1:A10",
123                    "condition_type": "NUMBER_GREATER",
124                    "condition_values": ["100"],
125                    "format": {
126                        "background_color": "#FF0000",
127                        "text_color": "#FFFFFF",
128                        "bold": true
129                    }
130                },
131                {
132                    "cf_id": "cf_002",
133                    "range": "B1:B10",
134                    "condition_type": "TEXT_CONTAINS",
135                    "condition_values": ["重要"],
136                    "format": {
137                        "background_color": "#FFFF00",
138                        "text_color": "#000000"
139                    }
140                }
141            ],
142            "has_more": false
143        });
144
145        let response: GetConditionFormatsResponseData = serde_json::from_value(json).unwrap();
146        assert_eq!(response.items.len(), 2);
147        assert_eq!(response.items[0].cf_id, "cf_001");
148        assert_eq!(
149            response.items[1].condition_format.condition_type,
150            "TEXT_CONTAINS"
151        );
152        assert!(!response.has_more);
153    }
154}