Class Protection

الحماية

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

// Protect range A1:B10, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var 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.
var 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.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Protect the active sheet, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var 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.
var 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تحصل على اسم النطاق المحمي إذا كان مرتبطًا بنطاق مُعنوَن.
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());
}

المَعلمات

الاسمTypeالوصف
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());
}

المَعلمات

الاسمTypeالوصف
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());
}

المَعلمات

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

استرجاع الكرة

Protection: الكائن الذي يمثّل إعدادات الحماية للسلاسل.

التفويض

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

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

addTargetAudience(audienceId)

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

المَعلمات

الاسمTypeالوصف
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());

استرجاع الكرة

Boolean - true إذا كان لدى جميع المستخدمين في النطاق الذين يملكون جدول البيانات الإذن بتعديل النطاق المحمي أو ورقة البيانات، و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.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}

استرجاع الكرة

Boolean: true إذا كان المستخدم لديه إذن بتعديل النطاق المحمي أو ورقة البيانات المحمية، و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.
var sheet = SpreadsheetApp.getActiveSheet();
var 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.
var 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) لا يكفي لربطها. ومع ذلك، يؤدي إنشاء نطاق محمي من نطاق مُعنوَن في واجهة مستخدم "جداول بيانات Google" إلى ربطه تلقائيًا.

// Protect a named range in a spreadsheet and log the name of the protected range.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var 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 إذا لم يكن النطاق المحمي مرتبطًا بنطاق مُعنوَن

التفويض

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

  • 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.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect();
var 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);

استرجاع الكرة

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

التفويض

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

  • 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.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove sheet protection from the active sheet, if the user has permission to edit it.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection && 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());
}

المَعلمات

الاسمTypeالوصف
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());
}

المَعلمات

الاسمTypeالوصف
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.
var sheet = SpreadsheetApp.getActiveSheet();
var 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.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

المَعلمات

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

استرجاع الكرة

Protection: الكائن الذي يمثّل إعدادات الحماية للتسلسل

التفويض

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

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

removeTargetAudience(audienceId)

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

المَعلمات

الاسمTypeالوصف
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);

المَعلمات

الاسمTypeالوصف
descriptionStringوصف النطاق المحمي أو الورقة المحميّة.

استرجاع الكرة

Protection: الكائن الذي يمثّل إعدادات الحماية للسلاسل.

التفويض

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

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

setDomainEdit(editable)

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

المَعلمات

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

استرجاع الكرة

Protection: الكائن الذي يمثّل إعدادات الحماية للتسلسل

التفويض

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

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

setNamedRange(namedRange)

لربط النطاق المحمي بنطاق مُعنوَن حالي. إذا كان النطاق المسمى يغطي مساحة مختلفة عن النطاق المحمي الحالي، فإن هذه الطريقة تنقل الحماية لتغطية النطاق المسمى بدلاً من ذلك. يجب أن يكون النطاق المسمى في نفس ورقة النطاق المحمي الحالي. تجدر الإشارة إلى أنّ النصوص البرمجية يجب أن تستدعي هذه الطريقة صراحةً لربط نطاق محمي بنطاق مُعنوَن، إذ أنّ استدعاء Range.protect() لإنشاء حماية من Range يصادف أنه نطاق مُعنوَن بدون استدعاء setRangeName(rangeName)، لا يكفي لربطه. ومع ذلك، يؤدي إنشاء نطاق محمي من نطاق مُعنوَن في واجهة مستخدم "جداول بيانات 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');

// 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());

المَعلمات

الاسمTypeالوصف
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());

المَعلمات

الاسمTypeالوصف
rangeRangeالنطاق الجديد للحماية من التعديلات

استرجاع الكرة

Protection: الكائن الذي يمثّل إعدادات الحماية للسلاسل.

التفويض

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

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

setRangeName(rangeName)

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

// Protect a named range in a spreadsheet and log the name of the protected range.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var 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.

المَعلمات

الاسمTypeالوصف
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.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');
var 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.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

المَعلمات

الاسمTypeالوصف
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());

المَعلمات

الاسمTypeالوصف
warningOnlyBoolean

استرجاع الكرة

Protection: الكائن الذي يمثّل إعدادات الحماية للسلاسل.

التفويض

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

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