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
62 changes: 62 additions & 0 deletions rust/src/wrapper/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,68 @@ impl Worksheet {
Ok(self.clone())
}

/// Freeze panes in a worksheet.
///
/// The `set_freeze_panes()` method can be used to divide a worksheet into
/// horizontal or vertical regions known as panes and to “freeze” these
/// panes so that the splitter bars are not visible.
///
/// As with Excel the split is to the top and left of the cell. So to freeze
/// the top row and leftmost column you would use `(1, 1)` (zero-indexed).
/// Also, you can set one of the row and col parameters as 0 if you do not
/// want either the vertical or horizontal split. See the example below.
///
/// In Excel it is also possible to set "split" panes without freezing them.
/// That feature isn't currently supported by `rust_xlsxwriter`.
///
/// # Parameters
///
/// - `row`: The zero indexed row number.
/// - `col`: The zero indexed column number.
///
/// # Errors
///
/// - [`XlsxError::RowColumnLimitError`] - Row or column exceeds Excel's
/// worksheet limits.
///
#[wasm_bindgen(js_name = "setFreezePanes", skip_jsdoc)]
pub fn set_freeze_panes(&self, row: xlsx::RowNum, col: xlsx::ColNum) -> WasmResult<Worksheet> {
let mut book = self.workbook.lock().unwrap();
let sheet = book.worksheet_from_index(self.index).unwrap();
let _ = map_xlsx_error(sheet.set_freeze_panes(row, col))?;
Ok(self.clone())
}

/// Set the top most cell in the scrolling area of a freeze pane.
///
/// This method is used in conjunction with the
/// [`Worksheet::set_freeze_panes()`] method to set the top most visible
/// cell in the scrolling range. For example you may want to freeze the top
/// row but have the worksheet pre-scrolled so that cell `A20` is visible in
/// the scrolled area. See the example below.
///
/// # Parameters
///
/// - `row`: The zero indexed row number.
/// - `col`: The zero indexed column number.
///
/// # Errors
///
/// - [`XlsxError::RowColumnLimitError`] - Row or column exceeds Excel's
/// worksheet limits.
///
#[wasm_bindgen(js_name = "setFreezePanesTopCell", skip_jsdoc)]
pub fn set_freeze_panes_top_cell(
&self,
row: xlsx::RowNum,
col: xlsx::ColNum,
) -> WasmResult<Worksheet> {
let mut book = self.workbook.lock().unwrap();
let sheet = book.worksheet_from_index(self.index).unwrap();
let _ = map_xlsx_error(sheet.set_freeze_panes_top_cell(row, col))?;
Ok(self.clone())
}

/// Make a worksheet the active/initially visible worksheet in a workbook.
///
/// The `set_active()` method is used to specify which worksheet is
Expand Down
Binary file added test/expected/write_freeze_panes.xlsx
Binary file not shown.
20 changes: 20 additions & 0 deletions test/write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,23 @@ describe("xlsx-wasm test", () => {
expect(actual).matchXlsx(expected);
});
});


describe("xlsx-wasm test", () => {
test("freeze header", async () => {
// Arrange
const workbook = new Workbook();
const worksheet = workbook.addWorksheet();
worksheet.write(0, 0, "Hello");
worksheet.write(1, 0, "world");
worksheet.write(2, 0, "Should be at top after hello row when opening");
worksheet.write(3, 0, "Another row");
worksheet.setFreezePanes(1, 0);
worksheet.setFreezePanesTopCell(2, 0);

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