Skip to content
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

Fix issue #1261 WorkbookWriter sheet.protect() function doesn't exist #1262

Merged
merged 9 commits into from
Sep 28, 2020
37 changes: 37 additions & 0 deletions lib/stream/xlsx/worksheet-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const _ = require('../../utils/under-dash');
const RelType = require('../../xlsx/rel-type');

const colCache = require('../../utils/col-cache');
const Encryptor = require('../../utils/encryptor');
const Dimensions = require('../../doc/range');
const StringBuf = require('../../utils/string-buf');

Expand Down Expand Up @@ -177,6 +178,9 @@ class WorksheetWriter {

this._media = [];

// worksheet protection
this.sheetProtection = null;

// start writing to stream now
this._writeOpenWorksheet();

Expand Down Expand Up @@ -473,6 +477,39 @@ class WorksheetWriter {
return this._background && this._background.imageId;
}

// =========================================================================
// Worksheet Protection
protect(password, options) {
// TODO: make this function truly async
// perhaps marshal to worker thread or something
return new Promise(resolve => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether this attribute should be declared in constructor ().

    // worksheet protection
    this.sheetProtection = null;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been added to the constructor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really work ? I set sheet protection to true it doesn't lock a particular cell

this.sheetProtection = {
sheet: true,
};
if (options && 'spinCount' in options) {
// force spinCount to be integer >= 0
options.spinCount = isFinite(options.spinCount) ? Math.round(Math.max(0, options.spinCount)) : 100000;
}
if (password) {
this.sheetProtection.algorithmName = 'SHA-512';
this.sheetProtection.saltValue = Encryptor.randomBytes(16).toString('base64');
this.sheetProtection.spinCount = options && 'spinCount' in options ? options.spinCount : 100000; // allow user specified spinCount
this.sheetProtection.hashValue = Encryptor.convertPasswordToHash(password, 'SHA512', this.sheetProtection.saltValue, this.sheetProtection.spinCount);
}
if (options) {
this.sheetProtection = Object.assign(this.sheetProtection, options);
if (!password && 'spinCount' in options) {
delete this.sheetProtection.spinCount;
}
}
resolve();
});
}

unprotect() {
this.sheetProtection = null;
}

// ================================================================================

_write(text) {
Expand Down
30 changes: 30 additions & 0 deletions spec/integration/pr/test-pr-1262.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const ExcelJS = verquire('exceljs');

describe('github issues', () => {
it('pull request 1262 - protect should work with streaming workbook writer', async () => {
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
filename: './test.xlsx',
});

const sheet = workbook.addWorksheet('data');
const row = sheet.addRow(['readonly cell']);
row.getCell(1).protection = {
locked: true,
};

expect(sheet.protect).to.exist();

sheet.protect('password', {
spinCount: 1,
});

await workbook.commit();

// read in file and ensure sheetProtection is there:
const checkBook = new ExcelJS.Workbook();
await checkBook.xlsx.readFile('./test.xlsx');

const checkSheet = checkBook.getWorksheet('data');
expect(checkSheet.sheetProtection.spinCount).to.equal(1);
});
});