pub trait Read: _Read {
// Provided method
fn read_cell<L: Location>(&self, loc: L) -> WorkSheetResult<Cell<String>> { ... }
}Provided Methods§
Sourcefn read_cell<L: Location>(&self, loc: L) -> WorkSheetResult<Cell<String>>
fn read_cell<L: Location>(&self, loc: L) -> WorkSheetResult<Cell<String>>
Examples found in repository?
examples/read_and_copy.rs (line 45)
7fn main() -> WorkbookResult<()> {
8 // Read an existed workbook
9 let reading_book = Workbook::from_path("./tests/xlsx/accounting.xlsx")?;
10 let reading_sheet = reading_book.get_worksheet_by_name("worksheet")?;
11 // Create a new workbook to write
12 let mut writing_book = Workbook::new();
13 let writing_sheet = writing_book.get_worksheet_mut(1)?;
14
15 // Synchronous column width and format
16 let columns_map = reading_sheet.get_columns_with_format("A:XFD")?;
17 match reading_sheet.get_default_column() {
18 None => writing_sheet.set_default_column_adaptive(),
19 Some(width) => writing_sheet.set_default_column(width),
20 }
21 columns_map.iter().for_each(|(col_range, (column, format))| {
22 if let Some(format) = format {
23 // if col format exists, write it to writing_sheet
24 writing_sheet.set_columns_with_format(col_range, column, format).unwrap()
25 } else {
26 writing_sheet.set_columns(col_range, column).unwrap()
27 }
28 });
29
30 // Synchronous row height and format
31 writing_sheet.set_default_row(reading_sheet.get_default_row());
32 for row_number in 1..=reading_sheet.max_row() {
33 let (row, format) = reading_sheet.get_row_with_format(row_number)?;
34 if let Some(format) = format {
35 // if col format exists, write it to writing_sheet
36 writing_sheet.set_row_with_format(row_number, &row, &format)?;
37 } else {
38 writing_sheet.set_row(row_number, &row)?;
39 }
40 }
41
42 // Read then write text and format
43 for row in 1..=reading_sheet.max_row() {
44 for col in 1..=reading_sheet.max_column() {
45 if let Ok(cell) = reading_sheet.read_cell((row, col)) {
46 writing_sheet.write_cell((row, col), &cell)?;
47 }
48 }
49 }
50
51 writing_book.save_as("./examples/read_and_copy.xlsx")?;
52 Ok(())
53}More examples
examples/xlsx2adoc.rs (line 146)
99pub fn xlsx_convert(
100 in_file_name: &Path,
101 out_file_name: &Path,
102) -> Result<Xlsx2AdocTestResults, Error> {
103 let workbook = Workbook::from_path(in_file_name);
104 let mut workbook = workbook.unwrap();
105 workbook.finish();
106
107 let reading_sheet = workbook.get_worksheet(1);
108 let sheet = reading_sheet.unwrap();
109 let default_row_hight = sheet.get_default_row();
110
111 println!(
112 "Rows {} -> ? ( {} )",
113 sheet.max_row(),
114 // formatted_row.len(),
115 default_row_hight
116 );
117
118 let mut hights = Vec::<f64>::new();
119 for _ in 0..sheet.max_row() {
120 hights.push(default_row_hight);
121 }
122
123 let widths = find_col_width(sheet)?;
124 let bounds = "|===\r";
125 let line = tab_header(&widths);
126
127 let mut output_file = File::create(out_file_name)?; // overwrites existing file
128 let mut writer = BufWriter::new(&mut output_file);
129
130 writer.write(line.as_bytes())?;
131 writer.write(bounds.as_bytes())?;
132
133 /* // todo test
134 writer.write("|1|2|3\r".as_bytes())?;
135 writer.write("|4|5|6\r".as_bytes())?;
136
137 writer.write(bounds.as_bytes())?;
138 */
139 for row in 0..sheet.max_row() {
140 println!("Row {} ({})", row, hights[row as usize]);
141 for col in 0..sheet.max_column() {
142 if col < sheet.max_column() {
143 writer.write("|".as_bytes())?;
144 }
145
146 let cell_content = sheet.read_cell((row + 1, col + 1)).unwrap_or_default();
147 let format = cell_content.format;
148 let mut text_color = FormatColor::Default;
149 let mut bg_color = FormatColor::Default;
150 let mut bg_bg_color = FormatColor::Default;
151 if format.is_some() {
152 let format = format.unwrap();
153 text_color = format.get_color().clone();
154 let ff = format.get_background().clone();
155 bg_color = ff.fg_color.clone();
156 bg_bg_color = ff.bg_color.clone();
157 }
158
159 let cell_format_string = format!(
160 "Text-Color = {:?} bg = {:?} bg_bg = {:?}",
161 text_color, bg_color, bg_bg_color
162 );
163 let cell_text = cell_content.text;
164 let text = match cell_text {
165 Some(t) => t,
166 None => "-".to_owned(),
167 };
168
169 println!(
170 "{} ({}) -> {} Format: {}",
171 col,
172 widths[(col) as usize],
173 text,
174 cell_format_string
175 );
176 writer.write(text.as_bytes())?;
177 }
178
179 writer.write("\r".as_bytes())?;
180 }
181 writer.write(bounds.as_bytes())?;
182
183 let xlsx_2_adoc_test_results = Xlsx2AdocTestResults { v1: 0, v2: 0 };
184 Ok(xlsx_2_adoc_test_results)
185}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.