Password does not apply when I protect the sheet

I am using the Excel API to protect an Excel worksheet. I tried to do as they mentioned in the documentation, but the Sheet is not password protected. It is simply protected without a password.

The code I'm trying to do is the following:

Excel.run(function (ctx) {
    var sheet = ctx.workbook.worksheets.getItem("Sheet1");
    var range = sheet.getRange("A1:B3").format.protection.locked = false;
    sheet.protection.protect({
        allowInsertRows: true
    }, "mypassword");
    return ctx.sync();
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

What is wrong with the password not being applied?

+4
source share
1 answer

You are actually doing the opposite of what you want here. When you install allowInsertRows: true, you unprotect the line insert. Since you are not protecting anything, the password you provided is simply ignored.

allowInsertRows: false, . , :

Excel.run(function (ctx) {
    var sheet = ctx.workbook.worksheets.getItem("Sheet1");
    sheet.protection.protect({
        allowInsertRows: false
    }, "mypassword");
    return ctx.sync();
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

, . locked :

var range = sheet.getRange("A1:B3").format.protection.locked = false;

: , ( , ):

Excel.run(function (ctx) {
    var sheet = ctx.workbook.worksheets.getItem("Sheet1");
    var range = sheet.getRange("A1:B3").format.protection.locked = false;
    sheet.protection.protect({
        allowInsertRows: true
    });
    return ctx.sync();
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

. , . , .

0

Source: https://habr.com/ru/post/1695568/


All Articles