libxlsxwriter
Loading...
Searching...
No Matches
worksheet_protection.c
<< doc_custom_properties.c macro.c >>

Example of setting Excel worksheet protection.

/*
* Example of cell locking and formula hiding in an Excel worksheet using
* libxlsxwriter.
*
* Copyright 2014-2024, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("protection.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_format *unlocked = workbook_add_format(workbook);
lxw_format *hidden = workbook_add_format(workbook);
/* Widen the first column to make the text clearer. */
worksheet_set_column(worksheet, 0, 0, 40, NULL);
/* Turn worksheet protection on without a password. */
worksheet_protect(worksheet, NULL, NULL);
/* Write a locked, unlocked and hidden cell. */
worksheet_write_string(worksheet, 0, 0, "B1 is locked. It cannot be edited.", NULL);
worksheet_write_string(worksheet, 1, 0, "B2 is unlocked. It can be edited.", NULL);
worksheet_write_string(worksheet, 2, 0, "B3 is hidden. The formula isn't visible.", NULL);
worksheet_write_formula(worksheet, 0, 1, "=1+2", NULL); /* Locked by default. */
worksheet_write_formula(worksheet, 1, 1, "=1+2", unlocked);
worksheet_write_formula(worksheet, 2, 1, "=1+2", hidden);
workbook_close(workbook);
return 0;
}
void format_set_hidden(lxw_format *format)
Hide formulas in a cell.
void format_set_unlocked(lxw_format *format)
Set the cell unlocked state.
Struct to represent the formatting properties of an Excel format.
Definition: format.h:359
Struct to represent an Excel workbook.
Definition: workbook.h:293
Struct to represent an Excel worksheet.
Definition: worksheet.h:2115
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
void worksheet_protect(lxw_worksheet *worksheet, const char *password, lxw_protection *options)
Protect elements of a worksheet from modification.
lxw_error worksheet_set_column(lxw_worksheet *worksheet, lxw_col_t first_col, lxw_col_t last_col, double width, lxw_format *format)
Set the properties for one or more columns of cells.
lxw_error worksheet_write_string(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *string, lxw_format *format)
Write a string to a worksheet cell.
lxw_error worksheet_write_formula(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *formula, lxw_format *format)
Write a formula to a worksheet cell.