【发布时间】:2016-08-09 09:39:50
【问题描述】:
我有一个关于 Excel 工作表保护的问题...
上下文是我需要为不同的用户组提供不同的工作表来编辑,但所有组必须至少看到所有工作表,例如usergroup1 可以编辑工作表二和三以及工作表一的部分内容,usergroup2 只能编辑工作表一。
我可以相应地设置 FormatProtection (range.format.protection.locked = false;) 和 WorksheetProtection (worksheet.protection.protect();) 以启用此功能,但我似乎无法通过 API 设置密码以防止 Worksheet Protection?这意味着,例如,任一组都可以简单地单击审阅功能区中的取消保护工作表选项并编辑我不希望他们这样做的工作表。
我已尝试浏览以下文档,但不幸的是无济于事。
- http://dev.office.com/reference/add-ins/excel/worksheetprotection
- https://github.com/OfficeDev/office-js-docs/blob/master/reference/excel/worksheetprotection.md
举个例子,我想完成一个函数:
function CopyWorksheet() {
var newAddress;
Excel.run(function (ctx) {
var worksheet = ctx.workbook.worksheets.getActiveWorksheet();
var range = worksheet.getUsedRange();
range.load();
// insert new worksheet
var newWorksheetName = "Copied_Sheet";
var newWorksheet = ctx.workbook.worksheets.add(newWorksheetName);
return ctx.sync().then(function () {
// copy the old values to the new worksheet
newAddress = range.address.substring(range.address.indexOf("!") + 1);
newWorksheet.getRange(newAddress).values = range.values;
newWorksheet.getRange(newAddress).formulas = range.formulas;
newWorksheet.getRange(newAddress).text = range.text;
// protect both worksheets
worksheet.protection.protect();
newWorksheet.protection.protect();
// requirement here to set a password so that no one can
// edit the worksheets by selecting 'Unprotect Sheet' in excel
// ...
})
.then(ctx.sync)})
.catch(function(error) {
console.log("Error: " + error);
});
}
目前,我使用的是 Excel 2016(桌面版)。这可以实现还是我错过了一些可以实现相同结果的现有功能?
感谢您的帮助。
【问题讨论】:
-
到目前为止你尝试过什么?请显示更多您的代码。关于实际编程的这么少的细节很难提供帮助。
-
@Tom 感谢您的快速回复。我在帖子中添加了一些代码以获取更多上下文。
标签: excel ms-office office-addins excel-addins office-js