open_lark/service/cloud_docs/sheets/v3/condition_format/
update.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, ConditionFormatRule};
18
19impl SpreadsheetSheetService {
20    /// 批量更新条件格式
21    pub async fn update_condition_formats(
22        &self,
23        request: UpdateConditionFormatsRequest,
24        option: Option<RequestOption>,
25    ) -> SDKResult<BaseResponse<UpdateConditionFormatsResponseData>> {
26        let mut api_req = request.api_request;
27        api_req.http_method = Method::PUT;
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        let api_resp = Transport::request(api_req, &self.config, option).await?;
34
35        Ok(api_resp)
36    }
37}
38
39/// 批量更新条件格式请求
40#[derive(Default, Debug, Serialize, Deserialize)]
41pub struct UpdateConditionFormatsRequest {
42    #[serde(skip)]
43    api_request: ApiRequest,
44    /// spreadsheet 的 token
45    spreadsheet_token: String,
46    /// sheet 的 id
47    sheet_id: String,
48    /// 要更新的条件格式规则列表
49    condition_formats: Vec<UpdateConditionFormatRule>,
50}
51
52impl UpdateConditionFormatsRequest {
53    pub fn builder() -> UpdateConditionFormatsRequestBuilder {
54        UpdateConditionFormatsRequestBuilder::default()
55    }
56}
57
58#[derive(Default)]
59pub struct UpdateConditionFormatsRequestBuilder {
60    request: UpdateConditionFormatsRequest,
61}
62
63impl UpdateConditionFormatsRequestBuilder {
64    pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
65        self.request.spreadsheet_token = spreadsheet_token.to_string();
66        self
67    }
68
69    pub fn sheet_id(mut self, sheet_id: impl ToString) -> Self {
70        self.request.sheet_id = sheet_id.to_string();
71        self
72    }
73
74    pub fn condition_formats(mut self, condition_formats: Vec<UpdateConditionFormatRule>) -> Self {
75        self.request.condition_formats = condition_formats;
76        self
77    }
78
79    pub fn add_condition_format(mut self, condition_format: UpdateConditionFormatRule) -> Self {
80        self.request.condition_formats.push(condition_format);
81        self
82    }
83
84    pub fn build(mut self) -> UpdateConditionFormatsRequest {
85        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
86        self.request
87    }
88}
89
90/// 更新条件格式规则
91#[derive(Debug, Serialize, Deserialize)]
92pub struct UpdateConditionFormatRule {
93    /// 条件格式 ID
94    pub cf_id: String,
95    /// 条件格式规则
96    #[serde(flatten)]
97    pub rule: ConditionFormatRule,
98}
99
100impl UpdateConditionFormatRule {
101    pub fn new(cf_id: impl ToString, rule: ConditionFormatRule) -> Self {
102        Self {
103            cf_id: cf_id.to_string(),
104            rule,
105        }
106    }
107}
108
109/// 批量更新条件格式响应体最外层
110#[derive(Deserialize, Debug)]
111pub struct UpdateConditionFormatsResponseData {
112    /// 更新后的条件格式列表
113    pub items: Vec<ConditionFormatInfo>,
114    /// 更新成功的数量
115    #[serde(default)]
116    pub updated_count: u32,
117}
118
119impl ApiResponseTrait for UpdateConditionFormatsResponseData {
120    fn data_format() -> ResponseFormat {
121        ResponseFormat::Data
122    }
123}
124
125#[cfg(test)]
126#[allow(unused_variables, unused_unsafe)]
127mod test {
128    use super::*;
129    use serde_json::json;
130
131    #[test]
132    fn test_update_condition_formats_response() {
133        let json = json!({
134            "items": [
135                {
136                    "cf_id": "cf_001",
137                    "range": "A1:A15",
138                    "condition_type": "NUMBER_GREATER",
139                    "condition_values": ["200"],
140                    "format": {
141                        "background_color": "#00FF00",
142                        "text_color": "#000000",
143                        "bold": false
144                    }
145                }
146            ],
147            "updated_count": 1
148        });
149
150        let response: UpdateConditionFormatsResponseData = serde_json::from_value(json).unwrap();
151        assert_eq!(response.items.len(), 1);
152        assert_eq!(response.items[0].cf_id, "cf_001");
153        assert_eq!(response.updated_count, 1);
154    }
155}