Protect and Unprotect Worksheet with Node.js via C++

Protect and unprotect Worksheet in MS Excel

protect and unprotect Worksheet

  1. Click Review > Protect Worksheet.
  2. Enter a password in the Password box.
  3. Select allow options.
  4. Select OK, re-enter the password to confirm it, and then select OK again.

Protect Worksheet Using Aspose.Cells for Node.js via C++

Only need the following simple lines of code to implement protecting workbook structure of Excel files.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create a new file.
const workbook = new AsposeCells.Workbook();
// Gets the first worksheet.
const sheet = workbook.getWorksheets().get(0);
// Protect contents of the worksheet.
sheet.protect(AsposeCells.ProtectionType.Contents);
// Protect worksheet with password.
sheet.getProtection().setPassword("test");
// Save as Excel file.
workbook.save("Book1.xlsx");

Unprotect Worksheet Using Aspose.Cells for Node.js via C++

Unprotecting the worksheet is easy with Aspose.Cells API. If the worksheet is password-protected, a correct password is required.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xlsx");
// Create a new file.
const workbook = new AsposeCells.Workbook(filePath);
// Gets the first worksheet.
const sheet = workbook.getWorksheets().get(0);
// Protect contents of the worksheet.
sheet.unprotect("password");
// Save Excel file.
workbook.save("Book1.xlsx");

Advance topics