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
130 changes: 123 additions & 7 deletions rust/src/wrapper/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,28 @@ impl Format {
impl_method!(self.set_border_diagonal_color(color.inner));
}

/// Set the cell diagonal border direction type.
///
/// See {@link Format#setBorderDiagonal} for details.
///
/// @param {FormatDiagonalBorder}`border_type - The diagonal border type as defined by a {@link FormatDiagonalBorder} enum value.
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setBorderDiagonalType", skip_jsdoc)]
pub fn set_border_diagonal_type(&self, border: FormatDiagonalBorder) -> Format {
impl_method!(self.set_border_diagonal_type(border.into()));
}

/// Set the hyperlink style.
///
/// Set the hyperlink style for use with urls. This is usually set
/// automatically when writing urls without a format applied.
///
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setHyperlink", skip_jsdoc)]
pub fn set_hyperlink(&self) -> Format {
impl_method!(self.set_hyperlink());
}

/// Set the color property for the Format font.
///
/// The `setFontColor()` method is used to set the font color in a cell.
Expand Down Expand Up @@ -527,6 +549,15 @@ impl Format {
impl_method!(self.set_font_strikethrough());
}

/// Set the Format font strikethrough property.
///
/// @param {boolean} strikethrough - The strikethrough property.
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setFormatScript", skip_jsdoc)]
pub fn set_format_script(&self, script: FormatScript) -> Format {
impl_method!(self.set_font_script(script.into()));
}

/// Set the Format pattern foreground color property.
///
/// The `set_foreground_color` method can be used to set the
Expand Down Expand Up @@ -587,10 +618,11 @@ impl Format {
/// To set the pattern colors see {@link Format#setBackgroundColor} and
/// {@link Format#setForegroundColor}
///
/// TODO: example omitted
///
/// @param {FormatPattern} pattern - The pattern property defined by a {@link FormatPattern} enum
/// value.
///
/// TODO: example omitted
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setPattern", skip_jsdoc)]
pub fn set_pattern(&self, pattern: FormatPattern) -> Format {
impl_method!(self.set_pattern(pattern.into()));
Expand All @@ -605,20 +637,48 @@ impl Format {
/// {@link Worksheet#protect} method.
///
/// TODO: example omitted
///
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setHidden", skip_jsdoc)]
pub fn set_hidden(&self) -> Format {
impl_method!(self.set_hidden());
}

#[wasm_bindgen(js_name = "setLocked")]
/// Set the locked Format property back to its default "on" state.
///
/// The opposite of {Format#setUnlocked}.
///
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setLocked", skip_jsdoc)]
pub fn set_locked(&self) -> Format {
impl_method!(self.set_locked());
}

#[wasm_bindgen(js_name = "setUnlocked")]
/// Set the Format cell unlocked state.
///
/// This method can be used to allow modification of a cell in a protected
/// worksheet. In Excel, cell locking is turned on by default for all cells.
/// However, it only has an effect if the worksheet has been protected using
/// the {@link Worksheet#protect} method.
///
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setUnlocked", skip_jsdoc)]
pub fn set_unlocked(&self) -> Format {
impl_method!(self.set_unlocked());
}

/// Set the `quote_prefix` property for a Format.
///
/// Set the quote prefix property of a format to ensure a string is treated
/// as a string after editing. This is the same as prefixing the string with
/// a single quote in Excel. You don't need to add the quote to the string
/// but you do need to add the format.
///
/// @return {Format} - The Format instance.
#[wasm_bindgen(js_name = "setQuotePrefix", skip_jsdoc)]
pub fn set_quote_prefix(&self) -> Format {
impl_method!(self.set_quote_prefix());
}
}

/// The `FormatAlign` enum defines the vertical and horizontal alignment properties
Expand Down Expand Up @@ -680,10 +740,11 @@ impl From<FormatAlign> for xlsx::FormatAlign {

/// The `FormatBorder` enum defines the Excel border types that can be added to
/// a {@link Format} pattern.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
#[wasm_bindgen]
pub enum FormatBorder {
/// No border.
#[default]
None,
/// Thin border style.
Thin,
Expand Down Expand Up @@ -733,11 +794,40 @@ impl From<FormatBorder> for xlsx::FormatBorder {
}
}
}
/// The `FormatDiagonalBorder` enum defines {@link Format} diagonal border types.
///
/// This is used with the {@link Format#setBorderDiagonal} method.
///
#[wasm_bindgen]
#[derive(Debug, Clone, Copy, Default)]
pub enum FormatDiagonalBorder {
/// The default/automatic format for an Excel font.
#[default]
None,
/// Cell diagonal border from bottom left to top right.
BorderUp,
/// Cell diagonal border from top left to bottom right.
BorderDown,
/// Cell diagonal border in both directions.
BorderUpDown,
}

#[derive(Debug, Clone, Copy)]
impl From<FormatDiagonalBorder> for xlsx::FormatDiagonalBorder {
fn from(border: FormatDiagonalBorder) -> xlsx::FormatDiagonalBorder {
match border {
FormatDiagonalBorder::None => xlsx::FormatDiagonalBorder::None,
FormatDiagonalBorder::BorderUp => xlsx::FormatDiagonalBorder::BorderUp,
FormatDiagonalBorder::BorderDown => xlsx::FormatDiagonalBorder::BorderDown,
FormatDiagonalBorder::BorderUpDown => xlsx::FormatDiagonalBorder::BorderUpDown,
}
}
}

#[derive(Debug, Clone, Copy, Default)]
#[wasm_bindgen]
pub enum FormatPattern {
/// Automatic or Empty pattern.
#[default]
None,
/// Solid pattern.
Solid,
Expand Down Expand Up @@ -810,10 +900,11 @@ impl From<FormatPattern> for xlsx::FormatPattern {
/// accounting underline underlines the entire cell width.
///
/// TODO: example omitted
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Default)]
#[wasm_bindgen]
pub enum FormatUnderline {
/// The default/automatic underline for an Excel font.
#[default]
None,
/// A single underline under the text/number in a cell.
Single,
Expand All @@ -836,3 +927,28 @@ impl From<FormatUnderline> for xlsx::FormatUnderline {
}
}
}

/// The `FormatScript` enum defines the {@link Format} font superscript and subscript
/// properties.
///
#[derive(Clone, Copy, Default)]
#[wasm_bindgen]
pub enum FormatScript {
/// The default/automatic format for an Excel font.
#[default]
None,
/// The cell text is superscripted.
Superscript,
/// The cell text is subscripted.
Subscript,
}

impl From<FormatScript> for xlsx::FormatScript {
fn from(script: FormatScript) -> xlsx::FormatScript {
match script {
FormatScript::None => xlsx::FormatScript::None,
FormatScript::Superscript => xlsx::FormatScript::Superscript,
FormatScript::Subscript => xlsx::FormatScript::Subscript,
}
}
}
Binary file modified test/expected/format_border.xlsx
Binary file not shown.
Binary file added test/expected/format_format_script.xlsx
Binary file not shown.
Binary file added test/expected/format_locked_unlocked.xlsx
Binary file not shown.
50 changes: 49 additions & 1 deletion test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {
Format,
FormatAlign,
FormatBorder,
FormatDiagonalBorder,
FormatScript,
FormatUnderline,
Formula,
Workbook,
} from "../rust/pkg";
import { describe, test, beforeAll, expect } from "vitest";
import { initWasModule, readXlsx, readXlsxFile } from "./common";
import { initWasModule, readXlsx, readXlsxFile, saveFile } from "./common";

beforeAll(async () => {
await initWasModule();
Expand Down Expand Up @@ -102,6 +104,11 @@ describe("xlsx-wasm test", () => {
.setBorderDiagonalColor(Color.rgb(0xff0000));
worksheet.writeStringWithFormat(0, 3, "DDD", format4);

const format5 = new Format()
.setBorderDiagonal(FormatBorder.Thick)
.setBorderDiagonalType(FormatDiagonalBorder.BorderUp);
worksheet.writeStringWithFormat(0, 4, "EEE", format5);

// Assert
const actual = await readXlsx(workbook.saveToBufferSync());
const expected = await readXlsxFile("./expected/format_border.xlsx");
Expand Down Expand Up @@ -226,3 +233,44 @@ describe("xlsx-wasm test", () => {
expect(actual).matchXlsx(expected);
});
});

describe("xlsx-wasm test", () => {
test("format script", async () => {
// Arrange
const workbook = new Workbook();

// Act
const worksheet = workbook.addWorksheet();
const superscript = new Format().setFormatScript(FormatScript.Superscript);
const subscript = new Format().setFormatScript(FormatScript.Subscript);

worksheet.writeStringWithFormat(0, 0, "superscript", superscript);
worksheet.writeStringWithFormat(0, 1, "subscript", subscript);

// Assert
const actual = await readXlsx(workbook.saveToBufferSync());
const expected = await readXlsxFile("./expected/format_format_script.xlsx");
expect(actual).matchXlsx(expected);
});
});

describe("xlsx-wasm test", () => {
test("format locked and unlocked", async () => {
// Arrange
const workbook = new Workbook();

// Act
const worksheet = workbook.addWorksheet();
const locked = new Format().setLocked();
const unlocked = new Format().setUnlocked();

worksheet.protect();
worksheet.writeStringWithFormat(0, 0, "locked", locked);
worksheet.writeStringWithFormat(0, 1, "unlocked", unlocked);

// Assert
const actual = await readXlsx(workbook.saveToBufferSync());
const expected = await readXlsxFile("./expected/format_locked_unlocked.xlsx");
expect(actual).matchXlsx(expected);
});
});