use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
constants::AccessTokenType,
endpoints::cloud_docs::*,
req_option,
standard_response::StandardResponse,
SDKResult,
},
impl_executable_builder_owned,
service::sheets::v3::{
data_operation::{FindCondition, FindReplaceResult},
SpreadsheetSheetService,
},
};
#[derive(Serialize, Debug, Default)]
pub struct ReplaceCellsRequest {
#[serde(skip)]
api_request: ApiRequest,
#[serde(skip)]
spreadsheet_token: String,
#[serde(skip)]
sheet_id: String,
find_condition: FindCondition,
find: String,
replacement: String,
}
impl ReplaceCellsRequest {
pub fn builder() -> ReplaceCellsRequestBuilder {
ReplaceCellsRequestBuilder::default()
}
}
#[derive(Default)]
pub struct ReplaceCellsRequestBuilder {
request: ReplaceCellsRequest,
}
impl ReplaceCellsRequestBuilder {
pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
self.request.spreadsheet_token = spreadsheet_token.to_string();
self
}
pub fn sheet_id(mut self, sheet_id: impl ToString) -> Self {
self.request.sheet_id = sheet_id.to_string();
self
}
pub fn find(mut self, find: impl ToString) -> Self {
self.request.find = find.to_string();
self
}
pub fn range(mut self, range: impl ToString) -> Self {
self.request.find_condition.range = range.to_string();
self
}
pub fn match_case(mut self, match_case: bool) -> Self {
self.request.find_condition.match_case = Some(match_case);
self
}
pub fn match_entire_cell(mut self, match_entire_cell: bool) -> Self {
self.request.find_condition.match_entire_cell = Some(match_entire_cell);
self
}
pub fn search_by_regex(mut self, search_by_regex: bool) -> Self {
self.request.find_condition.search_by_regex = Some(search_by_regex);
self
}
pub fn include_formulas(mut self, include_formulas: bool) -> Self {
self.request.find_condition.include_formulas = Some(include_formulas);
self
}
pub fn replacement(mut self, replacement: impl ToString) -> Self {
self.request.replacement = replacement.to_string();
self
}
pub fn build(mut self) -> ReplaceCellsRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
impl_executable_builder_owned!(
ReplaceCellsRequestBuilder,
SpreadsheetSheetService,
ReplaceCellsRequest,
ReplaceCellsResponse,
replace_cells
);
#[derive(Deserialize, Debug)]
pub struct ReplaceCellsResponse {
pub replace_result: FindReplaceResult,
}
impl ApiResponseTrait for ReplaceCellsResponse {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl SpreadsheetSheetService {
pub async fn replace_cells(
&self,
request: ReplaceCellsRequest,
option: Option<req_option::RequestOption>,
) -> SDKResult<ReplaceCellsResponse> {
let mut api_req = request.api_request;
api_req.api_path = SHEETS_V3_SPREADSHEET_SHEET_REPLACE
.replace("{}", &request.spreadsheet_token)
.replace("{}", &request.sheet_id);
api_req.http_method = reqwest::Method::POST;
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::App];
let api_resp: BaseResponse<ReplaceCellsResponse> =
crate::core::http::Transport::request(api_req, &self.config, option).await?;
api_resp.into_result()
}
}