Skip to content

Setting a cell or row Protection Locked to true does not prevent editing in xlsx #969

@ahouck

Description

@ahouck

When setting a column or row level Protection lock, the resulting .xlsx file still has those columns/cells editable. It is possible to lock the entire worksheet, but not individual cells.

import * as Excel from 'exceljs';
import * as fs from 'fs';  

  const stream = fs.createWriteStream(
    `${FilePath}`,
    {
      flags: 'a',
    }
  );
const DefaultColumns = [
    'Col1',
    'Col2',
    'Col3',
    'Col4',
    'Col5',
  ];

  const placeHolders = new Array(DefaultColumns.length);
  const workbook = new Excel.Workbook();
  workbook.created = new Date();
  workbook.modified = new Date();

  const sheet = workbook.addWorksheet('sheet 1', {
    //Also not sure what this is supposed to show
    //No difference if datavalidation condition is failed
    pageSetup: { errors: 'dash' },
  });

  const headerRow = sheet.addRow(DefaultColumns);
  headerRow.eachCell((cell, rowNum) => {
    //@ts-ignore  Typing is wrong
    cell.note = `This is ${rowNum}`;
    //Doesn't do anything
    cell.protection = { locked: true };
  });

  //Also doesn't do anything
  headerRow.protection = { locked: true };
  headerRow.commit();

await workbook.xlsx.write(res);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions