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