Skip to main content

开关文档保护

场景说明

文档保护功能可以限制文档的编辑权限,支持多种保护模式,包括表单录入、修订保护、区域编辑和批注模式。

适用场景:

  • 合同文档保护
  • 表单模板限制编辑
  • 启用文档修订模式
  • 协作文档权限控制

相关 API 文档

示例代码

开启区域编辑保护(完整参数示例)

async function enableEditAreaProtection() {
// 开启区域编辑保护,全文默认只读
// 参数:protectionType - 保护类型(必填), permission - 默认权限(可选,默认为只读)
const success = await Application.ActiveDocument.protect(
Word.ProtectionType.EditArea,
Word.DocPermission.ReadOnly // 明确指定全文默认只读
);

if (success) {
console.log('区域编辑保护已开启,全文只读');
// 此时只有设置了可编辑区域的部分可以编辑
}
}

开启区域编辑保护(全文可编辑)

async function enableEditAreaProtectionEditable() {
// 开启区域编辑保护,全文默认可编辑
const success = await Application.ActiveDocument.protect(
Word.ProtectionType.EditArea,
Word.DocPermission.Editable
);

if (success) {
console.log('区域编辑保护已开启,全文可编辑');
// 可以通过设置可编辑区域来限制特定用户的编辑权限
}
}

开启修订保护

async function enableRevisionProtection() {
// 开启修订保护,所有编辑都会记录修订
const success = await Application.ActiveDocument.protect(
Word.ProtectionType.Revision
);

if (success) {
console.log('修订保护已开启,所有修改都会被追踪');
}
}

同时开启区域编辑和修订保护

async function enableMultipleProtections() {
// 使用 | 操作符同时开启多种保护
// 注意:修订保护可以和其他保护类型同时设置
const success = await Application.ActiveDocument.protect(
Word.ProtectionType.EditArea | Word.ProtectionType.Revision,
Word.DocPermission.ReadOnly
);

if (success) {
console.log('已开启区域编辑保护和修订保护');
}
}

关闭文档保护

async function disableProtection() {
// 关闭所有文档保护
const success = await Application.ActiveDocument.unprotect();

if (success) {
console.log('文档保护已关闭');
}
}

检查文档保护状态

async function checkProtectionStatus() {
// 获取文档保护类型
const protectionType = await Application.ActiveDocument.ProtectionType;

console.log('当前保护类型:', protectionType);

// 判断具体的保护类型
const types = Word.ProtectionType;

if (protectionType === types.NoProtection) {
console.log('文档未设置保护');
} else if (protectionType === types.EditArea) {
console.log('已开启区域编辑保护');
}

if (protectionType === types.Revision) {
console.log('已开启修订保护');
}

if (protectionType === types.FormEntry) {
console.log('已开启表单录入保护');
}

if (protectionType === types.Comments) {
console.log('已开启批注模式');
}
}

检查区域编辑的默认权限

async function checkDocPermission() {
// 获取区域编辑保护时的默认权限
const permission = await Application.ActiveDocument.PermissionType;

if (permission === Word.DocPermission.ReadOnly) {
console.log('全文默认只读');
} else if (permission === Word.DocPermission.Editable) {
console.log('全文默认可编辑');
} else if (permission === Word.DocPermission.None) {
console.log('文档未开启区域编辑保护');
}
}

检查内容保护是否开启

async function checkDataProtection() {
// 检查内容保护(数据保护)是否开启
const isProtected = await Application.ActiveDocument.isDataProtectionOn();

if (isProtected) {
console.log('内容保护已开启');
} else {
console.log('内容保护未开启');
}
}

完整的保护切换流程

async function toggleDocumentProtection() {
// 检查当前保护状态
const currentType = await Application.ActiveDocument.ProtectionType;

if (currentType === Word.ProtectionType.NoProtection) {
// 如果未保护,则开启保护
await Application.ActiveDocument.protect(
Word.ProtectionType.EditArea,
Word.DocPermission.ReadOnly
);
console.log('文档保护已开启');
} else {
// 如果已保护,则关闭保护
await Application.ActiveDocument.unprotect();
console.log('文档保护已关闭');
}
}

注意事项

  • 只有修订保护类型可以和其他保护类型同时设置,其他保护类型只能单独设置
  • 使用 | 运算符组合保护类型
  • 区域编辑保护需要配合需配合编辑区域使用
  • DocPermission 只在区域编辑保护时有效
  • 保护状态可以通过 ProtectionType 属性查询
  • isDataProtectionOn() 检查内容保护是否开启