Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/structs/workbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ use std::sync::{
};

use crate::{
StringValue, XlsxError, helper::{
StringValue,
XlsxError,
helper::{
address::split_address,
coordinate::column_index_from_string,
}, reader::xlsx::raw_to_deserialize_by_worksheet, structs::{
},
reader::xlsx::raw_to_deserialize_by_worksheet,
structs::{
Address,
CellValue,
Cells,
Expand All @@ -19,10 +23,11 @@ use crate::{
WorkbookView,
Worksheet,
drawing::Theme,
}, traits::{
},
traits::{
AdjustmentCoordinate,
AdjustmentCoordinateWithSheet,
}
},
};

/// A Workbook Object.
Expand Down Expand Up @@ -399,6 +404,13 @@ impl Workbook {
&self.stylesheet
}

/// (This method is crate only.)
/// Get Stylesheet.
#[inline]
pub(crate) fn stylesheet_mut(&mut self) -> &mut Stylesheet {
&mut self.stylesheet
}

#[inline]
#[deprecated(since = "3.0.0", note = "Use stylesheet()")]
pub(crate) fn get_stylesheet(&self) -> &Stylesheet {
Expand Down Expand Up @@ -636,10 +648,7 @@ impl Workbook {

#[inline]
#[deprecated(since = "3.0.0", note = "Use sheet_by_name_mut()")]
pub fn get_sheet_by_name_mut(
&mut self,
sheet_name: &str,
) -> Result<&mut Worksheet, XlsxError> {
pub fn get_sheet_by_name_mut(&mut self, sheet_name: &str) -> Result<&mut Worksheet, XlsxError> {
self.sheet_by_name_mut(sheet_name)
}

Expand Down Expand Up @@ -685,8 +694,7 @@ impl Workbook {
/// # Arguments
/// * `value` - Work Sheet
/// # Return value
/// * `Result<&mut Worksheet, XlsxError>` - OK:added work sheet.
/// Err:Error.
/// * `Result<&mut Worksheet, XlsxError>` - OK:added work sheet. Err:Error.
#[inline]
pub fn add_sheet(&mut self, value: Worksheet) -> Result<&mut Worksheet, XlsxError> {
let title = value.name();
Expand All @@ -695,6 +703,14 @@ impl Workbook {
Ok(self.work_sheet_collection.last_mut().unwrap())
}

/// Remove and return all worksheets, leaving the collection empty.
/// Used by the streaming writer to take ownership of pre-built sheets while
/// keeping the workbook-level configuration.
#[inline]
pub(crate) fn take_all_sheets(&mut self) -> Vec<Worksheet> {
std::mem::take(&mut self.work_sheet_collection)
}

/// Remove Work Sheet.
/// # Arguments
/// * `index` - sheet index
Expand Down Expand Up @@ -729,8 +745,7 @@ impl Workbook {
/// # Arguments
/// * `sheet_title` - sheet title
/// # Return value
/// * `Result<&mut Worksheet, XlsxError>` - OK:added work sheet.
/// Err:Error.
/// * `Result<&mut Worksheet, XlsxError>` - OK:added work sheet. Err:Error.
#[inline]
pub fn new_sheet<S: Into<String>>(
&mut self,
Expand Down
59 changes: 22 additions & 37 deletions src/structs/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ impl Worksheet {
#[inline]
#[must_use]
pub fn cells_sorted(&self) -> Vec<&Cell> {
self.cells
.iter_cells_sorted_by_row_column()
.collect()
self.cells.iter_cells_sorted_by_row_column().collect()
}

#[inline]
Expand Down Expand Up @@ -393,9 +391,7 @@ impl Worksheet {
#[inline]
#[must_use]
pub fn collection_by_column(&self, column_num: u32) -> Vec<&Cell> {
self.cells
.iter_cells_by_column(column_num)
.collect()
self.cells.iter_cells_by_column(column_num).collect()
}

#[inline]
Expand Down Expand Up @@ -448,9 +444,7 @@ impl Worksheet {
/// # Arguments
/// * `cell` - Cell
pub fn set_cell(&mut self, cell: Cell) -> &mut Self {
let row_dimension = self
.row_dimension_mut(cell.coordinate().row_num())
.clone();
let row_dimension = self.row_dimension_mut(cell.coordinate().row_num()).clone();
let col_dimension = self
.column_dimension_by_number_mut(cell.coordinate().col_num())
.clone();
Expand Down Expand Up @@ -814,10 +808,7 @@ impl Worksheet {
pub fn threaded_comments_to_hashmap(&self) -> HashMap<String, &ThreadedComment> {
let mut result = HashMap::default();
for threaded_comment in &self.threaded_comments {
result.insert(
threaded_comment.coordinate().to_string(),
threaded_comment,
);
result.insert(threaded_comment.coordinate().to_string(), threaded_comment);
}
result
}
Expand Down Expand Up @@ -897,10 +888,8 @@ impl Worksheet {
let mut result: Vec<(String, &Hyperlink)> = Vec::new();
for cell in self.cells.collection_sorted() {
if let Some(hyperlink) = cell.hyperlink() {
let coordition = coordinate_from_index(
cell.coordinate().col_num(),
cell.coordinate().row_num(),
);
let coordition =
coordinate_from_index(cell.coordinate().col_num(), cell.coordinate().row_num());
result.push((coordition, hyperlink));
}
}
Expand Down Expand Up @@ -2602,7 +2591,7 @@ impl Worksheet {
}
result
}

#[deprecated(since = "3.0.0", note = "Use pivot_cache_definition_collection()")]
pub(crate) fn get_pivot_cache_definition_collection(&self) -> Vec<&str> {
self.pivot_cache_definition_collection()
Expand Down Expand Up @@ -2750,7 +2739,8 @@ impl Worksheet {
let mut copy_cells: Vec<Cell> = self
.cells
.iter_all_cells_by_range_sorted_by_row(range)
.flatten().cloned()
.flatten()
.cloned()
.collect();

// Delete cell information as iterating through in move mode
Expand Down Expand Up @@ -2789,10 +2779,7 @@ impl Worksheet {
if !cell.is_visually_empty() {
return;
}
indexes.push((
cell.coordinate().row_num(),
cell.coordinate().col_num(),
));
indexes.push((cell.coordinate().row_num(), cell.coordinate().col_num()));
}
}

Expand Down Expand Up @@ -2921,13 +2908,12 @@ impl AdjustmentCoordinate for Worksheet {
);

// worksheet_drawing
self.worksheet_drawing
.adjustment_insert_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
self.worksheet_drawing.adjustment_insert_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);

// comments
for comment in &mut self.comments {
Expand Down Expand Up @@ -3022,13 +3008,12 @@ impl AdjustmentCoordinate for Worksheet {
);

// worksheet_drawing
self.worksheet_drawing_mut()
.adjustment_remove_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
self.worksheet_drawing_mut().adjustment_remove_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);

// comments
self.comments.retain(|x| {
Expand Down
39 changes: 24 additions & 15 deletions src/structs/writer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use quick_xml::Writer;
use zip::result::ZipResult;

use crate::{
helper::const_str::{
Expand All @@ -15,6 +16,8 @@ use crate::{
CUSTOM_PROPS_TYPE,
DRAWING_TYPE,
OLE_OBJECT_TYPE,
PIVOT_CACHE_DEF_TYPE,
PIVOT_TABLE_TYPE,
PKG_CHARTS,
PKG_DRAWINGS,
PKG_EMBEDDINGS,
Expand All @@ -24,36 +27,34 @@ use crate::{
SHEET_TYPE,
STYLES_TYPE,
TABLE_TYPE,
PIVOT_TABLE_TYPE,
PIVOT_CACHE_DEF_TYPE,
THEME_TYPE,
VBA_TYPE,
WORKBOOK_MACRO_TYPE,
WORKBOOK_TYPE,
XPROPS_TYPE,
},
reader::driver::zip_by_name,
structs::{
Workbook,
XlsxError,
},
reader::driver::zip_by_name,
writer::driver::{
make_file_from_bin,
make_file_from_writer,
},
};
pub struct WriterManager<'a, W: io::Seek + io::Write> {
files: Vec<String>,
arv: &'a mut zip::ZipWriter<W>,
is_light: bool,
table_no: i32,
pivot_table_no: i32,
pub struct WriterManager<W: io::Seek + io::Write> {
files: Vec<String>,
arv: zip::ZipWriter<W>,
is_light: bool,
table_no: i32,
pivot_table_no: i32,
pivot_cache_hash_list: Vec<String>,
}

impl<'a, W: io::Seek + io::Write> WriterManager<'a, W> {
impl<W: io::Seek + io::Write> WriterManager<W> {
#[inline]
pub fn new(arv: &'a mut zip::ZipWriter<W>) -> Self {
pub fn new(arv: zip::ZipWriter<W>) -> Self {
WriterManager {
files: Vec::new(),
arv,
Expand All @@ -64,6 +65,11 @@ impl<'a, W: io::Seek + io::Write> WriterManager<'a, W> {
}
}

#[inline]
pub fn finish(self) -> ZipResult<W> {
self.arv.finish()
}

#[inline]
pub fn set_is_light(&mut self, value: bool) -> &mut Self {
self.is_light = value;
Expand Down Expand Up @@ -100,7 +106,10 @@ impl<'a, W: io::Seek + io::Write> WriterManager<'a, W> {
(true, i32::try_from(v + 1).unwrap())
} else {
self.pivot_cache_hash_list.push(hash.to_string());
(false, i32::try_from(self.pivot_cache_hash_list.len()).unwrap())
(
false,
i32::try_from(self.pivot_cache_hash_list.len()).unwrap(),
)
}
}

Expand All @@ -111,7 +120,7 @@ impl<'a, W: io::Seek + io::Write> WriterManager<'a, W> {
writer: Writer<Cursor<Vec<u8>>>,
) -> Result<(), XlsxError> {
if !self.check_file_exist(target) {
make_file_from_writer(target, self.arv, writer, None, self.is_light)?;
make_file_from_writer(target, &mut self.arv, writer, None, self.is_light)?;
self.files.push(target.to_string());
}
Ok(())
Expand All @@ -120,7 +129,7 @@ impl<'a, W: io::Seek + io::Write> WriterManager<'a, W> {
#[inline]
pub(crate) fn add_bin(&mut self, target: &str, data: &[u8]) -> Result<(), XlsxError> {
if !self.check_file_exist(target) {
make_file_from_bin(target, self.arv, data, None, self.is_light)?;
make_file_from_bin(target, &mut self.arv, data, None, self.is_light)?;
self.files.push(target.to_string());
}
Ok(())
Expand All @@ -145,7 +154,7 @@ impl<'a, W: io::Seek + io::Write> WriterManager<'a, W> {

#[inline]
pub(crate) fn get_arv_mut(&mut self) -> &mut zip::ZipWriter<W> {
self.arv
&mut self.arv
}

#[inline]
Expand Down
1 change: 1 addition & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

pub mod csv;
pub(crate) mod driver;
pub mod streaming_writer;
pub mod xlsx;
Loading
Loading