### Complete Example with Document Properties Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/properties.md A full example demonstrating how to create a workbook, set document properties, add worksheet content, and save the file. Ensure you have the necessary imports for Workbook, DocProperties, Format, and Color. ```rust use rust_xlsxwriter::{Workbook, DocProperties, Format, Color}; fn main() -> Result<(), Box> { let mut workbook = Workbook::new(); // Set document properties let properties = DocProperties::new() .set_title("Sales Dashboard") .set_subject("Monthly Sales Performance") .set_author("Analytics Team") .set_keywords("sales, dashboard, performance, monthly") .set_comments("Auto-generated sales report") .set_company("Global Sales Inc"); workbook.set_properties(&properties); // Create worksheet let worksheet = workbook.add_worksheet(); // Add title let title_format = Format::new() .set_bold() .set_font_size(14); worksheet.write_with_format(0, 0, "Sales Dashboard", &title_format)?; // Add headers let header = Format::new() .set_bold() .set_background_color(Color::Gray) .set_border(crate::FormatBorder::Thin); worksheet.write_with_format(2, 0, "Region", &header)?; worksheet.write_with_format(2, 1, "Sales", &header)?; worksheet.write_with_format(2, 2, "Growth", &header)?; // Save file workbook.save("sales_report.xlsx")?; println!("Report created with document properties"); Ok(()) } ``` -------------------------------- ### Full Example: Creating Various Hyperlinks Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md A comprehensive example demonstrating how to create simple web links, links with custom display text, email links with tooltips, formatted links, and internal links to specific cells. ```rust use rust_xlsxwriter::{Workbook, Url, Format, Color, FormatUnderline}; fn main() -> Result<(), Box> { let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); // Set column width worksheet.set_column_width(0, 30)?; // Simple web link worksheet.write_url(0, 0, "https://www.rust-lang.org")?; // Custom text link worksheet.write_url(1, 0, Url::new("https://github.com/jmcnamara/rust_xlsxwriter") .set_text("View Source on GitHub") )?; // Email link worksheet.write_url(2, 0, Url::new("mailto:info@example.com") .set_text("Contact Us") .set_tip("Send feedback") )?; // Formatted link let link_format = Format::new() .set_font_color(Color::Green) .set_underline(FormatUnderline::Single); worksheet.write_url_with_format(3, 0, Url::new("https://docs.rs/rust_xlsxwriter") .set_text("API Documentation"), &link_format )?; // Internal link to cell worksheet.write_url(4, 0, Url::new("internal:Sheet1!B10") .set_text("Jump to B10") )?; workbook.save("links.xlsx")?; Ok(()) } ``` -------------------------------- ### Method Chaining Example Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/INDEX.md Demonstrates how to chain multiple Format configuration methods for concise styling. ```rust Format::new() .set_bold() .set_italic() .set_font_color(Color::Red) ``` -------------------------------- ### Page Setup & Printing Methods Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Methods for configuring page setup and printing options, such as paper size, orientation, margins, print area, headers, footers, and scaling. ```APIDOC ## Page Setup & Printing ### `pub fn set_paper_size(&mut self, paper_size: u8) -> &mut Worksheet` Sets paper size for printing. **Paper Sizes:** - 0 = Letter, 1 = Legal, 2 = A4, 3 = A5, 4 = B4, 5 = B5, etc. ### `pub fn set_landscape(&mut self) -> &mut Worksheet` Sets page orientation to landscape. ### `pub fn set_portrait(&mut self) -> &mut Worksheet` Sets page orientation to portrait (default). ### `pub fn set_margins(&mut self, left: f64, right: f64, top: f64, bottom: f64) -> Result<&mut Worksheet, XlsxError>` Sets all page margins in inches. ### `pub fn set_print_area(&mut self, first_row: RowNum, first_col: ColNum, last_row: RowNum, last_col: ColNum) -> Result<&mut Worksheet, XlsxError>` Defines range to print. ### `pub fn set_repeat_rows(&mut self, first_row: RowNum, last_row: RowNum) -> Result<&mut Worksheet, XlsxError>` Repeats rows on each printed page. ### `pub fn set_repeat_columns(&mut self, first_col: ColNum, last_col: ColNum) -> Result<&mut Worksheet, XlsxError>` Repeats columns on each printed page. ### `pub fn set_print_scale(&mut self, scale: u16) -> &mut Worksheet` Sets print scale percentage. ### `pub fn set_print_fit_to_pages(&mut self, width: u16, height: u16) -> &mut Worksheet` Fits output to specified pages. ### `pub fn set_header(&mut self, header: impl Into) -> &mut Worksheet` Sets page header text. ### `pub fn set_footer(&mut self, footer: impl Into) -> &mut Worksheet` Sets page footer text. ### `pub fn set_print_gridlines(&mut self, enable: bool) -> &mut Worksheet` Shows/hides gridlines in print. ``` -------------------------------- ### FTP Link Examples Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Use `ftp://` or `ftps://` schemes to link to FTP servers. These can specify directories and files. ```rust use rust_xlsxwriter::Url; // FTP server Url::new("ftp://ftp.example.com/directory/file.zip") // Secure FTP Url::new("ftps://secure.example.com/protected/file.zip") ``` -------------------------------- ### Table::new() Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/table.md Creates a new Table instance with default settings. This is the starting point for configuring a table. ```APIDOC ## Table::new() ### Description Creates a new Table with default settings. ### Returns - `Table`: A new `Table` instance ready for configuration. ### Example ```rust use rust_xlsxwriter::{Table, TableStyle}; let mut table = Table::new() .set_name("SalesData") .set_style(TableStyle::TableStyleMedium2); // Assuming 'worksheet' is a valid Worksheet object // worksheet.add_table(0, 0, 100, 5, &table)?; ``` ``` -------------------------------- ### Email Link Examples Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Create email links using the `mailto:` scheme. You can specify the recipient, subject, and body. ```rust use rust_xlsxwriter::Url; // Simple email Url::new("mailto:user@example.com") // With subject Url::new("mailto:user@example.com?subject=Hello%20World") // With subject and body Url::new("mailto:user@example.com?subject=Question&body=Please%20help") ``` -------------------------------- ### Web Link Examples Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Construct web URLs using `Url::new`. HTTPS is preferred for secure connections. URLs can include paths, anchors, and query strings. ```rust use rust_xlsxwriter::Url; // HTTP Url::new("http://example.com") // HTTPS (secure, preferred) Url::new("https://example.com") // With path Url::new("https://example.com/page/section#anchor") // With query string Url::new("https://api.example.com/search?q=rust&sort=date") ``` -------------------------------- ### File Link Examples Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Link to local files using the `file://` scheme. Ensure correct path separators for the target operating system. Relative paths are also supported. ```rust use rust_xlsxwriter::Url; // Local file (Windows) Url::new("file:///C:/Users/Documents/data.xlsx") // Local file (Unix/Mac) Url::new("file:///home/user/documents/data.xlsx") // Relative file Url::new("file:///../data/backup.xlsx") ``` -------------------------------- ### Generate an Excel file with various data types and formatting Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/README.md This snippet demonstrates how to create a new Excel file, define formats, and write strings, numbers, formulas, dates, URLs, merged cells, and images to a worksheet. It covers basic setup and saving the workbook. ```rust use rust_xlsxwriter::* fn main() -> Result<(), XlsxError> { // Create a new Excel file object. let mut workbook = Workbook::new(); // Create some formats to use in the worksheet. let bold_format = Format::new().set_bold(); let decimal_format = Format::new().set_num_format("0.000"); let date_format = Format::new().set_num_format("yyyy-mm-dd"); let merge_format = Format::new() .set_border(FormatBorder::Thin) .set_align(FormatAlign::Center); // Add a worksheet to the workbook. let worksheet = workbook.add_worksheet(); // Set the column width for clarity. worksheet.set_column_width(0, 22)?; // Write a string without formatting. worksheet.write(0, 0, "Hello")?; // Write a string with the bold format defined above. worksheet.write_with_format(1, 0, "World", &bold_format)?; // Write some numbers. worksheet.write(2, 0, 1)?; worksheet.write(3, 0, 2.34)?; // Write a number with formatting. worksheet.write_with_format(4, 0, 3.00, &decimal_format)?; // Write a formula. worksheet.write(5, 0, Formula::new("=SIN(PI()/4)"))?; // Write a date. let date = ExcelDateTime::from_ymd(2023, 1, 25)?; worksheet.write_with_format(6, 0, &date, &date_format)?; // Write some links. worksheet.write(7, 0, Url::new("https://www.rust-lang.org"))?; worksheet.write(8, 0, Url::new("https://www.rust-lang.org").set_text("Rust"))?; // Write some merged cells. worksheet.merge_range(9, 0, 9, 1, "Merged cells", &merge_format)?; // Insert an image. let image = Image::new("examples/rust_logo.png")?; worksheet.insert_image(1, 2, &image)?; // Save the file to disk. workbook.save("demo.xlsx")?; Ok(()) } ``` -------------------------------- ### DocProperties::new Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/properties.md Creates a new DocProperties instance with default (empty) metadata. This is the starting point for configuring document properties. ```APIDOC ## DocProperties::new ### Description Creates a new `DocProperties` with default (empty) metadata. ### Returns A new `DocProperties` instance ready for configuration. ### Example ```rust use rust_xlsxwriter::DocProperties; let properties = DocProperties::new() .set_author("John Doe") .set_title("Sales Report") .set_subject("Q4 2024"); ``` ``` -------------------------------- ### Create a new TableColumn Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/table.md Use `TableColumn::new()` to create a new instance for column configuration. This is the starting point for setting various column properties. ```rust let column = TableColumn::new() .set_header("Product Name"); ``` -------------------------------- ### Format::new Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/format.md Creates a new Format object with default styling. This is the starting point for configuring cell formats. ```APIDOC ## Format::new ### Description Creates a new, empty Format with default (no) styling applied. This is the entry point for defining cell formatting. ### Method `new()` ### Returns A new `Format` instance ready for configuration. ### Example ```rust use rust_xlsxwriter::{Format, Color, FormatBorder}; let format = Format::new() .set_bold() .set_font_color(Color::Red) .set_border(FormatBorder::Thin); // Assuming 'worksheet' is a valid Worksheet object // worksheet.write_with_format(0, 0, "Styled", &format)?; ``` ``` -------------------------------- ### Quick Start: Create and Write to an Excel File Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/README.md This snippet demonstrates the basic usage of rust_xlsxwriter to create a new workbook, add a worksheet, write data with formatting, insert a chart, and save the file. Ensure the 'rust_xlsxwriter' crate is added to your Cargo.toml. ```rust use rust_xlsxwriter::*; fn main() -> Result<(), XlsxError> { // Create workbook and worksheet let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); // Write data with formatting let bold = Format::new().set_bold(); worksheet.write_string(0, 0, "Hello")?; worksheet.write_with_format(1, 0, "World", &bold)?; // Create chart let mut chart = Chart::new(ChartType::Column); let series = chart.add_series(); series.set_values("Sheet1!$A$2:$A$10")?; worksheet.insert_chart(0, 2, &chart)?; // Save file workbook.save("output.xlsx")?; Ok(()) } ``` -------------------------------- ### Rust XLSX Formula Error Handling Examples Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Shows how to implement #N/A, IFERROR, and IFNA error handling directly within formula strings. ```rust Formula::new("=IF(B1=0,NA(),A1/B1)") // #N/A if divide by zero Formula::new("=IFERROR(A1/B1,0)") // 0 if any error Formula::new("=IFNA(A1,\"Missing\")") // Text if #N/A ``` -------------------------------- ### Common Excel Date/Time Functions Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Examples of common Excel date and time functions that can be used with Rust xlsxwriter. ```rust Formula::new("=TODAY()") ``` ```rust Formula::new("=NOW()") ``` ```rust Formula::new("=DATE(2024,1,15)") ``` ```rust Formula::new("=YEAR(A1)") ``` ```rust Formula::new("=MONTH(A1)") ``` ```rust Formula::new("=DAY(A1)") ``` ```rust Formula::new("=DATEDIF(A1,B1,\"D\")") ``` -------------------------------- ### Common Excel Math & Aggregate Functions Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Examples of common Excel math and aggregate functions that can be used with Rust xlsxwriter. ```rust Formula::new("=SUM(A1:A10)") ``` ```rust Formula::new("=AVERAGE(B1:B100)") ``` ```rust Formula::new("=MAX(C1:C50)") ``` ```rust Formula::new("=MIN(C1:C50)") ``` ```rust Formula::new("=COUNT(D1:D100)") ``` ```rust Formula::new("=PRODUCT(E1:E10)") ``` ```rust Formula::new("=SQRT(F1)") ``` ```rust Formula::new("=POWER(G1,2)") ``` ```rust Formula::new("=ABS(H1)") ``` -------------------------------- ### Formula Constructor Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Creates a new Formula object from an Excel formula string. The formula must start with an equals sign. ```APIDOC ## `pub fn new(formula: impl AsRef) -> Formula` ### Description Creates a new Formula from a formula string. ### Parameters #### Path Parameters - **formula** (impl AsRef) - Required - Excel formula string starting with `=` ### Returns A new `Formula` instance. ### Example ```rust use rust_xlsxwriter::Formula; let sum_formula = Formula::new("=SUM(A1:A10)"); let if_formula = Formula::new("=IF(B1>0,B1,-B1)"); let vlookup = Formula::new("=VLOOKUP(C1,Table1,2,FALSE)"); ``` ``` -------------------------------- ### Internal Link Examples Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Create internal links to specific cells, named ranges, or other sheets within the same workbook using the `internal:` prefix. ```rust use rust_xlsxwriter::Url; // Link to cell Url::new("internal:Sheet1!A1") // Link to named range Url::new("internal:SalesData") // Link to another sheet Url::new("internal:AnalysisSheet!B5:B20") ``` -------------------------------- ### Excel 365 Dynamic Array Functions Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Examples of Excel 365 dynamic array functions like FILTER, UNIQUE, SORT, XLOOKUP, and LET. ```rust // FILTER - Returns matching rows Formula::new("=FILTER(A:A,B:B>0)") ``` ```rust // UNIQUE - Returns unique values Formula::new("=UNIQUE(C:C)") ``` ```rust // SORT - Sorts range Formula::new("=SORT(D:D)") ``` ```rust // XLOOKUP - Modern lookup Formula::new("=XLOOKUP(E1,Table1[ID],Table1[Value])") ``` ```rust // LET - Named expressions (365) Formula::new("=LET(x,A1,y,B1,x+y)") ``` -------------------------------- ### Catching IO Errors Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/errors.md Handle file system or I/O errors that occur during file operations like saving the workbook. This example specifically catches `XlsxError::IoError`. ```rust match workbook.save("readonly_dir/output.xlsx") { Err(XlsxError::IoError(io_err)) => { println!("IO error: {}", io_err); } _ => {} } ``` -------------------------------- ### Common Excel Lookup Functions Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Examples of common Excel lookup functions that can be used with Rust xlsxwriter. ```rust Formula::new("=VLOOKUP(E1,Table1,2,FALSE)") ``` ```rust Formula::new("=HLOOKUP(F1,Data!A1:D100,3,FALSE)") ``` ```rust Formula::new("=INDEX(G1:G100,MATCH(H1,F1:F100,0))") ``` -------------------------------- ### Common Excel Logical Functions Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Examples of common Excel logical functions that can be used with Rust xlsxwriter. ```rust Formula::new("=IF(A1>10,\"High\",\"Low\")") ``` ```rust Formula::new("=AND(B1>0,B1<100)") ``` ```rust Formula::new("=OR(C1=\"Yes\",C1=\"Y\")") ``` ```rust Formula::new("=NOT(D1)") ``` -------------------------------- ### Create and Configure a New Table Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/table.md Creates a new `Table` instance, sets its name and style, and prepares it for addition to a worksheet. Requires importing `Table` and `TableStyle`. ```rust use rust_xlsxwriter::{Table, TableStyle}; let mut table = Table::new() .set_name("SalesData") .set_style(TableStyle::TableStyleMedium2); worksheet.add_table(0, 0, 100, 5, &table)?; ``` -------------------------------- ### write_column_matrix Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes a 2D matrix of values to the worksheet column by column, starting at a specified row and column. ```APIDOC ## pub fn write_column_matrix(&mut self, row: RowNum, col: ColNum, matrix: I) -> Result<&mut Worksheet, XlsxError> ### Description Writes a 2D matrix column-by-column. ### Parameters - **row**: `RowNum` - The starting row number. - **col**: `ColNum` - The starting column number. - **matrix**: `I` - A 2D matrix (iterable of iterables) to write to the worksheet. ### Returns - `Result<&mut Worksheet, XlsxError>` - Returns a mutable reference to the worksheet on success, or an `XlsxError` on failure. ``` -------------------------------- ### Create Default and Custom Formatted Hyperlinks Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Shows how to write a default hyperlink and a hyperlink with custom formatting including font color, underline style, and italics. ```rust use rust_xlsxwriter::{Url, Format, Color, FormatUnderline}; // Default hyperlink style (blue, underlined) let default_link = Url::new("https://example.com"); // worksheet.write_url(0, 0, default_link)?; // Custom hyperlink formatting let custom_format = Format::new() .set_font_color(Color::Red) .set_underline(FormatUnderline::Double) .set_italic(); // worksheet.write_url_with_format(1, 0, // Url::new("https://example.com"), // &custom_format // )?; ``` -------------------------------- ### write_row_matrix Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes a 2D matrix of values to the worksheet row by row, starting at a specified row and column. ```APIDOC ## pub fn write_row_matrix(&mut self, row: RowNum, col: ColNum, matrix: I) -> Result<&mut Worksheet, XlsxError> ### Description Writes a 2D matrix row-by-row. ### Parameters - **row**: `RowNum` - The starting row number. - **col**: `ColNum` - The starting column number. - **matrix**: `I` - A 2D matrix (iterable of iterables) to write to the worksheet. ### Returns - `Result<&mut Worksheet, XlsxError>` - Returns a mutable reference to the worksheet on success, or an `XlsxError` on failure. ``` -------------------------------- ### write_formula Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes an Excel formula string to a specified cell. The formula must start with an equals sign (`=`). ```APIDOC ## write_formula ### Description Writes an Excel formula string. Formula must start with `=`. ### Method `pub fn write_formula(&mut self, row: RowNum, col: ColNum, formula: impl Into) -> Result<&mut Worksheet, XlsxError>` ### Parameters #### Path Parameters - **row** (RowNum) - Required - The row number to write to. - **col** (ColNum) - Required - The column number to write to. #### Request Body - **formula** (`impl Into`) - Required - Formula object or string starting with `=`. ### Response - `Result<&mut Worksheet, XlsxError>` - Indicates success or failure. ``` -------------------------------- ### Create a Table with Custom Styling and Features Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/table.md Illustrates how to create a table with various advanced styling and feature options enabled, such as header and total rows, banded rows, first/last column highlighting, autofilter, and a specific dark style. ```rust let table = Table::new() .set_name("Report") .set_header_row(true) .set_total_row(true) .set_banded_rows(true) .set_first_column(true) .set_last_column(true) .set_autofilter(true) .set_style(TableStyle::TableStyleDark1); worksheet.add_table(0, 0, 100, 5, &table)?; ``` -------------------------------- ### Create and Configure DocProperties Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/properties.md Creates a new DocProperties instance and sets initial metadata properties like author, title, and subject using method chaining. ```rust use rust_xlsxwriter::DocProperties; let properties = DocProperties::new() .set_author("John Doe") .set_title("Sales Report") .set_subject("Q4 2024"); ``` -------------------------------- ### Create a Basic Table Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/table.md Demonstrates how to create a new workbook, add a worksheet, define a basic table with a name and style, and add it to the worksheet. The table will cover the range A1:C11. ```rust use rust_xlsxwriter::{Workbook, Table, TableStyle}; let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); let table = Table::new() .set_name("Data") .set_style(TableStyle::TableStyleMedium2); // Add table covering A1:C11 worksheet.add_table(0, 0, 10, 2, &table)?; workbook.save("output.xlsx")?; ``` -------------------------------- ### Common Excel Text Functions Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Examples of common Excel text manipulation functions that can be used with Rust xlsxwriter. ```rust Formula::new("=CONCATENATE(A1,\" - \",B1)") ``` ```rust Formula::new("=A1&B1") ``` ```rust Formula::new("=LEN(C1)") ``` ```rust Formula::new("=UPPER(D1)") ``` ```rust Formula::new("=LOWER(E1)") ``` ```rust Formula::new("=LEFT(F1,3)") ``` ```rust Formula::new("=MID(G1,2,5)") ``` ```rust Formula::new("=TRIM(H1)") ``` -------------------------------- ### Create a new Worksheet Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Demonstrates creating a new workbook and adding a worksheet to it. This is the typical entry point for worksheet operations. ```rust let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); worksheet.write_string(0, 0, "Hello")?; ``` -------------------------------- ### write_column_with_format Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes an iterator of values to a column starting at a specified row, applying a uniform format to all cells in the column. ```APIDOC ## pub fn write_column_with_format(&mut self, row: RowNum, col: ColNum, values: I, format: &Format) -> Result<&mut Worksheet, XlsxError> ### Description Writes column with uniform formatting. ### Parameters - **row**: `RowNum` - The starting row number. - **col**: `ColNum` - The starting column number. - **values**: `I` - An iterator of values to write to the column. - **format**: `&Format` - A reference to the `Format` object to apply to all cells in the column. ### Returns - `Result<&mut Worksheet, XlsxError>` - Returns a mutable reference to the worksheet on success, or an `XlsxError` on failure. ``` -------------------------------- ### write_row_with_format Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes an iterator of values to a row starting at a specified column, applying a uniform format to all cells in the row. ```APIDOC ## pub fn write_row_with_format(&mut self, row: RowNum, col: ColNum, values: I, format: &Format) -> Result<&mut Worksheet, XlsxError> ### Description Writes row with uniform formatting. ### Parameters - **row**: `RowNum` - The starting row number. - **col**: `ColNum` - The starting column number. - **values**: `I` - An iterator of values to write to the row. - **format**: `&Format` - A reference to the `Format` object to apply to all cells in the row. ### Returns - `Result<&mut Worksheet, XlsxError>` - Returns a mutable reference to the worksheet on success, or an `XlsxError` on failure. ``` -------------------------------- ### Create Formatted Tables in Rust Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/README.md Shows how to define table columns with headers, total functions, and formatting, then add a table to a worksheet. Requires importing `Table`, `TableColumn`, `TableFunction`, `TableStyle`, and `Format`. ```rust let columns = vec![ TableColumn::new().set_header("Name"), TableColumn::new() .set_header("Sales") .set_total_function(TableFunction::Sum) .set_format(Format::new().set_num_format("$#,##0.00")) ]; let table = Table::new() .set_name("SalesData") .set_columns(&columns) .set_total_row(true) .set_style(TableStyle::TableStyleMedium2); worksheet.add_table(0, 0, 100, 1, &table)?; ``` -------------------------------- ### Apply Custom Theme Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/workbook.md Use `use_custom_theme` to apply a custom theme from an XML file. Provide the path to the theme file. ```rust workbook.use_custom_theme("themes/corporate.xml")?; ``` -------------------------------- ### Write Column Data Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes an iterator of values to a specified column starting at a given row. Supports string data. ```rust worksheet.write_column(0, 0, vec!["A", "B", "C"])?; ``` -------------------------------- ### Create Charts in Rust Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/README.md Illustrates how to create a column chart, add a data series with names, values, and categories, and set chart titles and axis labels. Requires importing `Chart`, `ChartType`. ```rust let mut chart = Chart::new(ChartType::Column); let series = chart.add_series(); series.set_name("Sales")?; series.set_values("Sheet1!$A$2:$A$100")?; series.set_categories("Sheet1!$B$2:$B$100")?; chart.title().set_name("Sales Report"); chart.y_axis().set_name("Amount ($)"); chart.x_axis().set_name("Month"); worksheet.insert_chart(0, 5, &chart)?; ``` -------------------------------- ### set_vba_name Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/workbook.md Sets the name of the VBA project within the workbook. Name must be a valid identifier starting with a letter or underscore. ```APIDOC ## set_vba_name ### Description Sets the name of the VBA project within the workbook. Name must be a valid identifier starting with a letter or underscore. ### Method `pub fn set_vba_name(&mut self, name: impl Into) -> Result<&mut Workbook, XlsxError>` ### Parameters #### Path Parameters - **name** (`impl Into`) - Required - VBA project name ### Request Example ```rust workbook.add_vba_project("macros.bin")?; workbook.set_vba_name("MyMacros")?; ``` ``` -------------------------------- ### Create a Table with Custom Columns and Totals Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/table.md Shows how to define custom columns for a table, including headers, total functions (Sum, Average), and number formatting for the Commission column. This table includes a total row. ```rust let columns = vec![ TableColumn::new().set_header("Name"), TableColumn::new() .set_header("Sales") .set_total_function(TableFunction::Sum), TableColumn::new() .set_header("Commission") .set_total_function(TableFunction::Average) .set_format(Format::new().set_num_format("0.00%\"")) ]; let table = Table::new() .set_name("SalesData") .set_columns(&columns) .set_total_row(true); worksheet.add_table(0, 0, 50, 2, &table)?; ``` -------------------------------- ### Create and Apply Basic Format Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/format.md Create a new Format object and apply bold, red font color, and a thin border. This format can then be applied to a cell when writing data. ```rust use rust_xlsxwriter::{Format, Color, FormatBorder}; let format = Format::new() .set_bold() .set_font_color(Color::Red) .set_border(FormatBorder::Thin); worksheet.write_with_format(0, 0, "Styled", &format)?; ``` -------------------------------- ### write_column Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes an iterator of values to a column starting at a specified row. This is useful for adding multiple data points to a single column. ```APIDOC ## pub fn write_column(&mut self, row: RowNum, col: ColNum, values: I) -> Result<&mut Worksheet, XlsxError> ### Description Writes an iterator of values to a column starting at specified row. ### Parameters - **row**: `RowNum` - The starting row number. - **col**: `ColNum` - The starting column number. - **values**: `I` - An iterator of values to write to the column. ### Returns - `Result<&mut Worksheet, XlsxError>` - Returns a mutable reference to the worksheet on success, or an `XlsxError` on failure. ``` -------------------------------- ### Create New Column and Pie Charts Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/chart.md Demonstrates creating new chart instances of type Column and Pie using the `Chart::new` constructor. Ensure `ChartType` enum is imported. ```rust use rust_xlsxwriter::{Chart, ChartType}; let mut column_chart = Chart::new(ChartType::Column); let mut pie_chart = Chart::new(ChartType::Pie); ``` -------------------------------- ### Write Row Data Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes an iterator of values to a specified row starting at a given column. Supports string and numeric data. ```rust worksheet.write_row(0, 0, vec!["A", "B", "C"])?; worksheet.write_row(1, 0, vec![1, 2, 3])?; ``` -------------------------------- ### Create Url Hyperlinks Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Create new Url instances for web, email, or file links. Ensure the URL uses a supported scheme and does not exceed 2080 characters. ```rust use rust_xlsxwriter::Url; let web_link = Url::new("https://www.rust-lang.org"); let email_link = Url::new("mailto:contact@example.com"); let file_link = Url::new("file:///C:/Users/data.xlsx"); ``` -------------------------------- ### Set DocProperties Keywords Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/properties.md Sets keywords for the document, which can aid in searching. Keywords should be comma-separated. ```rust .set_keywords("sales, revenue, quarterly, 2024") ``` -------------------------------- ### Catching Serde Errors Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/errors.md Handle serialization/deserialization errors when using the `serde` feature to write data structures to worksheets. This example catches `XlsxError::SerdeError`. ```rust #[cfg(feature = "serde")] match worksheet.serialize(&data) { Err(XlsxError::SerdeError(msg)) => { println!("Serialize error: {}", msg); } _ => {} } ``` -------------------------------- ### Format Cell Ranges and Merge Cells in Rust Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/README.md Demonstrates applying formatting to a range of cells, merging multiple cells into one, and setting row height and column width. Requires importing `Format` and `Color`, `FormatAlign`. ```rust // Format range let header = Format::new() .set_bold() .set_background_color(Color::Gray); worksheet.set_range_format(0, 0, 0, 5, &header)?; // Merge cells let center = Format::new().set_align(FormatAlign::Center); worksheet.merge_range(0, 0, 0, 3, "Title", ¢er)?; // Set row/column sizes worksheet.set_row_height(0, 30)?; worksheet.set_column_width(0, 15)?; ``` -------------------------------- ### Combine Charts in Rust Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/chart.md Demonstrates how to combine two charts of different types, such as a column chart with a line chart, using the `combine` method. Ensure both charts are configured before combining. ```rust let mut main_chart = Chart::new(ChartType::Column); // ... configure main chart ... let mut line_chart = Chart::new(ChartType::Line); // ... configure line chart ... main_chart.combine(&line_chart); worksheet.insert_chart(0, 0, &main_chart)?; ``` -------------------------------- ### Format to Worksheet Flow Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/INDEX.md Shows the process of creating and configuring a Format object, then applying it to a worksheet cell. ```rust Format::new() ↓ Format::set_*() methods (chaining) ↓ Worksheet::write_with_format() ↓ Result in styled cell ``` -------------------------------- ### write_row Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes an iterator of values to a row starting at a specified column. This method is useful for efficiently adding multiple data points to a single row. ```APIDOC ## pub fn write_row(&mut self, row: RowNum, col: ColNum, values: I) -> Result<&mut Worksheet, XlsxError> where I: IntoIterator, I::Item: Into ### Description Writes an iterator of values to a row starting at specified column. ### Parameters - **row**: `RowNum` - The starting row number. - **col**: `ColNum` - The starting column number. - **values**: `I` - An iterator of values to write to the row. Each item must be convertible to `RichStringTuple`. ### Returns - `Result<&mut Worksheet, XlsxError>` - Returns a mutable reference to the worksheet on success, or an `XlsxError` on failure. ``` -------------------------------- ### Create Image from File Path Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/image.md Use `Image::new` to create an image object from a local file path. Ensure the file exists and is a supported format (PNG, JPG, GIF, BMP). ```rust use rust_xlsxwriter::Image; let image = Image::new("logo.png")?; worksheet.insert_image(0, 0, &image)?; ``` -------------------------------- ### Add and Configure Chart Series Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/chart.md Shows how to add data series to a chart and configure their values and names using `add_series` and `set_values`/`set_name` methods. Requires a worksheet object and chart instance. ```rust let mut chart = Chart::new(ChartType::Column); let series1 = chart.add_series(); series1.set_values("Sheet1!$A$2:$A$10")?; series1.set_name("Sales")?; let series2 = chart.add_series(); series2.set_values("Sheet1!$B$2:$B$10")?; series2.set_name("Costs")?; worksheet.insert_chart(0, 0, &chart)?; ``` -------------------------------- ### Catching Worksheet Name Apostrophe Errors Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/errors.md Handles errors when a worksheet name starts or ends with an apostrophe ('). Ensures names adhere to Excel's naming conventions. ```rust match worksheet.set_name("'Sheet1'") { Err(XlsxError::SheetnameStartsOrEndsWithApostrophe(name)) => { println!("Name starts or ends with apostrophe: {}", name); } _ => {} } ``` -------------------------------- ### Create and Save a New Workbook Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/workbook.md Creates a new Excel workbook, adds a worksheet, writes data to a cell, and saves the workbook to a file. Requires the `rust_xlsxwriter` crate. ```rust use rust_xlsxwriter::Workbook; let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); worksheet.write(0, 0, "Hello")?; workbook.save("output.xlsx")?; ``` -------------------------------- ### Configure Table Columns with Headers and Totals Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/table.md Define custom column configurations using `set_columns`. This allows you to set headers, specify total labels, and assign aggregation functions like Sum or Average to columns. ```rust let columns = vec![ TableColumn::new() .set_header("Product") .set_total_label("TOTAL"), TableColumn::new() .set_header("Sales") .set_total_function(TableFunction::Sum), TableColumn::new() .set_header("Units") .set_total_function(TableFunction::Average), ]; let table = Table::new() .set_columns(&columns); ``` -------------------------------- ### Writing Traditional Array Formulas Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Use `write_array_formula` to define traditional array formulas that perform calculations on ranges of cells. This example shows element-wise multiplication followed by a sum. ```rust worksheet.write_array_formula( 0, 0, 9, 0, Formula::new("=SUM(A1:A10*B1:B10)") // Element-wise multiply then sum )?; ``` -------------------------------- ### Basic Image Insertion Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/image.md Demonstrates the fundamental process of creating an Image object and inserting it into a worksheet at a specified cell. ```rust use rust_xlsxwriter::{Workbook, Image}; let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); let image = Image::new("logo.png")?; worksheet.insert_image(0, 0, &image)?; workbook.save("output.xlsx")?; ``` -------------------------------- ### Writing Dynamic Array Formulas Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/formula.md Use `write_dynamic_array_formula` for Excel 365+ dynamic array formulas. These formulas can spill results into multiple cells automatically. This example uses the FILTER function. ```rust worksheet.write_dynamic_array_formula( 0, 0, Formula::new("=FILTER(Data!A:D, Data!B:B>100)") )?; ``` -------------------------------- ### Scaled and Positioned Image Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/image.md Shows how to set the dimensions (width and height) of an image before inserting it. ```APIDOC ## Scaled and Positioned Image ### Description Inserts an image with specified width and height, allowing for scaling. ### Code ```rust let image = Image::new("photo.jpg")? .set_width(400) .set_height(300); worksheet.insert_image(2, 1, &image)?; ``` ``` -------------------------------- ### Create Image with Hyperlink Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/image.md Create an image and set it as a clickable hyperlink to a URL. Supports custom link text by creating a Url object. ```rust let image = Image::new("button.png")? .set_url("https://example.com")?; ``` ```rust let url = Url::new("https://github.com").set_text("View Code"); let image = Image::new("github_icon.png")? .set_url(url)?; ``` -------------------------------- ### Set VBA Project Name Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/workbook.md Set the name of the VBA project within the workbook. The name must be a valid identifier starting with a letter or underscore. This is often used in conjunction with adding a VBA project. ```rust workbook.set_vba_name("MyMacros")?; ``` -------------------------------- ### Derive XlsxSerialize for Struct Serialization Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/macros/README.md Use the `XlsxSerialize` derive macro to define Excel formatting and options for structs when serializing to Excel. This example demonstrates setting bold headers, renaming fields, and applying number formats. ```rust use rust_xlsxwriter::{Workbook, XlsxError, XlsxSerialize}; use serde::Serialize; fn main() -> Result<(), XlsxError> { let mut workbook = Workbook::new(); // Add a worksheet to the workbook. let worksheet = workbook.add_worksheet(); // Create a serializable struct. #[derive(XlsxSerialize, Serialize)] #[xlsx(header_format = Format::new().set_bold())] struct Produce { #[xlsx(rename = "Item", column_width = 12.0)] fruit: &'static str, #[xlsx(rename = "Price", num_format = "$0.00")] cost: f64, } // Create some data instances. let items = [ Produce { fruit: "Peach", cost: 1.05, }, Produce { fruit: "Plum", cost: 0.15, }, Produce { fruit: "Pear", cost: 0.75, }, ]; // Set the serialization location and headers. worksheet.set_serialize_headers::(0, 0)?; // Serialize the data. worksheet.serialize(&items)?; // Save the file to disk. workbook.save("serialize.xlsx")?; Ok(()) } ``` -------------------------------- ### Workbook::new Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/workbook.md Creates a new, empty Excel workbook with no worksheets. This is the primary constructor for the Workbook struct. ```APIDOC ## new ### Description Creates a new, empty Excel workbook with no worksheets. ### Returns A new `Workbook` instance ready for worksheet and data addition. ### Example ```rust use rust_xlsxwriter::Workbook; let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); worksheet.write(0, 0, "Hello")?; workbook.save("output.xlsx")?; ``` ``` -------------------------------- ### Write URLs to Cells Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/url.md Use `write_url` for simple URLs or `write_url_with_format` for custom formatting. The `Url::new` constructor creates the URL object, which can be further configured with `set_text` and `set_tip`. ```rust use rust_xlsxwriter::{Color, Format, FormatUnderline, Url, Workbook, XlsxError}; fn main() -> Result<(), XlsxError> { let mut workbook = Workbook::new(); let worksheet = workbook.add_worksheet(); // Simple URL (URL as display text) worksheet.write_url(0, 0, "https://example.com")?; // URL with custom display text worksheet.write_url(1, 0, Url::new("https://github.com") .set_text("GitHub") )?; // URL with tooltip worksheet.write_url(2, 0, Url::new("https://docs.rs/rust_xlsxwriter") .set_text("Documentation") .set_tip("API docs for rust_xlsxwriter") )?; // URL with formatting let link_format = Format::new() .set_hyperlink() .set_font_color(Color::Blue) .set_underline(FormatUnderline::Single); worksheet.write_url_with_format(3, 0, Url::new("https://example.com"), &link_format )?; workbook.close()?; Ok(()) } ``` -------------------------------- ### Workbook to Worksheet Flow Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/INDEX.md Illustrates the typical sequence of creating a workbook, adding a worksheet, writing data, and saving the file. ```rust Workbook::new() ↓ Workbook::add_worksheet() ↓ Worksheet::write*() methods ↓ Workbook::save() ``` -------------------------------- ### Write a blank cell with formatting Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Shows how to write an empty cell but apply formatting to it, such as borders. Useful for styling cells without adding data. ```rust let border = Format::new().set_border(FormatBorder::Thin); worksheet.write_blank(0, 0, &border)?; ``` -------------------------------- ### ChartMarker Configuration Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/chart.md Methods for creating, configuring, and setting the type, size, format, and automatic or none visibility of data point markers. ```APIDOC ## ChartMarker Configures data point markers. ### `pub fn new() -> ChartMarker` Creates new marker configuration. ### `pub fn set_type(&mut self, marker_type: ChartMarkerType) -> &mut ChartMarker` Sets marker shape type. ### `pub fn set_size(&mut self, size: u8) -> &mut ChartMarker` Sets marker size in points (2-72). ### `pub fn set_format(&mut self, format: T) -> &mut ChartMarker` Sets marker formatting (fill, border). ### `pub fn set_automatic(&mut self) -> &mut ChartMarker` Uses automatic marker selection. ### `pub fn set_none(&mut self) -> &mut ChartMarker` Removes markers. ``` -------------------------------- ### write_url_with_options Source: https://github.com/jmcnamara/rust_xlsxwriter/blob/main/_autodocs/worksheet.md Writes hyperlink with full options (text, format, tooltip). ```APIDOC ## pub fn write_url_with_options ### Description Writes hyperlink with full options (text, format, tooltip). ### Parameters #### Path Parameters - **row** (RowNum) - Required - Row number. - **col** (ColNum) - Required - Column number. - **url** ( impl Into ) - Required - URL object or string. - **text** ( Option<&str> ) - Optional - Display text for the hyperlink. - **format** ( Option<&Format> ) - Optional - Format for the hyperlink. - **tip** ( Option<&str> ) - Optional - Tooltip for the hyperlink. ### Returns `Result<&mut Worksheet, XlsxError>` ```