Skip to content

Commit

Permalink
note: add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Jul 22, 2024
1 parent 84768cd commit a7b9164
Show file tree
Hide file tree
Showing 59 changed files with 1,413 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ impl Button {
text: self.name.clone(),
alt_text: self.alt_text.clone(),
macro_name: self.macro_name.clone(),
fill_color: "buttonFace [67]".to_string(),
..Default::default()
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,48 +114,48 @@ impl Comment {
self.writer.xml_start_tag("comment", &attributes);

// Write the text element.
self.write_text_block(&note.text);
self.write_text_block(note);

self.writer.xml_end_tag("comment");
}

// Write the <text> element.
fn write_text_block(&mut self, text: &str) {
fn write_text_block(&mut self, note: &Note) {
self.writer.xml_start_tag_only("text");
self.writer.xml_start_tag_only("r");

// Write the rPr element.
self.write_paragraph_run();
self.write_paragraph_run(note);

// Write the t text element.
self.write_text(text);
self.write_text(&note.text);

self.writer.xml_end_tag("r");
self.writer.xml_end_tag("text");
}

// Write the <rPr> element.
fn write_paragraph_run(&mut self) {
fn write_paragraph_run(&mut self, note: &Note) {
self.writer.xml_start_tag_only("rPr");

// Write the sz element.
self.write_font_size();
self.write_font_size(note);

// Write the color element.
self.write_font_color();

// Write the rFont element.
self.write_font_name();
self.write_font_name(note);

// Write the family element.
self.write_font_family();
self.write_font_family(note);

self.writer.xml_end_tag("rPr");
}

// Write the <sz> element.
fn write_font_size(&mut self) {
let attributes = [("val", "8".to_string())];
fn write_font_size(&mut self, note: &Note) {
let attributes = [("val", note.format.font.size.to_string())];

self.writer.xml_empty_tag("sz", &attributes);
}
Expand All @@ -168,15 +168,15 @@ impl Comment {
}

// Write the <rFont> element.
fn write_font_name(&mut self) {
let attributes = [("val", "Tahoma".to_string())];
fn write_font_name(&mut self, note: &Note) {
let attributes = [("val", note.format.font.name.clone())];

self.writer.xml_empty_tag("rFont", &attributes);
}

// Write the <family> element.
fn write_font_family(&mut self) {
let attributes = [("val", "2".to_string())];
fn write_font_family(&mut self, note: &Note) {
let attributes = [("val", note.format.font.family.to_string())];

self.writer.xml_empty_tag("family", &attributes);
}
Expand Down
12 changes: 12 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2560,6 +2560,18 @@ impl Color {
}
}

// Get the RGB hex value for a VML fill color in "#rrggbb" format.
pub(crate) fn vml_rgb_hex_value(self) -> String {
match self {
// Use Comment default color for non RGB colors.
Color::Theme(_, _) | Color::Default | Color::Automatic => "#ffffe1".to_string(),
_ => {
let rgb_color = Self::rgb_hex_value(self).to_lowercase();
format!("#{rgb_color}")
}
}
}

// Get the ARGB hex value for a color. The alpha channel is always FF.
pub(crate) fn argb_hex_value(self) -> String {
format!("FF{}", self.rgb_hex_value())
Expand Down
114 changes: 113 additions & 1 deletion src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use crate::drawing::{DrawingObject, DrawingType};
use crate::vml::VmlInfo;
use crate::{ColNum, ObjectMovement, RowNum, COL_MAX, ROW_MAX};
use crate::{ColNum, Format, IntoColor, ObjectMovement, RowNum, COL_MAX, ROW_MAX};

#[derive(Clone)]
/// The `Note` struct represents an worksheet note object.
Expand All @@ -31,6 +31,8 @@ pub struct Note {
pub(crate) alt_text: String,
pub(crate) object_movement: ObjectMovement,
pub(crate) decorative: bool,
pub(crate) is_visible: Option<bool>,
pub(crate) format: Format,
}

impl Note {
Expand All @@ -41,6 +43,11 @@ impl Note {
/// Create a new Note object to represent an Excel Form Control note.
///
pub fn new(text: impl Into<String>) -> Note {
let format = Format::new()
.set_background_color("#FFFFE1")
.set_font_name("Tahoma")
.set_font_size(8);

Note {
row: None,
col: None,
Expand All @@ -58,6 +65,8 @@ impl Note {
alt_text: String::new(),
object_movement: ObjectMovement::DontMoveOrSizeWithCells,
decorative: false,
is_visible: None,
format,
}
}

Expand Down Expand Up @@ -107,6 +116,107 @@ impl Note {
self
}

/// TODO
///
/// # Parameters
///
/// - `todo`:
///
pub fn set_visible(mut self, enable: bool) -> Note {
self.is_visible = Some(enable);
self
}

/// Set the TODO pattern background color property.
///
///
/// # Parameters
///
/// - `color`: The background color property defined by a
/// [`Color`](crate::Color) enum value or a type that implements the
/// [`IntoColor`] trait.
///
pub fn set_background_color<T>(mut self, color: T) -> Note
where
T: IntoColor,
{
let color = color.new_color();
if color.is_valid() {
self.format.fill.background_color = color;
}

self
}

/// Set the Format font name property. TODO
///
/// Set the font for a cell format. Excel can only display fonts that are
/// installed on the system that it is running on. Therefore it is generally
/// best to use standard Excel fonts.
///
/// # Parameters
///
/// - `font_name`: The font name property.
///
pub fn set_font_name(mut self, font_name: impl Into<String>) -> Note {
self.format.font.name = font_name.into();

if self.format.font.name != "Calibri" {
self.format.font.scheme = String::new();
}

self
}

/// Set the Format font size property. TODO
///
/// Set the font size of the cell format. The size is generally an integer
/// value but Excel allows x.5 values (hence the property is a f64 or
/// types that can convert [`Into`] a f64).
///
/// Excel adjusts the height of a row to accommodate the largest font size
/// in the row.
///
/// # Parameters
///
/// - `font_size`: The font size property.
///
pub fn set_font_size<T>(mut self, font_size: T) -> Note
where
T: Into<f64>,
{
self.format.font.size = font_size.into().to_string();
self
}

/// Set the Format font family property. TODO.
///
/// Set the font family. This is usually an integer in the range 1-4. This
/// function is implemented for completeness but is rarely used in practice.
///
/// # Parameters
///
/// - `font_family`: The font family property.
///
pub fn set_font_family(mut self, font_family: u8) -> Note {
self.format.font.family = font_family;
self
}

/// Set the [`Format`] of the conditional format rule.
///
/// Set the [`Format`] todo.
///
///
/// # Parameters
///
/// - `format`: The [`Format`] property for the cell Note.
///
pub fn set_format(mut self, format: impl Into<Format>) -> Note {
self.format = format.into();
self
}

/// Set the alt text for the note to help accessibility.
///
/// The alt text is used with screen readers to help people with visual
Expand Down Expand Up @@ -167,6 +277,8 @@ impl Note {
height: self.height,
text: self.text.clone(),
alt_text: self.alt_text.clone(),
is_visible: self.is_visible.unwrap_or(false),
fill_color: self.format.fill.background_color.vml_rgb_hex_value(),
..Default::default()
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/packager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,12 @@ impl<W: Write + Seek + Send> Packager<W> {
self.write_vba_project(workbook)?;

let mut image_index = 1;
let mut vml_index = 1;

for worksheet in &mut workbook.worksheets {
if !worksheet.drawing_relationships.is_empty() {
self.write_drawing_rels_file(&worksheet.drawing_relationships, image_index)?;
image_index += 1;
}
if !worksheet.vml_drawing_relationships.is_empty() {
self.write_vml_drawing_rels_file(&worksheet.vml_drawing_relationships, vml_index)?;
vml_index += 1;
}
}

if options.has_metadata {
Expand Down Expand Up @@ -445,7 +440,7 @@ impl<W: Write + Seek + Send> Packager<W> {
pub(crate) fn write_vml_drawing_rels_file(
&mut self,
relationships: &[(String, String, String)],
index: usize,
index: u32,
) -> Result<(), XlsxError> {
let mut rels = Relationship::new();

Expand Down Expand Up @@ -792,12 +787,16 @@ impl<W: Write + Seek + Send> Packager<W> {
.append(&mut worksheet.header_footer_vml_info);

vml.data_id = format!("{header_data_id}");
vml.shape_id = 1024 * header_data_id;
header_data_id += 1;

vml.shape_id = 1024 * index;
vml.assemble_xml_file();

self.zip.write_all(vml.writer.xmlfile.get_ref())?;

// The rels file index must match the vmlDrawing file index.
self.write_vml_drawing_rels_file(&worksheet.vml_drawing_relationships, index)?;

index += 1;
}
}
Expand Down
Loading

0 comments on commit a7b9164

Please sign in to comment.