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

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    core::{
5        api_req::ApiRequest,
6        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
7        constants::AccessTokenType,
8        endpoints::cloud_docs::*,
9        req_option,
10        standard_response::StandardResponse,
11        SDKResult,
12    },
13    impl_executable_builder_owned,
14    service::sheets::v3::{
15        data_operation::{FindCondition, FindReplaceResult},
16        SpreadsheetSheetService,
17    },
18};
19
20#[derive(Serialize, Debug, Default)]
21pub struct ReplaceCellsRequest {
22    #[serde(skip)]
23    api_request: ApiRequest,
24    #[serde(skip)]
25    spreadsheet_token: String,
26    /// 工作表的id
27    #[serde(skip)]
28    sheet_id: String,
29    /// 查找条件
30    find_condition: FindCondition,
31    /// 查找的字符串,当search_by_regex字段为 true 时,该字段为正则表达式
32    ///
33    /// 示例值:"如下
34    ///
35    /// - 普通查找示例: "hello"
36    /// - 正则查找示例: "[A-Z]\w+""
37    find: String,
38    /// 替换的字符串
39    replacement: String,
40}
41
42impl ReplaceCellsRequest {
43    pub fn builder() -> ReplaceCellsRequestBuilder {
44        ReplaceCellsRequestBuilder::default()
45    }
46}
47
48#[derive(Default)]
49pub struct ReplaceCellsRequestBuilder {
50    request: ReplaceCellsRequest,
51}
52
53impl ReplaceCellsRequestBuilder {
54    pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
55        self.request.spreadsheet_token = spreadsheet_token.to_string();
56        self
57    }
58
59    pub fn sheet_id(mut self, sheet_id: impl ToString) -> Self {
60        self.request.sheet_id = sheet_id.to_string();
61        self
62    }
63
64    pub fn find(mut self, find: impl ToString) -> Self {
65        self.request.find = find.to_string();
66        self
67    }
68
69    pub fn range(mut self, range: impl ToString) -> Self {
70        self.request.find_condition.range = range.to_string();
71        self
72    }
73
74    pub fn match_case(mut self, match_case: bool) -> Self {
75        self.request.find_condition.match_case = Some(match_case);
76        self
77    }
78
79    pub fn match_entire_cell(mut self, match_entire_cell: bool) -> Self {
80        self.request.find_condition.match_entire_cell = Some(match_entire_cell);
81        self
82    }
83
84    pub fn search_by_regex(mut self, search_by_regex: bool) -> Self {
85        self.request.find_condition.search_by_regex = Some(search_by_regex);
86        self
87    }
88
89    pub fn include_formulas(mut self, include_formulas: bool) -> Self {
90        self.request.find_condition.include_formulas = Some(include_formulas);
91        self
92    }
93
94    pub fn replacement(mut self, replacement: impl ToString) -> Self {
95        self.request.replacement = replacement.to_string();
96        self
97    }
98
99    pub fn build(mut self) -> ReplaceCellsRequest {
100        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
101        self.request
102    }
103}
104
105// Trait implementation
106impl_executable_builder_owned!(
107    ReplaceCellsRequestBuilder,
108    SpreadsheetSheetService,
109    ReplaceCellsRequest,
110    ReplaceCellsResponse,
111    replace_cells
112);
113
114/// 替换单元格响应
115#[derive(Deserialize, Debug)]
116pub struct ReplaceCellsResponse {
117    /// 符合查找条件并替换的单元格信息
118    pub replace_result: FindReplaceResult,
119}
120
121impl ApiResponseTrait for ReplaceCellsResponse {
122    fn data_format() -> ResponseFormat {
123        ResponseFormat::Data
124    }
125}
126
127impl SpreadsheetSheetService {
128    /// 替换单元格
129    ///
130    /// 按照指定的条件查找子表的某个范围内的数据符合条件的单元格并替换值,返回替换成功的单元格位置。
131    /// 一次请求最多允许替换5000个单元格,如果超过请将range缩小范围再操作。请求体中的
132    /// range、find、replacement 字段必填。
133    pub async fn replace_cells(
134        &self,
135        request: ReplaceCellsRequest,
136        option: Option<req_option::RequestOption>,
137    ) -> SDKResult<ReplaceCellsResponse> {
138        let mut api_req = request.api_request;
139        api_req.api_path = SHEETS_V3_SPREADSHEET_SHEET_REPLACE
140            .replace("{}", &request.spreadsheet_token)
141            .replace("{}", &request.sheet_id);
142        api_req.http_method = reqwest::Method::POST;
143        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::App];
144
145        let api_resp: BaseResponse<ReplaceCellsResponse> =
146            crate::core::http::Transport::request(api_req, &self.config, option).await?;
147        api_resp.into_result()
148    }
149}