use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{
core::{
api_req::ApiRequest,
api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
constants::AccessTokenType,
endpoints::cloud_docs::*,
http::Transport,
req_option::RequestOption,
SDKResult,
},
impl_executable_builder_owned,
service::sheets::v3::SpreadsheetSheetService,
};
impl SpreadsheetSheetService {
pub async fn create_condition_formats(
&self,
request: CreateConditionFormatsRequest,
option: Option<RequestOption>,
) -> SDKResult<BaseResponse<CreateConditionFormatsResponseData>> {
let mut api_req = request.api_request;
api_req.http_method = Method::POST;
api_req.api_path = SHEETS_V3_SPREADSHEET_CONDITION_FORMAT
.replace("{}", &request.spreadsheet_token)
.replace("{}", &request.sheet_id);
api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::User];
let api_resp = Transport::request(api_req, &self.config, option).await?;
Ok(api_resp)
}
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct CreateConditionFormatsRequest {
#[serde(skip)]
api_request: ApiRequest,
spreadsheet_token: String,
sheet_id: String,
condition_formats: Vec<ConditionFormatRule>,
}
impl CreateConditionFormatsRequest {
pub fn builder() -> CreateConditionFormatsRequestBuilder {
CreateConditionFormatsRequestBuilder::default()
}
}
#[derive(Default)]
pub struct CreateConditionFormatsRequestBuilder {
request: CreateConditionFormatsRequest,
}
impl CreateConditionFormatsRequestBuilder {
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 condition_formats(mut self, condition_formats: Vec<ConditionFormatRule>) -> Self {
self.request.condition_formats = condition_formats;
self
}
pub fn add_condition_format(mut self, condition_format: ConditionFormatRule) -> Self {
self.request.condition_formats.push(condition_format);
self
}
pub fn build(mut self) -> CreateConditionFormatsRequest {
self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
self.request
}
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct ConditionFormatRule {
pub range: String,
pub condition_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub condition_values: Option<Vec<String>>,
pub format: FormatStyle,
#[serde(skip_serializing_if = "Option::is_none")]
pub cf_id: Option<String>,
}
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct FormatStyle {
#[serde(skip_serializing_if = "Option::is_none")]
pub background_color: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text_color: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bold: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub italic: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub underline: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub strikethrough: Option<bool>,
}
impl ConditionFormatRule {
pub fn number_comparison(
range: impl ToString,
comparison_type: impl ToString,
value: f64,
format: FormatStyle,
) -> Self {
Self {
range: range.to_string(),
condition_type: comparison_type.to_string(),
condition_values: Some(vec![value.to_string()]),
format,
cf_id: None,
}
}
pub fn greater_than(range: impl ToString, value: f64, format: FormatStyle) -> Self {
Self::number_comparison(range, "NUMBER_GREATER", value, format)
}
pub fn less_than(range: impl ToString, value: f64, format: FormatStyle) -> Self {
Self::number_comparison(range, "NUMBER_LESS", value, format)
}
pub fn equal_to(range: impl ToString, value: f64, format: FormatStyle) -> Self {
Self::number_comparison(range, "NUMBER_EQ", value, format)
}
pub fn text_contains(range: impl ToString, text: impl ToString, format: FormatStyle) -> Self {
Self {
range: range.to_string(),
condition_type: "TEXT_CONTAINS".to_string(),
condition_values: Some(vec![text.to_string()]),
format,
cf_id: None,
}
}
pub fn duplicate_values(range: impl ToString, format: FormatStyle) -> Self {
Self {
range: range.to_string(),
condition_type: "DUPLICATE".to_string(),
condition_values: None,
format,
cf_id: None,
}
}
pub fn blank_values(range: impl ToString, format: FormatStyle) -> Self {
Self {
range: range.to_string(),
condition_type: "BLANK".to_string(),
condition_values: None,
format,
cf_id: None,
}
}
}
impl FormatStyle {
pub fn background_color(color: impl ToString) -> Self {
Self {
background_color: Some(color.to_string()),
text_color: None,
bold: None,
italic: None,
underline: None,
strikethrough: None,
}
}
pub fn text_color(color: impl ToString) -> Self {
Self {
background_color: None,
text_color: Some(color.to_string()),
bold: None,
italic: None,
underline: None,
strikethrough: None,
}
}
pub fn font_style(bold: bool, italic: bool, underline: bool) -> Self {
Self {
background_color: None,
text_color: None,
bold: Some(bold),
italic: Some(italic),
underline: Some(underline),
strikethrough: None,
}
}
pub fn with_background_color(mut self, color: impl ToString) -> Self {
self.background_color = Some(color.to_string());
self
}
pub fn with_text_color(mut self, color: impl ToString) -> Self {
self.text_color = Some(color.to_string());
self
}
pub fn with_bold(mut self, bold: bool) -> Self {
self.bold = Some(bold);
self
}
pub fn with_italic(mut self, italic: bool) -> Self {
self.italic = Some(italic);
self
}
pub fn with_underline(mut self, underline: bool) -> Self {
self.underline = Some(underline);
self
}
pub fn with_strikethrough(mut self, strikethrough: bool) -> Self {
self.strikethrough = Some(strikethrough);
self
}
}
#[derive(Deserialize, Debug)]
pub struct ConditionFormatInfo {
pub cf_id: String,
#[serde(flatten)]
pub condition_format: ConditionFormatRule,
}
#[derive(Deserialize, Debug)]
pub struct CreateConditionFormatsResponseData {
pub items: Vec<ConditionFormatInfo>,
#[serde(default)]
pub created_count: u32,
}
impl ApiResponseTrait for CreateConditionFormatsResponseData {
fn data_format() -> ResponseFormat {
ResponseFormat::Data
}
}
impl_executable_builder_owned!(
CreateConditionFormatsRequestBuilder,
SpreadsheetSheetService,
CreateConditionFormatsRequest,
BaseResponse<CreateConditionFormatsResponseData>,
create_condition_formats
);
#[cfg(test)]
#[allow(unused_variables, unused_unsafe)]
mod test {
use super::*;
use serde_json::json;
#[test]
fn test_condition_format_rule_creation() {
let format = FormatStyle::background_color("#FF0000").with_text_color("#FFFFFF");
let rule = ConditionFormatRule::greater_than("A1:A10", 100.0, format);
assert_eq!(rule.range, "A1:A10");
assert_eq!(rule.condition_type, "NUMBER_GREATER");
assert_eq!(rule.condition_values.as_ref().unwrap()[0], "100");
assert_eq!(rule.format.background_color.as_ref().unwrap(), "#FF0000");
assert_eq!(rule.format.text_color.as_ref().unwrap(), "#FFFFFF");
}
#[test]
fn test_create_condition_formats_response() {
let json = json!({
"items": [
{
"cf_id": "cf_001",
"range": "A1:A10",
"condition_type": "NUMBER_GREATER",
"condition_values": ["100"],
"format": {
"background_color": "#FF0000",
"text_color": "#FFFFFF",
"bold": true
}
}
],
"created_count": 1
});
let response: CreateConditionFormatsResponseData = serde_json::from_value(json).unwrap();
assert_eq!(response.items.len(), 1);
assert_eq!(response.items[0].cf_id, "cf_001");
assert_eq!(response.created_count, 1);
}
}