Skip to content

Commit

Permalink
serde: add column width support for custom headers
Browse files Browse the repository at this point in the history
Feature request: #63
  • Loading branch information
jmcnamara committed Dec 30, 2023
1 parent 4c467a4 commit aca115c
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,8 @@ pub struct CustomSerializeHeader {
pub(crate) skip: bool,
pub(crate) row: RowNum,
pub(crate) col: ColNum,
pub(crate) width: Option<f64>,
pub(crate) pixel_width: Option<u16>,
}

impl CustomSerializeHeader {
Expand All @@ -1359,6 +1361,8 @@ impl CustomSerializeHeader {
skip: false,
row: 0,
col: 0,
width: None,
pixel_width: None,
}
}

Expand Down Expand Up @@ -1741,6 +1745,18 @@ impl CustomSerializeHeader {
self.skip = enable;
self
}

/// TODO
pub fn set_column_width(mut self, width: impl Into<f64>) -> CustomSerializeHeader {
self.width = Some(width.into());
self
}

/// TODO
pub fn set_column_width_pixels(mut self, width: u16) -> CustomSerializeHeader {
self.pixel_width = Some(width);
self
}
}

// -----------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6686,6 +6686,13 @@ impl Worksheet {
custom_header.row = row;
custom_header.col = col;

// Set the column width if specified by user.
if let Some(width) = custom_header.width {
self.set_column_width(col, width)?;
} else if let Some(pixel_width) = custom_header.pixel_width {
self.set_column_width_pixels(col, pixel_width)?;
}

if !header_options.hide_headers {
// Use the column specific header format or else the header row
// format, and if neither of those have been specified then
Expand Down
Binary file added tests/input/serde12.xlsx
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ mod serde09;
mod serde10;
#[cfg(feature = "serde")]
mod serde11;
#[cfg(feature = "serde")]
mod serde12;
mod set_row01;
mod set_row02;
mod set_row03;
Expand Down
166 changes: 166 additions & 0 deletions tests/integration/serde12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Test case that compares a file generated by rust_xlsxwriter with a file
// created by Excel.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2023, John McNamara, [email protected]

use crate::common;
use rust_xlsxwriter::{CustomSerializeHeader, SerializeHeadersOptions, Workbook, XlsxError};
use serde::Serialize;

// Test case for Serde serialization. First test isn't serialized.
fn create_new_xlsx_file_1(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
worksheet.set_paper_size(9);
worksheet.set_column_width(1, 13.57)?;

// Not serialized.
worksheet.write(0, 0, "col1")?;
worksheet.write(1, 0, 1)?;
worksheet.write(2, 0, 2)?;
worksheet.write(3, 0, 3)?;

worksheet.write(0, 1, "col2")?;
worksheet.write(1, 1, 4)?;
worksheet.write(2, 1, 5)?;
worksheet.write(3, 1, 6)?;

worksheet.write(0, 2, "col3")?;
worksheet.write(1, 2, 7)?;
worksheet.write(2, 2, 8)?;
worksheet.write(3, 2, 9)?;

workbook.save(filename)?;

Ok(())
}

// Test case for Serde serialization. Set the column width.
fn create_new_xlsx_file_2(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
worksheet.set_paper_size(9);

// Create a serializable test struct.
#[derive(Serialize)]
struct MyStruct {
col1: u8,
col2: u8,
col3: u8,
}

let data1 = MyStruct {
col1: 1,
col2: 4,
col3: 7,
};

let data2 = MyStruct {
col1: 2,
col2: 5,
col3: 8,
};

let data3 = MyStruct {
col1: 3,
col2: 6,
col3: 9,
};

let header_options = SerializeHeadersOptions::new()
.set_custom_headers(&[CustomSerializeHeader::new("col2").set_column_width(13.57)]);

worksheet.serialize_headers_with_options(0, 0, &data1, &header_options)?;

worksheet.serialize(&data1)?;
worksheet.serialize(&data2)?;
worksheet.serialize(&data3)?;

workbook.save(filename)?;

Ok(())
}

// Test case for Serde serialization. Set the column width.
fn create_new_xlsx_file_3(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
worksheet.set_paper_size(9);

// Create a serializable test struct.
#[derive(Serialize)]
struct MyStruct {
col1: u8,
col2: u8,
col3: u8,
}

let data1 = MyStruct {
col1: 1,
col2: 4,
col3: 7,
};

let data2 = MyStruct {
col1: 2,
col2: 5,
col3: 8,
};

let data3 = MyStruct {
col1: 3,
col2: 6,
col3: 9,
};

let header_options = SerializeHeadersOptions::new()
.set_custom_headers(&[CustomSerializeHeader::new("col2").set_column_width_pixels(100)]);

worksheet.serialize_headers_with_options(0, 0, &data1, &header_options)?;

worksheet.serialize(&data1)?;
worksheet.serialize(&data2)?;
worksheet.serialize(&data3)?;

workbook.save(filename)?;

Ok(())
}

#[test]
fn test_serde12_1() {
let test_runner = common::TestRunner::new()
.set_name("serde12")
.set_function(create_new_xlsx_file_1)
.unique("1")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}

#[test]
fn test_serde12_2() {
let test_runner = common::TestRunner::new()
.set_name("serde12")
.set_function(create_new_xlsx_file_2)
.unique("2")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}

#[test]
fn test_serde12_3() {
let test_runner = common::TestRunner::new()
.set_name("serde12")
.set_function(create_new_xlsx_file_3)
.unique("3")
.initialize();

test_runner.assert_eq();
test_runner.cleanup();
}

0 comments on commit aca115c

Please sign in to comment.