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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use super::Worksheet;
use super::Properties;
use super::Security;
use super::Calculation;
use super::Style;
use super::NumberFormat;
use super::Font;
use super::Fill;
use super::DefinedName;
use super::Borders;
use super::Theme;
use helper::coordinate::*;

#[derive(Default, Debug)]
pub struct Spreadsheet {
    properties: Properties,
    security: Security,
    work_sheet_collection: Vec<Worksheet>,
    calculation_engine: Calculation,
    active_sheet_index: usize,
    named_ranges: Vec<String>,
    has_macros: bool,
    macros_code: Option<Vec<u8>>,
    macros_certificate: String,
    ribbon_xml_data: Option<String>,
    ribbon_bin_objects: Option<Vec<String>>,
    unparsed_loaded_data: Vec<String>,
    show_horizontal_scroll: bool,
    show_vertical_scroll: bool,
    show_sheet_tabs: bool,
    minimized: bool,
    auto_filter_date_grouping: bool,
    first_sheet_index: i32,
    visibility: String,
    tab_ratio: i32,
    theme: Theme,
    defined_names: Vec<DefinedName>,
}
impl Spreadsheet {
    // ************************
    // update Coordinate
    // ************************

    /// Insert new rows.
    /// # Arguments
    /// * `sheet_name` - Specify the sheet name. ex) "Sheet1"
    /// * `row_index` - Specify point of insert. ex) 1
    /// * `num_rows` - Specify number to insert. ex) 2
    /// # Examples
    /// ```
    /// let mut book = umya_spreadsheet::new_file();
    /// book.insert_new_row("Sheet1", 2, 3);
    /// ```
    pub fn insert_new_row<S: Into<String>>(&mut self, sheet_name:S, row_index:usize, num_rows:usize) {
        self.adjustment_insert_coordinate(&sheet_name.into(), &0, &0, &row_index, &num_rows);
    }

    /// Insert new colmuns.
    /// # Arguments
    /// * `sheet_name` - Specify the sheet name. ex) "Sheet1"
    /// * `column` - Specify point of insert. ex) "B"
    /// * `num_columns` - Specify number to insert. ex) 3
    /// # Examples
    /// ```
    /// let mut book = umya_spreadsheet::new_file();
    /// book.insert_new_colmun("Sheet1", "B", 3);
    /// ```
    pub fn insert_new_colmun<S: Into<String>>(&mut self, sheet_name:S, column:S, num_columns:usize) {
        let column_upper = column.into().to_uppercase();
        let column_index = column_index_from_string(column_upper);
        self.insert_new_colmun_by_index(&sheet_name.into(), column_index, num_columns);
    }

    /// Insert new colmuns.
    /// # Arguments
    /// * `sheet_name` - Specify the sheet name. ex) "Sheet1"
    /// * `column_index` - Specify point of insert. ex) 2
    /// * `num_columns` - Specify number to insert. ex) 3
    /// # Examples
    /// ```
    /// let mut book = umya_spreadsheet::new_file();
    /// book.insert_new_colmun_by_index("Sheet1", 2, 3);
    /// ```
    pub fn insert_new_colmun_by_index<S: Into<String>>(&mut self, sheet_name:S, column_index:usize, num_columns:usize) {
        self.adjustment_insert_coordinate(&sheet_name.into(), &column_index, &num_columns, &0, &0);
    }

    /// Remove rows.
    /// # Arguments
    /// * `sheet_name` - Specify the sheet name. ex) "Sheet1"
    /// * `row_index` - Specify point of remove. ex) 1
    /// * `num_rows` - Specify number to remove. ex) 2
    /// # Examples
    /// ```
    /// let mut book = umya_spreadsheet::new_file();
    /// book.remove_row("Sheet1", 2, 3);
    /// ```
    pub fn remove_row<S: Into<String>>(&mut self, sheet_name:S, row_index:usize, num_rows:usize) {
        self.adjustment_remove_coordinate(&sheet_name.into(), &0, &0, &row_index, &num_rows);
    }

    /// Remove colmuns.
    /// # Arguments
    /// * `sheet_name` - Specify the sheet name. ex) "Sheet1"
    /// * `column` - Specify point of remove. ex) "B"
    /// * `num_columns` - Specify number to remove. ex) 3
    /// # Examples
    /// ```
    /// let mut book = umya_spreadsheet::new_file();
    /// book.remove_colmun("Sheet1", "B", 3);
    /// ```
    pub fn remove_colmun<S: Into<String>>(&mut self, sheet_name:S, column:S, num_columns:usize) {
        let column_upper = column.into().to_uppercase();
        let column_index = column_index_from_string(column_upper);
        self.remove_colmun_by_index(&sheet_name.into(), column_index, num_columns);
    }

    /// Remove colmuns.
    /// # Arguments
    /// * `sheet_name` - Specify the sheet name. ex) "Sheet1"
    /// * `column_index` - Specify point of remove. ex) 2
    /// * `num_columns` - Specify number to remove. ex) 3
    /// # Examples
    /// ```
    /// let mut book = umya_spreadsheet::new_file();
    /// book.remove_colmun_by_index("Sheet1", 2, 3);
    /// ```
    pub fn remove_colmun_by_index<S: Into<String>>(&mut self, sheet_name:S, column_index:usize, num_columns:usize) {
        self.adjustment_remove_coordinate(&sheet_name.into(), &column_index, &num_columns, &0, &0);
    }

    pub(crate) fn adjustment_insert_coordinate(&mut self, sheet_name:&str, column_index:&usize, num_columns:&usize, row_index:&usize, num_rows:&usize) {
        for defined_name in &mut self.defined_names {
            defined_name.get_address_obj_mut().adjustment_insert_coordinate(sheet_name, column_index, num_columns, row_index, num_rows);
        }
        for worksheet in &mut self.work_sheet_collection {
            worksheet.adjustment_insert_coordinate(sheet_name, column_index, num_columns, row_index, num_rows);
        }
    }

    pub(crate) fn adjustment_remove_coordinate(&mut self, sheet_name:&str, column_index:&usize, num_columns:&usize, row_index:&usize, num_rows:&usize) {
        self.defined_names.retain(|x| {
            !(x.get_address_obj().is_remove(sheet_name, column_index, num_columns, row_index, num_rows))
        });
        for defined_name in &mut self.defined_names {
            defined_name.get_address_obj_mut().adjustment_remove_coordinate(sheet_name, column_index, num_columns, row_index, num_rows);
        }
        for worksheet in &mut self.work_sheet_collection {
            worksheet.adjustment_remove_coordinate(sheet_name, column_index, num_columns, row_index, num_rows);
        }
    }

    pub fn get_defined_names(&self) -> &Vec<DefinedName> {
        &self.defined_names
    }

    pub fn get_defined_names_mut(&mut self) -> &mut Vec<DefinedName> {
        &mut self.defined_names
    }

    pub fn set_defined_names(&mut self, value:Vec<DefinedName>) {
        self.defined_names = value;
    }

    pub fn add_defined_names(&mut self, value:DefinedName) {
        self.defined_names.push(value);
    }

    pub fn add_defined_name<S: Into<String>>(&mut self, name:S, address:S)->Result<(), &str> {
        let mut defined_name = DefinedName::default();
        defined_name.set_name(name.into());
        defined_name.set_address(address.into());
        self.defined_names.push(defined_name);
        Ok(())
    }

    pub(crate) fn get_all_conditional_style_list(&self) -> Vec<(String, Style)> {
        let mut result:Vec<(String, Style)> = Vec::new();
        for work_sheet in &self.work_sheet_collection {
            for conditional_formatting in work_sheet.get_conditional_styles_collection() {
                for conditional in conditional_formatting.get_conditional_collection() {
                    match conditional.get_style() {
                        Some(v) => {
                            let mut is_match = false;
                            for (hash, _) in &result {
                                if hash == &v.get_hash_code() {
                                    is_match = true;
                                    break;
                                }
                            }
                            if is_match == false {
                                result.push((v.get_hash_code(), v.clone()));
                            }
                        },
                        None => {}
                    }
                }
            }
        }
        result
    }

    pub fn get_theme(&self) -> &Theme {
        &self.theme
    }

    pub(crate) fn set_theme(&mut self, value:Theme) {
        self.theme = value;
    }

    pub(crate) fn has_comment(&self) -> bool {
        for worksheet in &self.work_sheet_collection {
            if worksheet.get_comments().len() > 0 {
                return true;
            }
        }
        false
    }

    pub fn get_properties(&self) -> &Properties {
        &self.properties
    }

    pub fn get_properties_mut(&mut self) -> &mut Properties {
        &mut self.properties
    }
    
    pub fn set_properties(&mut self, value:Properties) {
        self.properties = value;
    }

    pub fn get_security(&self) -> &Security {
        &self.security
    }

    pub fn get_security_mut(&mut self) -> &mut Security {
        &mut self.security
    }

    pub fn set_security(&mut self, value:Security) {
        self.security = value;
    }

    pub fn get_macros_code(&self) -> &Option<Vec<u8>> {
        &self.macros_code
    }

    pub(crate) fn set_macros_code(&mut self, value:Vec<u8>) {
        self.macros_code = Some(value);
    }
    
    pub fn get_has_macros(&self) -> &bool {
        &self.has_macros
    }

    pub(crate) fn set_has_macros(&mut self, value:bool) {
        self.has_macros = value;
    }

    pub fn get_unparsed_loaded_data(&self) -> &Vec<String> {
        &self.unparsed_loaded_data
    }

    pub fn set_unparsed_loaded_data(&mut self, value:Vec<String>) {
        self.unparsed_loaded_data = value;
    }

    pub fn get_active_sheet_index(&self) -> &usize {
        &self.active_sheet_index
    }

    pub(crate) fn set_active_sheet_index(&mut self, value:usize) {
        self.active_sheet_index = value;
    }

    pub(crate) fn get_all_number_format(&self) -> Vec<(String, NumberFormat)> {
        let mut result:Vec<(String, NumberFormat)> = Vec::new();
        for (_, style) in &self.get_all_cell_style() {
            match style.get_number_format() {
                Some(v) => {
                    if v.get_built_in_format_code() != &None {
                        continue;
                    }
                    let mut is_match = false;
                    for (hash, _) in &result {
                        if hash == &v.get_hash_code() {
                            is_match = true;
                            break;
                        }
                    }
                    if is_match == false {
                        result.push((v.get_hash_code(), v.clone()));
                    }
                },
                None => {}
            }
        }
        result
    }
    pub(crate) fn get_all_font(&self) -> Vec<(String, Font)> {
        let mut result:Vec<(String, Font)> = Vec::new();
        let def = Font::get_defalut_value();
        result.push((def.get_hash_code(), def));
        for (_, style) in &self.get_all_cell_style() {
            match style.get_font() {
                Some(v) => {
                    let mut is_match = false;
                    for (hash, _) in &result {
                        if hash == &v.get_hash_code() {
                            is_match = true;
                            break;
                        }
                    }
                    if is_match == false {
                        result.push((v.get_hash_code(), v.clone()));
                    }
                }
                None => {}
            }
        }
        result
    }

    pub(crate) fn get_all_fill(&self) -> Vec<(String, Fill)> {
        let mut result:Vec<(String, Fill)> = Vec::new();
        let def = Fill::get_defalut_value();
        result.push((def.get_hash_code(), def));
        let def2 = Fill::get_defalut_value_2();
        result.push((def2.get_hash_code(), def2));
        for (_, style) in &self.get_all_cell_style() {
            match style.get_fill() {
                Some(v) => {
                    let mut is_match = false;
                    for (hash, _) in &result {
                        if hash == &v.get_hash_code() {
                            is_match = true;
                            break;
                        }
                    }
                    if is_match == false {
                        result.push((v.get_hash_code(), v.clone()));
                    }
                },
                None => {}
            }
        }
        result
    }

    pub(crate) fn get_all_borders(&self) -> Vec<(String, Borders)> {
        let mut result:Vec<(String, Borders)> = Vec::new();
        let def = Borders::get_defalut_value();
        result.push((def.get_hash_code(), def));
        for (_, style) in &self.get_all_cell_style() {
            match style.get_borders() {
                Some(v) => {
                    let mut is_match = false;
                    for (hash, _) in &result {
                        if hash == &v.get_hash_code() {
                            is_match = true;
                            break;
                        }
                    }
                    if is_match == false {
                        result.push((v.get_hash_code(), v.clone()));
                    }
                },
                None => {}
            }
        }
        result
    }

    pub(crate) fn get_all_cell_style(&self) -> Vec<(String, Style)> {
        let mut result:Vec<(String, Style)> = Vec::new();
        let def = Style::get_defalut_value();
        result.push((def.get_hash_code(), def));
        for worksheet in &self.work_sheet_collection {
            for style in worksheet.get_style_collection() {
                let mut is_match = false;
                for (hash, _) in &result {
                    if hash == &style.get_hash_code() {
                        is_match = true;
                        break;
                    }
                }
                if is_match == false {
                    result.push((style.get_hash_code(), style.clone()));
                }
            }
        }
        result
    }

    pub fn get_sheet_collection(&self) -> &Vec<Worksheet> {
        &self.work_sheet_collection
    }

    pub fn get_sheet_count(&self) -> usize {
        self.work_sheet_collection.len()
    }

    pub fn get_sheet(&self, index:usize) -> Result<&Worksheet, &'static str> {
        match &self.work_sheet_collection.get(index) {
            Some(v) => return Ok(v),
            None => return Err("Not found.")
        }
    }

    pub fn get_sheet_mut(&mut self, index:usize) -> &mut Worksheet {
        self.work_sheet_collection.get_mut(index).unwrap()
    }

    pub fn get_sheet_by_name<S: Into<String>>(&self, value:S) -> Result<&Worksheet, &'static str> {
        let v = value.into();
        for sheet in &self.work_sheet_collection {
            if sheet.get_title() == &v {
                return Ok(sheet);
            }
        }
        Err("not found.")
    }

    pub fn get_sheet_by_sheet_id<S: Into<String>>(&self, value:S) -> Result<&Worksheet, &'static str> {
        let v = value.into();
        for sheet in &self.work_sheet_collection {
            if sheet.get_sheet_id() == &v {
                return Ok(sheet);
            }
        }
        Err("not found.")
    }

    pub fn get_sheet_by_name_mut<S: Into<String>>(&mut self, value:S) -> Result<&mut Worksheet, &'static str> {
        let v = value.into();
        for sheet in &mut self.work_sheet_collection {
            if sheet.get_title() == &v {
                return Ok(sheet);
            }
        }
        Err("not found.")
    }

    pub fn new_sheet<S: Into<String>>(&mut self, value:S) -> Result<&mut Worksheet, &'static str> {
        let v = value.into();
        match Spreadsheet::check_sheet_title(self, &v) {
            Ok(_) => {},
            Err(e) => return Err(e)
        }
        let sheet_id = (self.work_sheet_collection.len() + 1).to_string();
        Ok(Spreadsheet::new_sheet_crate(self, sheet_id, v))
    }

    pub(crate) fn new_sheet_crate<S: Into<String>>(&mut self, sheet_id:S, value:S) -> &mut Worksheet {
        let mut worksheet = Worksheet::default();
        worksheet.set_sheet_id(sheet_id.into());
        worksheet.set_title(value.into());
        self.work_sheet_collection.push(worksheet);
        self.work_sheet_collection.last_mut().unwrap()
    }

    pub fn set_sheet_title<S: Into<String>>(&mut self, index:usize, value:S) -> Result<(), &'static str>{
        let v = value.into();
        match Spreadsheet::check_sheet_title(self, &v) {
            Ok(_) => {},
            Err(e) => return Err(e)
        }
        match self.work_sheet_collection.get_mut(index) {
            Some(sheet) => {
                sheet.set_title(v);
                Ok(())
            },
            None => return Err("sheet not found.")
        }
    }

    pub(crate) fn check_sheet_title<S: Into<String>>(&self, value:S) -> Result<(), &'static str> {
        let v = value.into();
        for work_sheet in &self.work_sheet_collection {
            if &v == work_sheet.get_title() {
                return Err("title duplicate.");
            }
        }
        Ok(())
    }

    pub fn has_ribbon(&self) -> bool {
        self.ribbon_xml_data.is_some()
    }

    pub fn has_formula(&self) -> bool {
        for worksheet in &self.work_sheet_collection {
            for cell in worksheet.get_cell_collection() {
                if cell.get_formula() != "" {
                    return true;
                }
            }
        }
        false
    }
}