I need help in locking cells using excel js modules. I am using node js. I have created some dummy rows and added a formula. All I need is the user can only edit numerator and denominator columns. When I pass in getCell(C1
).protection. All it does the header becomes editable. If I change it to C2. It inserts an empty row. Not sure what to do.
I have looped over the rows array and tried locking cell + index + 1.
Please guide me..
Thanks Meet
const Excel = require('exceljs/modern.nodejs');
const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet('ExampleSheet');
worksheet.columns = [
{ header: 'Package', key: 'package_name' },
{ header: 'Author', key: 'author_name' },
{ header: 'N', key: 'num'},
{ header: 'D', key: 'dino'},
{ header: 'Computed', key: 'computed'}
];
const rows = [
{ package_name: "FGH", author_name: "Author 4", num: 11, dino: 8, computed:{ formula: 'C2+D2' }},
{ package_name: "PQR", author_name: "Author 5", num: 10, dino: 4, computed:{ formula: 'C3+D3' }}
];
rows.forEach((el, i) => {
console.log(`C${i+1}`)
worksheet.getCell(`C${i+1}`).protection = {
locked: false
};
worksheet.getCell(`D${i+1}`).protection = {
locked: false
}
});
const func = async() => {
const res = await worksheet.protect('password', {selectUnlockedCells: true});
console.log(res);
}
func();
worksheet
.addRows(rows);
workbook
.xlsx
.writeFile('sample.xlsx')
.then(() => {
console.log("saved");
})
.catch((err) => {
console.log("err", err);
});