excelize-rs 0.1.7

Rust language library for reading and writing Microsoft Excel (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
Documentation
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Error types and messages for the `excelize` crate.
//!
//! These errors are ported from the Go `excelize` package's `errors.go`.

use std::fmt;

use thiserror::Error;

use crate::constants::{
    MAX_CELL_STYLES, MAX_COLUMN_WIDTH, MAX_COLUMNS, MAX_FIELD_LENGTH, MAX_FILE_PATH_LENGTH,
    MAX_FONT_SIZE, MAX_FORM_CONTROL_VALUE, MAX_GRAPHIC_ALT_TEXT_LENGTH, MAX_GRAPHIC_NAME_LENGTH,
    MAX_ROW_HEIGHT, MAX_SHEET_NAME_LENGTH, MIN_COLUMNS, MIN_FONT_SIZE, TOTAL_CELL_CHARS,
};

// ------------------------------------------------------------------
// Static errors
// ------------------------------------------------------------------

#[derive(Debug, Error)]
#[error("unsupported VBA project")]
pub struct ErrAddVBAProject;

#[derive(Debug, Error)]
#[error("unexpected child of attrValBool")]
pub struct ErrAttrValBool;

#[derive(Debug, Error)]
#[error("cell value must be 0-{0} characters", TOTAL_CELL_CHARS)]
pub struct ErrCellCharsLength;

#[derive(Debug, Error)]
#[error("the cell styles exceeds the {MAX_CELL_STYLES} limit")]
pub struct ErrCellStyles;

#[derive(Debug, Error)]
#[error("cannot set both 'Formula' and 'Paragraph' for chart title")]
pub struct ErrChartTitle;

#[derive(Debug, Error)]
#[error(
    "the column number must be greater than or equal to {MIN_COLUMNS} and less than or equal to {MAX_COLUMNS}"
)]
pub struct ErrColumnNumber;

#[derive(Debug, Error)]
#[error("the width of the column must be less than or equal to {MAX_COLUMN_WIDTH} characters")]
pub struct ErrColumnWidth;

#[derive(Debug, Error)]
#[error("coordinates length must be 4")]
pub struct ErrCoordinates;

#[derive(Debug, Error)]
#[error("custom number format can not be empty")]
pub struct ErrCustomNumFmt;

#[derive(Debug, Error)]
#[error("data validation must be 0-{MAX_FIELD_LENGTH} characters")]
pub struct ErrDataValidationFormulaLength;

#[derive(Debug, Error)]
#[error("data validation range exceeds limit")]
pub struct ErrDataValidationRange;

#[derive(Debug, Error)]
#[error("the same name already exists on the scope")]
pub struct ErrDefinedNameDuplicate;

#[derive(Debug, Error)]
#[error("no defined name on the scope")]
pub struct ErrDefinedNameScope;

#[derive(Debug, Error)]
#[error("the same name sheet already exists")]
pub struct ErrExistsSheet;

#[derive(Debug, Error)]
#[error("the same name table already exists")]
pub struct ErrExistsTableName;

#[derive(Debug, Error)]
#[error("fill type value must be one of 'gradient' or 'pattern'")]
pub struct ErrFillType;

#[derive(Debug, Error)]
#[error("fill color value must be an array of two colors for 'gradient' type")]
pub struct ErrFillGradientColor;

#[derive(Debug, Error)]
#[error("fill shading value must be between 0 and 16 for 'gradient' type")]
pub struct ErrFillGradientShading;

#[derive(Debug, Error)]
#[error("fill color value must be empty or an array of one color for 'pattern' type")]
pub struct ErrFillPatternColor;

#[derive(Debug, Error)]
#[error("fill pattern value must be between 0 and 18")]
pub struct ErrFillPattern;

#[derive(Debug, Error)]
#[error("the length of the font family name must be less than or equal to 31")]
pub struct ErrFontLength;

#[derive(Debug, Error)]
#[error("font size must be an integer from {MIN_FONT_SIZE} to {MAX_FONT_SIZE} points")]
pub struct ErrFontSize;

#[derive(Debug, Error)]
#[error("scroll value must be an integer from 0 to {MAX_FORM_CONTROL_VALUE}")]
pub struct ErrFormControlValue;

#[derive(Debug, Error)]
#[error("group worksheet must contain an active worksheet")]
pub struct ErrGroupSheets;

#[derive(Debug, Error)]
#[error("unsupported image extension")]
pub struct ErrImgExt;

#[derive(Debug, Error)]
#[error("image decode failed")]
pub struct ErrImgLoad;

#[derive(Debug, Error)]
#[error("formula not valid")]
pub struct ErrInvalidFormula;

#[derive(Debug, Error)]
#[error("invalid date value")]
pub struct ErrInvalidDate;

#[derive(Debug, Error)]
#[error("file path length exceeds maximum limit {MAX_FILE_PATH_LENGTH} characters")]
pub struct ErrMaxFilePathLength;

#[derive(Debug, Error)]
#[error("the height of the row must be less than or equal to {MAX_ROW_HEIGHT} points")]
pub struct ErrMaxRowHeight;

#[derive(Debug, Error)]
#[error("row number exceeds maximum limit")]
pub struct ErrMaxRows;

#[derive(Debug, Error)]
#[error("the name length exceeds the {MAX_FIELD_LENGTH} characters limit")]
pub struct ErrNameLength;

#[derive(Debug, Error)]
#[error("the alt text length exceeds the {MAX_GRAPHIC_ALT_TEXT_LENGTH} characters limit")]
pub struct ErrMaxGraphicAltTextLength;

#[derive(Debug, Error)]
#[error("the name length exceeds the {MAX_GRAPHIC_NAME_LENGTH} characters limit")]
pub struct ErrMaxGraphicNameLength;

#[derive(Debug, Error)]
#[error("the value of UnzipSizeLimit should be greater than or equal to UnzipXMLSizeLimit")]
pub struct ErrOptionsUnzipSizeLimit;

#[derive(Debug, Error)]
#[error("invalid outline level")]
pub struct ErrOutlineLevel;

#[derive(Debug, Error)]
#[error("adjust to value must be an integer from 0 to 400")]
pub struct ErrPageSetupAdjustTo;

#[derive(Debug, Error)]
#[error("parameter is invalid")]
pub struct ErrParameterInvalid;

#[derive(Debug, Error)]
#[error("parameter is required")]
pub struct ErrParameterRequired;

#[derive(Debug, Error)]
#[error("password length invalid")]
pub struct ErrPasswordLengthInvalid;

#[derive(Debug, Error)]
#[error("this kind of show value as type requires a base field")]
pub struct ErrPivotTableShowValuesAsBaseField;

#[derive(Debug, Error)]
#[error("this kind of show value as type and base field requires a base item")]
pub struct ErrPivotTableShowValuesAsBaseItem;

#[derive(Debug, Error)]
#[error("cannot enable ClassicLayout and CompactData in the same time")]
pub struct ErrPivotTableClassicLayout;

#[derive(Debug, Error)]
#[error("no path defined for file, consider File.write_to or File.write")]
pub struct ErrSave;

#[derive(Debug, Error)]
#[error("invalid worksheet index")]
pub struct ErrSheetIdx;

#[derive(Debug, Error)]
#[error("the sheet name can not be blank")]
pub struct ErrSheetNameBlank;

#[derive(Debug, Error)]
#[error("the sheet can not contain any of the characters :\\/?*[or]")]
pub struct ErrSheetNameInvalid;

#[derive(Debug, Error)]
#[error("the sheet name length exceeds the {MAX_SHEET_NAME_LENGTH} characters limit")]
pub struct ErrSheetNameLength;

#[derive(Debug, Error)]
#[error("the first or last character of the sheet name can not be a single quote")]
pub struct ErrSheetNameSingleQuote;

#[derive(Debug, Error)]
#[error("must have the same number of 'Location' and 'Range' parameters")]
pub struct ErrSparkline;

#[derive(Debug, Error)]
#[error("parameter 'Location' is required")]
pub struct ErrSparklineLocation;

#[derive(Debug, Error)]
#[error("parameter 'Range' is required")]
pub struct ErrSparklineRange;

#[derive(Debug, Error)]
#[error("parameter 'Style' value must be an integer from 0 to 35")]
pub struct ErrSparklineStyle;

#[derive(Debug, Error)]
#[error("parameter 'Type' value must be one of 'line', 'column' or 'win_loss'")]
pub struct ErrSparklineType;

#[derive(Debug, Error)]
#[error("over maximum limit hyperlinks in a worksheet")]
pub struct ErrTotalSheetHyperlinks;

#[derive(Debug, Error)]
#[error("transparency value must be an integer from 0 to 100")]
pub struct ErrTransparency;

#[derive(Debug, Error)]
#[error("unknown encryption mechanism")]
pub struct ErrUnknownEncryptMechanism;

#[derive(Debug, Error)]
#[error("worksheet has set no protect")]
pub struct ErrUnprotectSheet;

#[derive(Debug, Error)]
#[error("worksheet protect password not match")]
pub struct ErrUnprotectSheetPassword;

#[derive(Debug, Error)]
#[error("workbook has set no protect")]
pub struct ErrUnprotectWorkbook;

#[derive(Debug, Error)]
#[error("workbook protect password not match")]
pub struct ErrUnprotectWorkbookPassword;

#[derive(Debug, Error)]
#[error("unsupported encryption mechanism")]
pub struct ErrUnsupportedEncryptMechanism;

#[derive(Debug, Error)]
#[error("unsupported hash algorithm")]
pub struct ErrUnsupportedHashAlgorithm;

#[derive(Debug, Error)]
#[error("unsupported number format token")]
pub struct ErrUnsupportedNumberFormat;

#[derive(Debug, Error)]
#[error("unsupported pivot table show value as type")]
pub struct ErrUnsupportedPivotTableShowValuesAsType;

#[derive(Debug, Error)]
#[error("unsupported workbook file format")]
pub struct ErrWorkbookFileFormat;

#[derive(Debug, Error)]
#[error("the supplied open workbook password is not correct")]
pub struct ErrWorkbookPassword;

#[derive(Debug, Error)]
#[error("workbook must contain at least one worksheet")]
pub struct ErrWorkbook;

// ------------------------------------------------------------------
// Parameterized errors
// ------------------------------------------------------------------

/// Error returned when a worksheet does not exist.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrSheetNotExist {
    pub sheet_name: String,
}

impl fmt::Display for ErrSheetNotExist {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "sheet {} does not exist", self.sheet_name)
    }
}

impl std::error::Error for ErrSheetNotExist {}

/// A convenience alias for a `Result` using the dynamic `Error` type.
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

// ------------------------------------------------------------------
// Helper constructors (ported from `errors.go`)
// ------------------------------------------------------------------

pub fn new_add_comment_error(cell: &str) -> String {
    format!("comment already exist on cell {cell}")
}

pub fn new_cell_name_to_coordinates_error(cell: &str, err: impl std::error::Error) -> String {
    format!("cannot convert cell {cell:?} to coordinates: {err}")
}

pub fn new_chart_title_error(name: &str) -> String {
    format!("chart title field {name} value must be an integer from 0 to 100")
}

pub fn new_coordinates_to_cell_name_error(col: i32, row: i32) -> String {
    format!("invalid cell reference [{col}, {row}]")
}

pub fn new_field_length_error(name: &str) -> String {
    format!("field {name} must be less than or equal to 255 characters")
}

pub fn new_invalid_auto_filter_column_error(col: &str) -> String {
    format!("incorrect index of column {col:?}")
}

pub fn new_invalid_auto_filter_exp_error(exp: &str) -> String {
    format!("incorrect number of tokens in criteria {exp:?}")
}

pub fn new_invalid_auto_filter_operator_error(op: &str, exp: &str) -> String {
    format!(
        "the operator {op:?} in expression {exp:?} is not valid in relation to Blanks/NonBlanks"
    )
}

pub fn new_invalid_cell_name_error(cell: &str) -> String {
    format!("invalid cell name {cell:?}")
}

pub fn new_invalid_column_name_error(col: &str) -> String {
    format!("invalid column name {col:?}")
}

pub fn new_invalid_excel_date_error(date_value: f64) -> String {
    format!("invalid date value {date_value}, negative values are not supported")
}

pub fn new_invalid_link_type_error(link_type: &str) -> String {
    format!("invalid link type {link_type:?}")
}

pub fn new_invalid_name_error(name: &str) -> String {
    format!(
        "invalid name {name:?}, the name should be starts with a letter or underscore, can not include a space or character, and can not conflict with an existing name in the workbook"
    )
}

pub fn new_invalid_optional_value(name: &str, value: &str, values: &[&str]) -> String {
    format!(
        "invalid {name} value {value:?}, acceptable value should be one of {}",
        values.join(", ")
    )
}

pub fn new_invalid_row_number_error(row: i32) -> String {
    format!("invalid row number {row}")
}

pub fn new_invalid_shared_string_index_error(idx: i32) -> String {
    format!("invalid shared string index {idx}")
}

pub fn new_invalid_slicer_name_error(name: &str) -> String {
    format!("invalid slicer name {name:?}")
}

pub fn new_invalid_style_id_error(style_id: i32) -> String {
    format!("invalid style ID {style_id}")
}

pub fn new_no_exist_slicer_error(name: &str) -> String {
    format!("slicer {name} does not exist")
}

pub fn new_no_exist_table_error(name: &str) -> String {
    format!("table {name} does not exist")
}

pub fn new_not_worksheet_error(name: &str) -> String {
    format!("sheet {name} is not a worksheet")
}

pub fn new_pivot_table_col_fields_error(data: &[String]) -> String {
    format!(
        "data fields {} appear both in the pivot table column fields and filter fields",
        data.join(", ")
    )
}

pub fn new_pivot_table_row_fields_error(data: &[String]) -> String {
    format!(
        "data fields {} appear both in the pivot table row fields and filter fields",
        data.join(", ")
    )
}

pub fn new_pivot_table_data_range_error(msg: &str) -> String {
    format!("parameter 'DataRange' parsing error: {msg}")
}

pub fn new_pivot_table_selected_item_error(item: &str, field: &str) -> String {
    format!("selected item {item} does not exist in pivot table field {field}")
}

pub fn new_pivot_table_range_error(msg: &str) -> String {
    format!("parameter 'PivotTableRange' parsing error: {msg}")
}

pub fn new_pivot_table_show_values_as_base_field_error(field: &str) -> String {
    format!("base field {field} does not exist in shared items")
}

pub fn new_stream_set_row_error(row: i32) -> String {
    format!("row {row} has already been written")
}

pub fn new_stream_set_row_order_error(name: &str) -> String {
    format!("must call the {name} function before the SetRow function")
}

pub fn new_unknown_filter_token_error(token: &str) -> String {
    format!("unknown operator: {token}")
}

pub fn new_unsupported_chart_type_error(chart_type: i32) -> String {
    format!("unsupported chart type {chart_type}")
}

pub fn new_unsupported_pivot_cache_source_type_error(source_type: &str) -> String {
    format!("unsupported pivot table cache source type: {source_type}")
}

pub fn new_unzip_size_limit_error(unzip_size_limit: i64) -> String {
    format!("unzip size exceeds the {unzip_size_limit} bytes limit")
}

pub fn new_view_idx_error(view_index: i32) -> String {
    format!("view index {view_index} out of range")
}