excelize/
utils.rs

1// Copyright 2021 The excelize Authors. All rights reserved. Use of
2// this source code is governed by a BSD-style license that can be found in
3// the LICENSE file.
4
5use crate::ExcelizeError;
6
7static _MAX_FONT_FAMILY_LENGTH: u32 = 31;
8static _MAX_FONT_SIZE: u32 = 409;
9static _MAX_FILE_NAME_LENGTH: u32 = 207;
10static _MAX_COLUMN_WIDTH: u32 = 255;
11static _MAX_ROW_HEIGHT: u32 = 409;
12static _TOTAL_ROWS: u32 = 1048576;
13static TOTAL_COLUMNS: u32 = 16384;
14static _TOTAL_SHEET_HYPERLINKS: u32 = 65529;
15static _TOTAL_CELL_CHARS: u32 = 32767;
16
17// column_number_to_name provides a function to convert the integer to Excel
18// sheet column title.
19pub fn column_number_to_name(col: u32) -> Result<String, ExcelizeError> {
20    if col < 1 {
21        let err = format!("incorrect column number {}", col);
22        return Err(ExcelizeError::CommonError(err));
23    }
24    if col > TOTAL_COLUMNS {
25        return Err(ExcelizeError::CommonError(String::from(
26            "column number exceeds maximum limit",
27        )));
28    }
29    let mut column = "".to_string();
30    if col > 0 {
31        let mut v = col;
32        let a = b'A';
33        while v > 0 {
34            let curt_i = ((v - 1) % 26) as u8;
35            let curt_c = (a + curt_i) as char;
36            column.insert(0, curt_c);
37            v = (v - 1) / 26;
38        }
39    }
40    Ok(column)
41}