Skip to content

How to make the cell locked? Please give me a example!! #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
txtxtxtxtxtxtx opened this issue Dec 19, 2024 · 5 comments
Closed

How to make the cell locked? Please give me a example!! #311

txtxtxtxtxtxtx opened this issue Dec 19, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request resolved This issue has been resolved.

Comments

@txtxtxtxtxtxtx
Copy link

Now I use OpenXLSX to write some cells' value, and then I want to set these cells' cellFormat to make their value be protected. So when user open the Excel, he can't change the value of the locked cells. Can OpenXLSX do that?

@aral-matrix
Copy link
Collaborator

I just did some tests, and apparently

  1. OpenXLSX currently does not support the worksheet's sheetProtection property, which is used to flag that the whole worksheet is protected
  2. it appears that individual cells can not be protected, but you can set the whole worksheet to protected (until this feature is implemented, you'll have to do that afterwards, directly in MS Office), and then set cells to be NOT protected when you want them to be edited.

By default, all cells in a worksheet are protected unless specifically "un-protected":

    XLDocument doc;
    doc.create("./Demo01.xlsx", XLForceOverwrite);
    auto wks = doc.workbook().worksheet("Sheet1");

    wks.cell("A1").value() = 41;
    wks.cell("B1").value() = 42;
    wks.cell("C1").value() = 43;
    XLCellFormats & cellFormats = doc.styles().cellFormats();
    XLStyleIndex newCellFormatIndex = cellFormats.create( cellFormats[ wks.cell("B1").cellFormat() ] );
    wks.cell("B1").setCellFormat(newCellFormatIndex);
    // cellFormats[ newCellFormatIndex ].setApplyProtection( true ); // seems to be irrelevant
    cellFormats[ newCellFormatIndex ].setLocked( true );  // locked seems to be default behavior if attribute is not set
    cellFormats[ newCellFormatIndex ].setLocked( false ); // unprotect a cell
    doc.save();
    doc.close();

With the above example, if you open the worksheet and "protect sheet" in MS Office, you should be able to overwrite the cell B2, but no other cell.

@aral-matrix aral-matrix self-assigned this Dec 19, 2024
@aral-matrix aral-matrix added the enhancement New feature or request label Dec 19, 2024
@aral-matrix
Copy link
Collaborator

I made a mental note to implement the general worksheet protection setting.

@txtxtxtxtxtxtx
Copy link
Author

Thank you for your reply!

@aral-matrix aral-matrix added testing Functionality has been implemented in development branch and is pending a merge into main ready to close Pull request has been answered or implemented & is pending closure labels Jan 9, 2025
@aral-matrix
Copy link
Collaborator

Hi @txtxtxtxtxtxtx - I implemented the functionality today in 9a7a678 in the development-aral branch. Feel free to test it and let me know here if you have any feedback.

@aral-matrix aral-matrix added resolved This issue has been resolved. and removed testing Functionality has been implemented in development branch and is pending a merge into main ready to close Pull request has been answered or implemented & is pending closure labels Jan 16, 2025
@aral-matrix
Copy link
Collaborator

merged the patch into master today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resolved This issue has been resolved.
Projects
None yet
Development

No branches or pull requests

2 participants