open_lark/service/cloud_docs/sheets/v2/data_operation/
share.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::core::api_resp::{ApiResponseTrait, ResponseFormat};
5#[derive(Serialize, Debug, Default)]
6pub(crate) struct ValueRangeRequest {
7    /// 插入范围,包含 sheetId 与单元格范围两部分,目前支持四种索引方式,详见
8    /// 在线表格开发指南,range所表示的范围需要大于等于values占用的范围。
9    pub(crate) range: String,
10    /// 需要写入的值,如要写入公式、超链接、email、@人等,可详看附录sheet 支持写入数据类型
11    pub(crate) values: Value,
12}
13
14#[derive(Deserialize, Debug, Default)]
15#[allow(dead_code)]
16pub struct ValueRangeResponse {
17    /// 插入维度
18    #[serde(rename = "majorDimension")]
19    pub major_dimension: String,
20    /// 插入范围,包含 sheetId 与单元格范围两部分,目前支持四种索引方式,详见
21    /// 在线表格开发指南,range所表示的范围需要大于等于values占用的范围。
22    pub range: String,
23    /// 需要写入的值,如要写入公式、超链接、email、@人等,可详看附录sheet 支持写入数据类型
24    pub values: Value,
25    /// sheet 的版本号
26    pub revision: i32,
27}
28
29/// 更新数据响应体
30#[derive(Deserialize, Debug)]
31#[allow(dead_code)]
32pub struct UpdateSheetDataResponse {
33    /// spreadsheet 的 token
34    #[serde(rename = "spreadsheetToken")]
35    pub spreed_sheet_token: String,
36    /// 写入的范围
37    #[serde(rename = "tableRange")]
38    pub table_range: String,
39    /// sheet 的版本号
40    pub revision: i32,
41    /// 追加数据的范围、行列数等
42    pub updates: SheetDataUpdates,
43}
44
45impl ApiResponseTrait for UpdateSheetDataResponse {
46    fn data_format() -> ResponseFormat {
47        ResponseFormat::Data
48    }
49}
50
51/// 追加数据的范围、行列数等
52#[derive(Deserialize, Debug)]
53#[allow(dead_code)]
54pub struct SheetDataUpdates {
55    /// spreadsheet 的 token
56    #[serde(rename = "spreadsheetToken")]
57    pub spreed_sheet_token: String,
58    /// 写入的范围
59    #[serde(rename = "updatedRange")]
60    pub updated_range: String,
61    /// 写入的行数
62    #[serde(rename = "updatedRows")]
63    pub updated_rows: i32,
64    /// 写入的列数
65    #[serde(rename = "updatedColumns")]
66    pub updated_columns: i32,
67    /// 写入的单元格总数
68    #[serde(rename = "updatedCells")]
69    pub updated_cells: i32,
70    /// sheet 的版本号
71    pub revision: Option<i32>,
72}
73
74impl ApiResponseTrait for SheetDataUpdates {
75    fn data_format() -> ResponseFormat {
76        ResponseFormat::Data
77    }
78}
79
80/// 值与范围
81#[derive(Deserialize, Debug)]
82#[allow(dead_code)]
83pub struct ReadRangeValueRange {
84    /// 插入维度
85    #[serde(rename = "majorDimension")]
86    pub major_dimension: String,
87    /// 返回数据的范围,为空时表示查询范围没有数据
88    pub range: String,
89    /// sheet 的版本号
90    pub revision: i32,
91    /// 查询得到的值
92    pub values: Value,
93}
94
95#[derive(Debug, Serialize, Default)]
96pub struct CellStyle {
97    /// 字体相关样式
98    pub(crate) font: StyleFont,
99    /// 文本装饰 ,0 默认,1 下划线,2 删除线 ,3 下划线和删除线
100    #[serde(rename = "textDecoration")]
101    pub(crate) text_decoration: Option<i32>,
102    /// 数字格式,详见附录 sheet支持数字格式类型
103    pub(crate) formatter: Option<String>,
104    /// 水平对齐,0 左对齐,1 中对齐,2 右对齐
105    #[serde(rename = "hAlign")]
106    pub(crate) h_align: Option<i32>,
107    #[serde(rename = "vAlign")]
108    /// 垂直对齐, 0 上对齐,1 中对齐, 2 下对齐
109    pub(crate) v_align: Option<i32>,
110    /// 字体颜色
111    #[serde(rename = "foreColor")]
112    pub(crate) fore_color: Option<String>,
113    /// 背景颜色
114    #[serde(rename = "backColor")]
115    pub(crate) back_color: Option<String>,
116    /// 边框类型,可选 "FULL_BORDER","OUTER_BORDER","INNER_BORDER","NO_BORDER","LEFT_BORDER","
117    /// RIGHT_BORDER","TOP_BORDER","BOTTOM_BORDER"
118    #[serde(rename = "borderType")]
119    pub(crate) border_type: Option<String>,
120    /// 边框颜色
121    #[serde(rename = "borderColor")]
122    pub(crate) border_color: Option<String>,
123    /// 是否清除所有格式,默认 false
124    pub(crate) clean: bool,
125}
126
127impl CellStyle {
128    pub fn builder() -> CellStyleBuilder {
129        CellStyleBuilder::default()
130    }
131}
132
133#[derive(Default)]
134pub struct CellStyleBuilder {
135    request: CellStyle,
136}
137
138impl CellStyleBuilder {
139    pub fn font(mut self, font: StyleFont) -> Self {
140        self.request.font = font;
141        self
142    }
143
144    pub fn text_decoration(mut self, text_decoration: i32) -> Self {
145        self.request.text_decoration = Some(text_decoration);
146        self
147    }
148
149    pub fn formatter(mut self, formatter: impl ToString) -> Self {
150        self.request.formatter = Some(formatter.to_string());
151        self
152    }
153
154    pub fn h_align(mut self, h_align: i32) -> Self {
155        self.request.h_align = Some(h_align);
156        self
157    }
158
159    pub fn v_align(mut self, v_align: i32) -> Self {
160        self.request.v_align = Some(v_align);
161        self
162    }
163
164    pub fn fore_color(mut self, fore_color: impl ToString) -> Self {
165        self.request.fore_color = Some(fore_color.to_string());
166        self
167    }
168
169    pub fn back_color(mut self, back_color: impl ToString) -> Self {
170        self.request.back_color = Some(back_color.to_string());
171        self
172    }
173
174    pub fn border_type(mut self, border_type: impl ToString) -> Self {
175        self.request.border_type = Some(border_type.to_string());
176        self
177    }
178
179    pub fn border_color(mut self, border_color: impl ToString) -> Self {
180        self.request.border_color = Some(border_color.to_string());
181        self
182    }
183
184    pub fn clean(mut self, clean: bool) -> Self {
185        self.request.clean = clean;
186        self
187    }
188
189    pub fn build(self) -> CellStyle {
190        self.request
191    }
192}
193
194/// 字体相关样式
195#[derive(Debug, Serialize, Default)]
196pub struct StyleFont {
197    /// 是否加粗
198    bold: Option<bool>,
199    /// 是否斜体
200    italic: Option<bool>,
201    /// 字体大小 字号大小为9~36 行距固定为1.5,如:10pt/1.5
202    #[serde(rename = "fontSize")]
203    font_size: Option<String>,
204    /// 清除 font 格式,默认 false
205    clean: Option<bool>,
206}
207
208impl StyleFont {
209    pub fn builder() -> StyleFontBuilder {
210        StyleFontBuilder::default()
211    }
212}
213
214#[derive(Default)]
215pub struct StyleFontBuilder {
216    font: StyleFont,
217}
218
219impl StyleFontBuilder {
220    pub fn bold(mut self, bold: bool) -> Self {
221        self.font.bold = Some(bold);
222        self
223    }
224
225    pub fn italic(mut self, italic: bool) -> Self {
226        self.font.italic = Some(italic);
227        self
228    }
229
230    pub fn font_size(mut self, font_size: impl ToString) -> Self {
231        self.font.font_size = Some(font_size.to_string());
232        self
233    }
234
235    pub fn clean(mut self, clean: bool) -> Self {
236        self.font.clean = Some(clean);
237        self
238    }
239
240    pub fn build(self) -> StyleFont {
241        self.font
242    }
243}