我在使用 Excel Javascript API 将允许的编辑范围添加到工作表保护对象时遇到问题。我不断收到错误无法读取未定义的属性(读取“添加”)。我相信我已经添加了带有声明的属性
worksheet.load("protection/protected", "protection/allowEditRanges");
但也许这是错误的?
我在这里参考了API参考https://learn.microsoft.com/en-us/javascript/api/excel/excel.alloweditrangecollection?view=excel-js-preview
async function protect(worksheetName) {
await Excel.run(async (context) => {
worksheet = context.workbook.worksheets.getItem(worksheetName);
worksheet.load("protection/protected", "protection/allowEditRanges");
await context.sync();
//can't add without pausing protection
worksheet.protection.unprotect("");
var wholerange = worksheet.getRange();
wholerange.format.protection.locked = true;
worksheet.protection.allowEditRange.add({title: "Range1", rangeAddress: "A4:G500"});
worksheet.protection.allowEditRange.add({title: "Range2", rangeAddress: "I4::L500"});
worksheet.protection.protect({
allowFormatCells: true,
allowAutoFilter: true,
allowDeleteRows: true,
allowEditObjects: true,
//allowFormatColumns: true,
allowFormatRows: true,
allowInsertHyperlinks: true,
allowInsertRows: true,
allowPivotTables: true,
allowSort: true
}, "");
await context.sync();
});
}