Class Protection

الحماية

الوصول إلى النطاقات وأوراق البيانات المحمية وتعديلها يمكن أن يحمي النطاق المحمي نطاقًا ثابتًا من الخلايا أو نطاقًا مُعنوَنًا. قد تتضمّن ورقة محمية مناطق غير محمية. بالنسبة إلى جداول البيانات التي تم إنشاؤها باستخدام الإصدار القديم من "جداول بيانات Google"، استخدِم فئة PageProtection بدلاً من ذلك.

// Protect range A1:B10, then remove all other users from the list of editors.
const ss = SpreadsheetApp.getActive();
const range = ss.getRange('A1:B10');
const protection = range.protect().setDescription('Sample protected range');

// Ensure the current user is an editor before removing others. Otherwise, if
// the user's edit permission comes from a group, the script throws an exception
// upon removing the group.
const me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}
// Remove all range protections in the spreadsheet that the user has permission
// to edit.
const ss = SpreadsheetApp.getActive();
const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (let i = 0; i < protections.length; i++) {
  const protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Protect the active sheet, then remove all other users from the list of
// editors.
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.protect().setDescription('Sample protected sheet');

// Ensure the current user is an editor before removing others. Otherwise, if
// the user's edit permission comes from a group, the script throws an exception
// upon removing the group.
const me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

الطُرق

الطريقةنوع القيمة التي يتم إرجاعهاوصف قصير
addEditor(emailAddress)Protectionتضيف هذه الدالة المستخدم المحدّد إلى قائمة المحرّرين للورقة أو النطاق المحميَّين.
addEditor(user)Protectionتضيف هذه الدالة المستخدم المحدّد إلى قائمة المحرّرين للورقة أو النطاق المحميَّين.
addEditors(emailAddresses)Protectionتضيف هذه الدالة مصفوفة المستخدمين المحدّدة إلى قائمة المحرّرين للورقة أو النطاق المحميَّين.
addTargetAudience(audienceId)Protectionتضيف هذه الدالة الجمهور المستهدف المحدّد كمحرّر للنطاق المحميّ.
canDomainEdit()Booleanتحدّد هذه السمة ما إذا كان جميع المستخدمين في النطاق الذي يملك جدول البيانات لديهم الإذن بتعديل النطاق أو ورقة البيانات المحمية.
canEdit()Booleanتحدِّد هذه السمة ما إذا كان لدى المستخدم إذن بتعديل النطاق أو ورقة البيانات المحمية.
getDescription()Stringتعرض هذه الدالة وصف النطاق أو ورقة البيانات المحمية.
getEditors()User[]تعرض هذه الدالة قائمة بالمحرّرين للنطاق أو ورقة البيانات المحمية.
getProtectionType()ProtectionTypeتعرض هذه السمة نوع المنطقة المحمية، إما RANGE أو SHEET.
getRange()Rangeتعرض هذه الدالة النطاق الذي تتم حمايته.
getRangeName()String|nullتعرض هذه الدالة اسم النطاق المحميّ إذا كان مرتبطًا بنطاق مُعنوَن.
getTargetAudiences()TargetAudience[]تعرض هذه السمة أرقام تعريف شرائح الجمهور المستهدَفة التي يمكنها تعديل النطاق المحمي.
getUnprotectedRanges()Range[]تعرض هذه الدالة مصفوفة من النطاقات غير المحمية ضمن ورقة محمية.
isWarningOnly()Booleanتحدّد هذه السمة ما إذا كانت المنطقة المحمية تستخدم الحماية "القائمة على التحذيرات".
remove()voidتتم إزالة حماية النطاق أو ورقة البيانات.
removeEditor(emailAddress)Protectionتزيل هذه الدالة المستخدم المحدّد من قائمة المحرّرين للورقة أو النطاق المحميَّين.
removeEditor(user)Protectionتزيل هذه الدالة المستخدم المحدّد من قائمة المحرّرين للورقة أو النطاق المحميَّين.
removeEditors(emailAddresses)Protectionتزيل هذه الدالة مصفوفة المستخدمين المحدّدة من قائمة المحرّرين للورقة أو النطاق المحميَّين.
removeTargetAudience(audienceId)Protectionتزيل هذه الدالة الجمهور المستهدَف المحدّد كمحرّر للنطاق المحميّ.
setDescription(description)Protectionتضبط هذه السمة وصف النطاق أو ورقة البيانات المحمية.
setDomainEdit(editable)Protectionتضبط هذه السمة ما إذا كان جميع المستخدمين في النطاق الذي يملك جدول البيانات لديهم إذن بتعديل النطاق أو الورقة المحمية.
setNamedRange(namedRange)Protectionيربط النطاق المحمي بنطاق مسمّى حالي.
setRange(range)Protectionتعديل النطاق الذي تتم حمايته
setRangeName(rangeName)Protectionيربط النطاق المحمي بنطاق مسمّى حالي.
setUnprotectedRanges(ranges)Protectionتزيل هذه الدالة الحماية عن مجموعة النطاقات المحدّدة في ورقة محمية.
setWarningOnly(warningOnly)Protectionتضبط هذه السمة ما إذا كان النطاق المحمي يستخدم الحماية "القائمة على التحذير" أم لا.

مستندات تفصيلية

addEditor(emailAddress)

تضيف هذه الدالة المستخدم المحدّد إلى قائمة المحرّرين للورقة أو النطاق المحميَّين. لا يمنح هذا الإجراء المستخدم تلقائيًا إذنًا بتعديل جدول البيانات نفسه. ولإجراء ذلك، عليك استدعاء Spreadsheet.addEditor(emailAddress) أيضًا.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Adds an editor to the spreadsheet using an email address.
// TODO(developer): Replace the email address with a valid email.
ss.addEditor('cloudysanfrancisco@gmail.com');

// Gets a sheet by its name and protects it.
const sheet = ss.getSheetByName('Sheet1');
const sampleProtectedSheet = sheet.protect();

// Adds an editor of the protected sheet using an email address.
// TODO(developer): Replace the email address with a valid email.
sampleProtectedSheet.addEditor('cloudysanfrancisco@gmail.com');

// Gets the editors of the protected sheet.
const editors = sampleProtectedSheet.getEditors();

// Logs the editors' email addresses to the console.
for (const editor of editors) {
  console.log(editor.getEmail());
}

المعلمات

الاسمالنوعالوصف
emailAddressStringعنوان البريد الإلكتروني للمستخدم الذي تريد إضافته

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addEditor(user)

تضيف هذه الدالة المستخدم المحدّد إلى قائمة المحرّرين للورقة أو النطاق المحميَّين. لا يمنح هذا الإجراء المستخدم تلقائيًا إذنًا بتعديل جدول البيانات نفسه. ولإجراء ذلك، عليك استدعاء Spreadsheet.addEditor(user) أيضًا.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Adds the active user as an editor of the protected sheet.
sampleProtectedSheet.addEditor(Session.getActiveUser());

// Gets the editors of the protected sheet.
const editors = sampleProtectedSheet.getEditors();

// Logs the editors' email addresses to the console.
for (const editor of editors) {
  console.log(editor.getEmail());
}

المعلمات

الاسمالنوعالوصف
userUserتمثيل للمستخدم الذي تريد إضافته

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addEditors(emailAddresses)

تضيف هذه الدالة مصفوفة المستخدمين المحدّدة إلى قائمة المحرّرين للورقة أو النطاق المحميَّين. لا تمنح هذه الطريقة المستخدمين تلقائيًا إذن تعديل جدول البيانات نفسه، ولإجراء ذلك، عليك أيضًا استدعاء Spreadsheet.addEditors(emailAddresses).

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Creates variables for the email addresses to add as editors.
// TODO(developer): Replace the email addresses with valid ones.
const TEST_EMAIL_1 = 'cloudysanfrancisco@gmail.com';
const TEST_EMAIL_2 = 'baklavainthebalkans@gmail.com';

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Adds editors to the protected sheet using the email address variables.
sampleProtectedSheet.addEditors([TEST_EMAIL_1, TEST_EMAIL_2]);

// Gets the editors of the protected sheet.
const editors = sampleProtectedSheet.getEditors();

// Logs the editors' email addresses to the console.
for (const editor of editors) {
  console.log(editor.getEmail());
}

المعلمات

الاسمالنوعالوصف
emailAddressesString[]مصفوفة من عناوين البريد الإلكتروني للمستخدمين الذين تريد إضافتهم

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addTargetAudience(audienceId)

تضيف هذه الدالة الجمهور المستهدف المحدّد كمحرّر للنطاق المحميّ.

المعلمات

الاسمالنوعالوصف
audienceIdStringمعرّف الجمهور المستهدَف الذي تريد إضافته.

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

canDomainEdit()

تحدّد هذه السمة ما إذا كان جميع المستخدمين في النطاق الذي يملك جدول البيانات لديهم الإذن بتعديل النطاق أو ورقة البيانات المحمية. يُنشئ هذا الإجراء استثناءً إذا لم يكن لدى المستخدم إذن بتعديل النطاق المحمي أو ورقة البيانات.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Logs whether domain users have permission to edit the protected sheet to the
// console.
console.log(sampleProtectedSheet.canDomainEdit());

الإرجاع

Booleantrue إذا كان جميع المستخدمين في النطاق الذي يملك جدول البيانات لديهم الإذن بتعديل النطاق أو الورقة المحمية، false إذا لم يكن لديهم هذا الإذن.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

canEdit()

تحدِّد هذه السمة ما إذا كان لدى المستخدم إذن بتعديل النطاق أو ورقة البيانات المحمية. يمكن لمالك جدول البيانات دائمًا تعديل النطاقات والأوراق المحمية.

// Remove all range protections in the spreadsheet that the user has permission
// to edit.
const ss = SpreadsheetApp.getActive();
const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (let i = 0; i < protections.length; i++) {
  const protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}

الإرجاع

Booleantrue إذا كان لدى المستخدم إذن بتعديل النطاق أو الورقة المحمية، false إذا لم يكن لديه إذن

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getDescription()

تعرض هذه الدالة وصف النطاق أو ورقة البيانات المحمية. إذا لم يتم ضبط أي وصف، تعرض هذه الطريقة سلسلة فارغة.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet and sets the description.
const sampleProtectedSheet =
    sheet.protect().setDescription('Sample sheet is protected');

// Gets the description of the protected sheet and logs it to the console.
const sampleProtectedSheetDescription = sampleProtectedSheet.getDescription();
console.log(sampleProtectedSheetDescription);

الإرجاع

String: وصف النطاق أو الورقة المحمية، أو سلسلة فارغة إذا لم يتم ضبط أي وصف.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getEditors()

تعرض هذه الدالة قائمة بالمحرّرين للنطاق أو الورقة المحمية. يتم عرض استثناء إذا لم يكن لدى المستخدم إذن بتعديل النطاق المحمي أو ورقة البيانات.

// Protect the active sheet, then remove all other users from the list of
// editors.
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.protect().setDescription('Sample protected sheet');

// Ensure the current user is an editor before removing others. Otherwise, if
// the user's edit permission comes from a group, the script throws an exception
// upon removing the group.
const me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

الإرجاع

User[]: مصفوفة من المستخدمين الذين لديهم إذن بتعديل النطاق أو ورقة البيانات المحمية

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getProtectionType()

تعرض هذه السمة نوع المنطقة المحمية، إما RANGE أو SHEET.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Gets the type of the protected area.
const protectionType = sampleProtectedSheet.getProtectionType();

// Logs 'SHEET'to the console since the type of the protected area is a sheet.
console.log(protectionType.toString());

الإرجاع

ProtectionType: نوع المنطقة المحمية، إما RANGE أو SHEET

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRange()

تعرض هذه الدالة النطاق الذي تتم حمايته. إذا كانت الحماية تنطبق على ورقة البيانات بدلاً من نطاق، تعرض هذه الطريقة نطاقًا يمتد على ورقة البيانات بأكملها.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the range 'A1:B10' of Sheet1.
const range = sheet.getRange('A1:B10');

// Makes cells A1:B10 a protected range.
const sampleProtectedRange = range.protect();

// Gets the protected ranges on the sheet.
const protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);

// Logs the A1 notation of the first protected range on the sheet.
console.log(protections[0].getRange().getA1Notation());

الإرجاع

Range: النطاق الذي تتم حمايته.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRangeName()

تعرض هذه الدالة اسم النطاق المحميّ إذا كان مرتبطًا بنطاق مُعنوَن. تعرِض الدالة القيمة null إذا لم تكن الحماية مرتبطة بنطاق مسمّى. يُرجى العِلم أنّه يجب أن تستدعي البرامج النصية بشكل صريح الدالة setRangeName(rangeName) لربط نطاق محمي بنطاق مسمّى، ولا يكفي استدعاء الدالة Range.protect() لإنشاء حماية من Range يكون نطاقًا مسمّى، بدون استدعاء الدالة setRangeName(rangeName)، لربط النطاقين. ومع ذلك، يؤدي إنشاء نطاق محمي من نطاق مسمّى في واجهة مستخدم &quot;جداول بيانات Google&quot; إلى ربطهما تلقائيًا.

// Protect a named range in a spreadsheet and log the name of the protected
// range.
const ss = SpreadsheetApp.getActive();
const range = ss.getRange('A1:B10');
const protection = range.protect();
ss.setNamedRange('Test', range);  // Create a named range.
protection.setRangeName(
    'Test');  // Associate the protection with the named range.
Logger.log(
    protection.getRangeName());  // Verify the name of the protected range.

الإرجاع

String|null: اسم النطاق المسمّى المحمي، أو null إذا لم يكن النطاق المحمي مرتبطًا بنطاق مسمّى

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getTargetAudiences()

تعرِض هذه الدالة أرقام تعريف شرائح الجمهور المستهدَفة التي يمكنها تعديل النطاق المحمي.

الإرجاع

TargetAudience[]: مصفوفة تتضمّن أرقام تعريف الجماهير المستهدَفة.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getUnprotectedRanges()

تعرض هذه الدالة مصفوفة من النطاقات غير المحمية ضمن ورقة محمية. إذا كان العنصر Protection يتوافق مع نطاق محمي بدلاً من ورقة محمية، يعرض هذا الإجراء مصفوفة فارغة. لتغيير النطاقات غير المحمية، استخدِم setUnprotectedRanges(ranges) لضبط مصفوفة جديدة من النطاقات، ولإعادة حماية جدول البيانات بأكمله، اضبط مصفوفة فارغة.

// Unprotect cells E2:F5 in addition to any other unprotected ranges in the
// protected sheet.
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.protect();
const unprotected = protection.getUnprotectedRanges();
unprotected.push(sheet.getRange('E2:F5'));
protection.setUnprotectedRanges(unprotected);

الإرجاع

Range[]: مصفوفة من النطاقات غير المحمية ضمن ورقة محمية

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

isWarningOnly()

تحدّد هذه السمة ما إذا كانت المنطقة المحمية تستخدم الحماية "القائمة على التحذيرات". تعني الحماية المستندة إلى التحذيرات أنّه يمكن لكل مستخدم تعديل البيانات في المنطقة، ولكن سيظهر تحذير يطلب من المستخدم تأكيد التعديل. بشكلٍ تلقائي، لا تستند النطاقات أو الأوراق المحمية إلى التحذيرات. للتغيير إلى حالة التحذير، استخدِم setWarningOnly(warningOnly).

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Sets the warning status for the protected sheet as true.
sampleProtectedSheet.setWarningOnly(true);

const protectedSheetWarningStatus = sampleProtectedSheet.isWarningOnly();

// Logs the warning status of the protected sheet to the console.
console.log(protectedSheetWarningStatus);

الإرجاع

Booleantrue إذا كان النطاق أو ورقة البيانات المحمية يستخدمان الحماية المستندة إلى التحذيرات فقط

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

remove()

تتم إزالة حماية النطاق أو ورقة البيانات.

// Remove all range protections in the spreadsheet that the user has permission
// to edit.
const ss = SpreadsheetApp.getActive();
const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (let i = 0; i < protections.length; i++) {
  const protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove sheet protection from the active sheet, if the user has permission to
// edit it.
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection?.canEdit()) {
  protection.remove();
}

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeEditor(emailAddress)

تزيل هذه الدالة المستخدم المحدّد من قائمة المحرّرين للورقة أو النطاق المحميَّين. يُرجى العِلم أنّه إذا كان المستخدم عضوًا في مجموعة Google لديها إذن تعديل، أو إذا كان جميع المستخدمين في النطاق لديهم إذن تعديل، سيظل بإمكان المستخدم تعديل المنطقة المحمية. لا يمكن إزالة مالك جدول البيانات أو المستخدم الحالي.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Creates a variable for an email address.
// TODO(developer): Replace the email address with a valid one.
const TEST_EMAIL = 'baklavainthebalkans@gmail.com';

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Adds an editor to the protected sheet using the email address variable.
sampleProtectedSheet.addEditor(TEST_EMAIL);

// Removes the editor from the protected sheet using the email address variable.
sampleProtectedSheet.removeEditor(TEST_EMAIL);

// Gets the editors of the protected sheet.
const editors = sampleProtectedSheet.getEditors();

// Logs the editors' email addresses to the console.
for (const editor of editors) {
  console.log(editor.getEmail());
}

المعلمات

الاسمالنوعالوصف
emailAddressStringعنوان البريد الإلكتروني للمستخدم الذي تريد إزالته

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeEditor(user)

تزيل هذه الدالة المستخدم المحدّد من قائمة المحرّرين للورقة أو النطاق المحميَّين. يُرجى العِلم أنّه إذا كان المستخدم عضوًا في مجموعة Google لديها إذن تعديل، أو إذا كان جميع المستخدمين في النطاق لديهم إذن تعديل، سيظل بإمكان المستخدم تعديل المنطقة المحمية أيضًا. لا يمكن إزالة مالك جدول البيانات أو المستخدم الحالي.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Removes the active user from the editors of the protected sheet.
sampleProtectedSheet.removeEditor(Session.getActiveUser());

// Gets the editors of the protected sheet.
const editors = sampleProtectedSheet.getEditors();

// Logs the editors' email addresses to the console.
for (const editor of editors) {
  console.log(editor.getEmail());
}

المعلمات

الاسمالنوعالوصف
userUserتمثيل للمستخدم المطلوب إزالته

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، من أجل التسلسل

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeEditors(emailAddresses)

تزيل هذه الدالة مصفوفة المستخدمين المحدّدة من قائمة المحرّرين للورقة أو النطاق المحميَّين. يُرجى العِلم أنّه إذا كان أي من المستخدمين أعضاءً في مجموعة Google لديها إذن التعديل، أو إذا كان جميع المستخدمين في النطاق لديهم إذن التعديل، سيظل بإمكان هؤلاء المستخدمين تعديل المنطقة المحمية. لا يمكن إزالة مالك جدول البيانات أو المستخدم الحالي.

// Protect the active sheet, then remove all other users from the list of
// editors.
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.protect().setDescription('Sample protected sheet');

// Ensure the current user is an editor before removing others. Otherwise, if
// the user's edit permission comes from a group, the script throws an exception
// upon removing the group.
const me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

المعلمات

الاسمالنوعالوصف
emailAddressesString[]مصفوفة من عناوين البريد الإلكتروني للمستخدمين المطلوب إزالتهم.

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، من أجل التسلسل

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeTargetAudience(audienceId)

تزيل هذه الدالة الجمهور المستهدَف المحدّد كمحرّر للنطاق المحميّ.

المعلمات

الاسمالنوعالوصف
audienceIdStringمعرّف الجمهور المستهدَف الذي تريد إزالته.

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setDescription(description)

تضبط هذه السمة وصف النطاق أو ورقة البيانات المحمية.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets the sheet 'Sheet1' by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet.
const sampleProtectedSheet = sheet.protect();

// Sets the sheet description to 'Sheet1 is protected.'
sampleProtectedSheet.setDescription('Sheet1 is protected');

// Gets the description of the protected sheet.
const sampleProtectedSheetDescription = sampleProtectedSheet.getDescription();

// Logs the description of the protected sheet to the console.
console.log(sampleProtectedSheetDescription);

المعلمات

الاسمالنوعالوصف
descriptionStringوصف النطاق أو ورقة البيانات المحمية

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setDomainEdit(editable)

تضبط هذه السمة ما إذا كان جميع المستخدمين في النطاق الذي يملك جدول البيانات لديهم إذن بتعديل النطاق أو الورقة المحمية. يُرجى العِلم أنّ أي مستخدمين لديهم إذن تعديل صريح يمكنهم تعديل المنطقة المحمية بغض النظر عن هذا الإعداد. يطرح هذا الإجراء استثناءً إذا كان جدول البيانات لا ينتمي إلى نطاق Google Workspace (أي إذا كان مملوكًا لحساب gmail.com).

المعلمات

الاسمالنوعالوصف
editableBooleantrue إذا كان يجب أن يكون لدى جميع المستخدمين في النطاق الذي يملك جدول البيانات إذن بتعديل النطاق أو ورقة البيانات المحمية، false إذا لم يكن كذلك.

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، من أجل التسلسل

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setNamedRange(namedRange)

يربط النطاق المحمي بنطاق مسمّى حالي. إذا كان النطاق المسمّى يغطي مساحة مختلفة عن النطاق المحمي الحالي، تنقل هذه الطريقة الحماية لتغطية النطاق المسمّى بدلاً من ذلك. يجب أن يكون النطاق المسمّى في ورقة البيانات نفسها التي يتوفّر فيها النطاق المحمي الحالي. يُرجى العِلم أنّه يجب أن تستدعي البرامج النصية هذه الطريقة بشكل صريح لربط نطاق محمي بنطاق مسمّى، لأنّ استدعاء Range.protect() لإنشاء حماية من Range الذي يكون نطاقًا مسمّى، بدون استدعاء setRangeName(rangeName)، لا يكفي لربطهما. ومع ذلك، يؤدي إنشاء نطاق محمي من نطاق مسمّى في واجهة مستخدم &quot;جداول بيانات Google&quot; إلى ربطهما تلقائيًا.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Protects cells A1:D10 on Sheet1.
const sheet = ss.getSheetByName('Sheet1');
const protectedRange = sheet.getRange('A1:D10').protect();

// Logs the current protected range, A1:D10.
console.log(protectedRange.getRange().getA1Notation());

// Creates a named range for cells E1:J10 called 'NewRange.'
const newRange = sheet.getRange('E1:J10');
ss.setNamedRange('NewRange', newRange);
const namedRange = ss.getNamedRanges()[0];

// Updates the protected range to the named range, 'NewRange.'
// This updates the protected range on Sheet1 from A1:D10 to E1:J10.
protectedRange.setNamedRange(namedRange);

// Logs the updated protected range to the console.
console.log(protectedRange.getRange().getA1Notation());

المعلمات

الاسمالنوعالوصف
namedRangeNamedRangeالنطاق المُعنوَن الحالي الذي سيتم ربطه بالنطاق المحمي

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setRange(range)

تعديل النطاق الذي تتم حمايته إذا كان النطاق المحدّد يغطي مساحة مختلفة عن النطاق المحمي الحالي، تنقل هذه الطريقة الحماية لتغطية النطاق الجديد بدلاً من ذلك.

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Protects cells A1:D10 on Sheet1 of the spreadsheet.
const sheet = ss.getSheetByName('Sheet1');
const protectedRange = sheet.getRange('A1:D10').protect();

// Logs the original protected range, A1:D10, to the console.
console.log(protectedRange.getRange().getA1Notation());

// Gets the range E1:J10.
const newRange = sheet.getRange('E1:J10');

// Updates the protected range to E1:J10.
protectedRange.setRange(newRange);

// Logs the updated protected range to the console.
console.log(protectedRange.getRange().getA1Notation());

المعلمات

الاسمالنوعالوصف
rangeRangeالنطاق الجديد الذي يجب حمايته من التعديلات

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setRangeName(rangeName)

يربط النطاق المحمي بنطاق مسمّى حالي. إذا كان النطاق المسمّى يغطي مساحة مختلفة عن النطاق المحمي الحالي، تنقل هذه الطريقة الحماية لتغطية النطاق المسمّى بدلاً من ذلك. يجب أن يكون النطاق المسمّى في ورقة البيانات نفسها التي يتوفّر فيها النطاق المحمي الحالي. يُرجى العِلم أنّه يجب أن تستدعي البرامج النصية هذه الطريقة بشكل صريح لربط نطاق محمي بنطاق مسمّى، لأنّ استدعاء Range.protect() لإنشاء حماية من Range الذي يكون نطاقًا مسمّى، بدون استدعاء setRangeName(rangeName)، لا يكفي لربطهما. ومع ذلك، يؤدي إنشاء نطاق محمي من نطاق مسمّى في واجهة مستخدم &quot;جداول بيانات Google&quot; إلى ربطهما تلقائيًا.

// Protect a named range in a spreadsheet and log the name of the protected
// range.
const ss = SpreadsheetApp.getActive();
const range = ss.getRange('A1:B10');
const protection = range.protect();
ss.setNamedRange('Test', range);  // Create a named range.
protection.setRangeName(
    'Test');  // Associate the protection with the named range.
Logger.log(
    protection.getRangeName());  // Verify the name of the protected range.

المعلمات

الاسمالنوعالوصف
rangeNameStringتمثّل هذه السمة اسم النطاق المُعنوَن المطلوب حمايته.

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، من أجل التسلسل

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setUnprotectedRanges(ranges)

تزيل هذه الدالة الحماية عن مجموعة النطاقات المحدّدة في ورقة محمية. يتم عرض استثناء إذا كان الكائن Protection يتوافق مع نطاق محمي بدلاً من ورقة محمية أو إذا لم تكن أي من النطاقات في الورقة المحمية. لتغيير النطاقات غير المحمية، اضبط مصفوفة جديدة من النطاقات، ولإعادة حماية ورقة البيانات بأكملها، اضبط مصفوفة فارغة.

// Protect the active sheet except B2:C5, then remove all other users from the
// list of editors.
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.protect().setDescription('Sample protected sheet');
const unprotected = sheet.getRange('B2:C5');
protection.setUnprotectedRanges([unprotected]);

// Ensure the current user is an editor before removing others. Otherwise, if
// the user's edit permission comes from a group, the script throws an exception
// upon removing the group.
const me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

المعلمات

الاسمالنوعالوصف
rangesRange[]مصفوفة النطاقات التي يجب تركها غير محمية ضمن ورقة محمية

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، من أجل التسلسل

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setWarningOnly(warningOnly)

تضبط هذه السمة ما إذا كان النطاق المحمي يستخدم الحماية "القائمة على التحذير" أم لا. تعني الحماية المستندة إلى التحذيرات أنّه يمكن لكل مستخدم تعديل البيانات في المنطقة، ولكن سيظهر تحذير يطلب من المستخدم تأكيد التعديل. بشكلٍ تلقائي، لا تستند النطاقات أو الأوراق المحمية إلى التحذيرات. للتحقّق من حالة التحذير، استخدِم isWarningOnly().

// Opens the spreadsheet file by its URL. If you created your script from within
// a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet()
// instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);

// Gets the sheet 'Sheet1' by its name.
const sheet = ss.getSheetByName('Sheet1');

// Protects the sheet and sets the protection to warning-based.
const sampleProtectedSheet = sheet.protect().setWarningOnly(true);

// Logs whether the protected sheet is warning-based to the console.
console.log(sampleProtectedSheet.isWarningOnly());

المعلمات

الاسمالنوعالوصف
warningOnlyBoolean

الإرجاع

Protection: العنصر الذي يمثّل إعدادات الحماية، وذلك من أجل الربط.

التفويض

تتطلّب النصوص البرمجية التي تستخدم هذه الطريقة الحصول على إذن باستخدام نطاق واحد أو أكثر من النطاقات التالية:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets