Class Protection

حفاظت

به محدوده ها و برگه های محافظت شده دسترسی داشته باشید و آنها را اصلاح کنید. یک محدوده محافظت شده می تواند از یک محدوده ایستا از سلول ها یا یک محدوده نامگذاری شده محافظت کند. یک صفحه محافظت شده ممکن است شامل مناطق محافظت نشده باشد. برای صفحات گسترده ایجاد شده با نسخه قدیمی Google Sheets، به جای آن از کلاس 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());
}

مولفه های

نام تایپ کنید شرح
emailAddress String آدرس ایمیل کاربر برای افزودن

برگشت

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

مولفه های

نام تایپ کنید شرح
user User نمایشی از کاربر برای افزودن.

برگشت

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

مولفه های

نام تایپ کنید شرح
emailAddresses String[] مجموعه ای از آدرس های ایمیل کاربران برای افزودن.

برگشت

Protection - شیئی که تنظیمات حفاظتی را برای زنجیر نشان می دهد.

مجوز

اسکریپت هایی که از این روش استفاده می کنند نیاز به مجوز با یک یا چند مورد از حوزه های زیر دارند:

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

addTargetAudience(audienceId)

مخاطبین هدف مشخص شده را به عنوان ویرایشگر محدوده محافظت شده اضافه می کند.

مولفه های

نام تایپ کنید شرح
audienceId String شناسه مخاطب هدف برای افزودن.

برگشت

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

مولفه های

نام تایپ کنید شرح
emailAddress String آدرس ایمیل کاربر برای حذف.

برگشت

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

مولفه های

نام تایپ کنید شرح
user User نمایشی از کاربر برای حذف.

برگشت

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);
}

مولفه های

نام تایپ کنید شرح
emailAddresses String[] مجموعه ای از آدرس های ایمیل کاربران برای حذف.

برگشت

Protection - شیئی که تنظیمات حفاظتی را برای زنجیر نشان می دهد

مجوز

اسکریپت هایی که از این روش استفاده می کنند نیاز به مجوز با یک یا چند مورد از حوزه های زیر دارند:

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

removeTargetAudience(audienceId)

مخاطبین هدف مشخص شده را به عنوان ویرایشگر محدوده محافظت شده حذف می کند.

مولفه های

نام تایپ کنید شرح
audienceId String شناسه مخاطب هدف برای حذف.

برگشت

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

مولفه های

نام تایپ کنید شرح
description String شرح محدوده یا برگه حفاظت شده.

برگشت

Protection - شیئی که تنظیمات حفاظتی را برای زنجیر نشان می دهد.

مجوز

اسکریپت هایی که از این روش استفاده می کنند نیاز به مجوز با یک یا چند مورد از حوزه های زیر دارند:

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

setDomainEdit(editable)

تعیین می‌کند که آیا همه کاربران در دامنه‌ای که صفحه‌گسترده را دارند، مجوز ویرایش محدوده یا صفحه محافظت‌شده را داشته باشند یا خیر. توجه داشته باشید که کاربرانی که مجوز ویرایش صریح دارند می‌توانند بدون در نظر گرفتن این تنظیم، ناحیه محافظت شده را ویرایش کنند. اگر صفحه‌گسترده به دامنه Google Workspace تعلق نداشته باشد (یعنی اگر متعلق به یک حساب gmail.com باشد)، یک استثنا ایجاد می‌کند.

مولفه های

نام تایپ کنید شرح
editable Boolean true اگر همه کاربران دامنه ای که صفحه گسترده دارند باید اجازه ویرایش محدوده یا صفحه محافظت شده را داشته باشند. 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());

مولفه های

نام تایپ کنید شرح
namedRange NamedRange محدوده نامگذاری شده موجود برای ارتباط با محدوده محافظت شده.

برگشت

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

مولفه های

نام تایپ کنید شرح
range Range محدوده جدید برای محافظت در برابر ویرایش ها.

برگشت

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.

مولفه های

نام تایپ کنید شرح
rangeName String نام محدوده نام‌گذاری‌شده برای محافظت.

برگشت

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);
}

مولفه های

نام تایپ کنید شرح
ranges Range[] آرایه محدوده هایی که در یک صفحه محافظت شده بدون محافظت باقی می مانند.

برگشت

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

مولفه های

نام تایپ کنید شرح
warningOnly Boolean

برگشت

Protection - شیئی که تنظیمات حفاظتی را برای زنجیر نشان می دهد.

مجوز

اسکریپت هایی که از این روش استفاده می کنند نیاز به مجوز با یک یا چند مورد از حوزه های زیر دارند:

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