1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use serde::{Deserialize, Serialize};

use crate::{
    core::{
        api_req::ApiRequest,
        api_resp::{ApiResponseTrait, BaseResponse, ResponseFormat},
        constants::AccessTokenType,
        req_option, SDKResult,
    },
    service::sheets::v2::{
        data_operation::{CellStyle, SheetDataUpdates, StyleFont},
        SpreadsheetService,
    },
};

#[derive(Debug, Serialize, Default)]
pub struct SetCellStyleRequest {
    #[serde(skip)]
    api_request: ApiRequest,
    #[serde(skip)]
    spreadsheet_token: String,
    /// 设置单元格样式
    #[serde(rename = "appendStyle")]
    append_style: AppendStyle,
}

#[derive(Debug, Serialize, Default)]
struct AppendStyle {
    range: String,
    style: CellStyle,
}

impl SetCellStyleRequest {
    pub fn builder() -> SetCellStyleRequestBuilder {
        SetCellStyleRequestBuilder::default()
    }
}

#[derive(Default)]
pub struct SetCellStyleRequestBuilder {
    request: SetCellStyleRequest,
}

impl SetCellStyleRequestBuilder {
    pub fn spreadsheet_token(mut self, spreadsheet_token: impl ToString) -> Self {
        self.request.spreadsheet_token = spreadsheet_token.to_string();
        self
    }

    pub fn range(mut self, range: impl ToString) -> Self {
        self.request.append_style.range = range.to_string();
        self
    }

    pub fn font(mut self, font: StyleFont) -> Self {
        self.request.append_style.style.font = font;
        self
    }

    pub fn text_decoration(mut self, text_decoration: i32) -> Self {
        self.request.append_style.style.text_decoration = Some(text_decoration);
        self
    }

    pub fn formatter(mut self, formatter: impl ToString) -> Self {
        self.request.append_style.style.formatter = Some(formatter.to_string());
        self
    }

    pub fn h_align(mut self, h_align: i32) -> Self {
        self.request.append_style.style.h_align = Some(h_align);
        self
    }

    pub fn v_align(mut self, v_align: i32) -> Self {
        self.request.append_style.style.v_align = Some(v_align);
        self
    }

    pub fn fore_color(mut self, fore_color: impl ToString) -> Self {
        self.request.append_style.style.fore_color = Some(fore_color.to_string());
        self
    }

    pub fn back_color(mut self, back_color: impl ToString) -> Self {
        self.request.append_style.style.back_color = Some(back_color.to_string());
        self
    }

    pub fn border_type(mut self, border_type: impl ToString) -> Self {
        self.request.append_style.style.border_type = Some(border_type.to_string());
        self
    }

    pub fn border_color(mut self, border_color: impl ToString) -> Self {
        self.request.append_style.style.border_color = Some(border_color.to_string());
        self
    }

    pub fn clean(mut self, clean: bool) -> Self {
        self.request.append_style.style.clean = clean;
        self
    }

    pub fn build(mut self) -> SetCellStyleRequest {
        self.request.api_request.body = serde_json::to_vec(&self.request).unwrap();
        self.request
    }
}

#[derive(Deserialize, Debug)]
pub struct SetCellStyleResponse {
    pub updates: SheetDataUpdates,
}

impl ApiResponseTrait for SetCellStyleResponse {
    fn data_format() -> ResponseFormat {
        ResponseFormat::Data
    }
}

impl SpreadsheetService {
    /// 该接口用于根据 spreadsheetToken 、range
    /// 和样式信息更新单元格样式;单次写入不超过5000行,100列。建议在设置边框样式时,
    /// 每次更新的单元格数量不要超过30000个。
    pub async fn set_cell_style(
        &self,
        request: SetCellStyleRequest,
        option: Option<req_option::RequestOption>,
    ) -> SDKResult<BaseResponse<SetCellStyleResponse>> {
        let mut api_req = request.api_request;
        api_req.api_path = format!(
            "/open-apis/sheets/v2/spreadsheets/{spreadsheet_token}/style",
            spreadsheet_token = request.spreadsheet_token
        );
        api_req.http_method = reqwest::Method::PUT;
        api_req.supported_access_token_types = vec![AccessTokenType::Tenant, AccessTokenType::App];

        let api_resp = crate::core::http::Transport::request(api_req, &self.config, option).await?;

        Ok(api_resp)
    }
}