Class Protection

حفاظت

به محدوده‌ها و برگه‌های محافظت‌شده دسترسی پیدا کنید و آنها را تغییر دهید. یک محدوده محافظت‌شده می‌تواند هم از یک محدوده ثابت از سلول‌ها و هم از یک محدوده نامگذاری‌شده محافظت کند. یک برگه محافظت‌شده ممکن است شامل نواحی محافظت‌نشده باشد. برای صفحات گسترده‌ای که با نسخه قدیمی‌تر Google Sheets ایجاد شده‌اند، به جای آن از کلاس Page Protection استفاده کنید.

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

روش‌ها

روش نوع بازگشتی شرح مختصر
add Editor(emailAddress) Protection کاربر داده شده را به لیست ویرایشگرهای برگه یا محدوده محافظت شده اضافه می‌کند.
add Editor(user) Protection کاربر داده شده را به لیست ویرایشگرهای برگه یا محدوده محافظت شده اضافه می‌کند.
add Editors(emailAddresses) Protection آرایه‌ی داده شده از کاربران را به لیست ویرایشگرهای برگه یا محدوده‌ی محافظت‌شده اضافه می‌کند.
add Target Audience(audienceId) Protection مخاطب هدف مشخص شده را به عنوان ویرایشگر محدوده حفاظت شده اضافه می‌کند.
can Domain Edit() Boolean تعیین می‌کند که آیا همه کاربران در دامنه‌ای که مالک صفحه گسترده است، اجازه ویرایش محدوده یا صفحه محافظت‌شده را دارند یا خیر.
can Edit() Boolean تعیین می‌کند که آیا کاربر اجازه ویرایش محدوده یا برگه محافظت‌شده را دارد یا خیر.
get Description() String توضیحات مربوط به محدوده یا صفحه حفاظت‌شده را دریافت می‌کند.
get Editors() User[] لیست ویرایشگرهای مربوط به محدوده یا برگه محافظت شده را دریافت می‌کند.
get Protection Type() Protection Type نوع منطقه حفاظت‌شده، یا RANGE یا SHEET را برمی‌گرداند.
get Range() Range محدوده‌ای که محافظت می‌شود را دریافت می‌کند.
get Range Name() String|null اگر محدوده‌ی حفاظت‌شده به یک محدوده‌ی نامگذاری‌شده مرتبط باشد، نام آن را برمی‌گرداند.
get Target Audiences() Target Audience[] شناسه‌های مخاطبان هدف را که می‌توانند محدوده محافظت‌شده را ویرایش کنند، برمی‌گرداند.
get Unprotected Ranges() Range[] آرایه‌ای از محدوده‌های محافظت نشده درون یک شیت محافظت شده را دریافت می‌کند.
is Warning Only() Boolean تعیین می‌کند که آیا منطقه حفاظت‌شده از حفاظت «مبتنی بر هشدار» استفاده می‌کند یا خیر.
remove() void محدوده یا صفحه را از حالت محافظت خارج می‌کند.
remove Editor(emailAddress) Protection کاربر داده شده را از لیست ویرایشگرهای برگه یا محدوده محافظت شده حذف می‌کند.
remove Editor(user) Protection کاربر داده شده را از لیست ویرایشگرهای برگه یا محدوده محافظت شده حذف می‌کند.
remove Editors(emailAddresses) Protection آرایه‌ی داده شده از کاربران را از فهرست ویرایشگرهای برگه یا محدوده‌ی محافظت‌شده حذف می‌کند.
remove Target Audience(audienceId) Protection مخاطب هدف مشخص شده را به عنوان ویرایشگر محدوده حفاظت شده حذف می‌کند.
set Description(description) Protection توضیحات مربوط به محدوده یا برگه محافظت‌شده را تنظیم می‌کند.
set Domain Edit(editable) Protection تعیین می‌کند که آیا همه کاربران در دامنه‌ای که مالک صفحه گسترده است، اجازه ویرایش محدوده یا صفحه محافظت‌شده را دارند یا خیر.
set Named Range(namedRange) Protection محدوده‌ی حفاظت‌شده را به یک محدوده‌ی دارای نام موجود مرتبط می‌کند.
set Range(range) Protection محدوده‌ای که محافظت می‌شود را تنظیم می‌کند.
set Range Name(rangeName) Protection محدوده‌ی حفاظت‌شده را به یک محدوده‌ی دارای نام موجود مرتبط می‌کند.
set Unprotected Ranges(ranges) Protection آرایه‌ی داده شده از محدوده‌ها را درون یک شیت محافظت‌شده از حالت محافظت‌شده خارج می‌کند.
set Warning Only(warningOnly) Protection تنظیم می‌کند که آیا این محدوده‌ی محافظت‌شده از محافظت «مبتنی بر هشدار» استفاده می‌کند یا خیر.

مستندات دقیق

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

پارامترها

نام نوع توضیحات
email Address String آدرس ایمیل کاربر برای اضافه کردن.

بازگشت

Protection — شیء نمایانگر تنظیمات حفاظت، برای زنجیره‌سازی.

مجوز

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

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

add Editor(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

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

پارامترها

نام نوع توضیحات
email Addresses String[] آرایه‌ای از آدرس‌های ایمیل کاربران برای اضافه کردن.

بازگشت

Protection — شیء نمایانگر تنظیمات حفاظت، برای زنجیره‌سازی.

مجوز

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

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

add Target Audience(audienceId)

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

پارامترها

نام نوع توضیحات
audience Id String شناسه مخاطب هدف برای اضافه کردن.

بازگشت

Protection — شیء نمایانگر تنظیمات حفاظت، برای زنجیره‌سازی.

مجوز

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

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

can Domain Edit()

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

// 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 true و اگر نداشته باشند false برمی‌گرداند.

مجوز

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

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

can Edit()

تعیین می‌کند که آیا کاربر اجازه ویرایش محدوده یا برگه محافظت‌شده را دارد یا خیر. مالک صفحه گسترده همیشه قادر به ویرایش محدوده‌ها و برگه‌های محافظت‌شده است.

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

بازگشت

Boolean - اگر کاربر اجازه ویرایش محدوده یا برگه محافظت‌شده را داشته باشد، true و در غیر این صورت false برمی‌گرداند.

مجوز

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

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

get 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 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

get Editors()

لیست ویرایشگرهای محدوده یا برگه محافظت‌شده را دریافت می‌کند. اگر کاربر مجوز ویرایش محدوده یا برگه محافظت‌شده را نداشته باشد، استثنا ایجاد می‌کند.

// 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

get Protection Type()

نوع منطقه حفاظت‌شده، یا 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());

بازگشت

Protection Type — نوع منطقه حفاظت‌شده، یا RANGE یا SHEET .

مجوز

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

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

get 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',
);

// 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

get Range Name()

اگر محدوده‌ی محافظت‌شده با یک محدوده‌ی نامگذاری‌شده مرتبط باشد، نام آن را برمی‌گرداند. اگر محافظت با محدوده‌ی نامگذاری‌شده مرتبط نباشد، null را برمی‌گرداند. توجه داشته باشید که اسکریپت‌ها باید صریحاً set Range Name(rangeName) برای مرتبط کردن یک محدوده‌ی محافظت‌شده با یک محدوده‌ی نامگذاری‌شده فراخوانی کنند؛ فراخوانی Range.protect() برای ایجاد یک محافظت از Range که اتفاقاً یک محدوده‌ی نامگذاری‌شده است، بدون فراخوانی set Range Name(rangeName) ، برای مرتبط کردن آنها کافی نیست. با این حال، ایجاد یک محدوده‌ی محافظت‌شده از یک محدوده‌ی نامگذاری‌شده در رابط کاربری Google Sheets، آنها را به طور خودکار مرتبط می‌کند.

// 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

get Target Audiences()

شناسه‌های مخاطبان هدف را که می‌توانند محدوده محافظت‌شده را ویرایش کنند، برمی‌گرداند.

بازگشت

Target Audience[] — آرایه‌ای از شناسه‌های مخاطبان هدف.

مجوز

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

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

get Unprotected Ranges()

آرایه‌ای از محدوده‌های محافظت نشده درون یک شیت محافظت شده را دریافت می‌کند. اگر شیء Protection به جای یک شیت محافظت شده، مربوط به یک محدوده محافظت شده باشد، این متد یک آرایه خالی برمی‌گرداند. برای تغییر محدوده‌های محافظت نشده، set Unprotected Ranges(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

is Warning Only()

تعیین می‌کند که آیا منطقه حفاظت‌شده از حفاظت "مبتنی بر هشدار" استفاده می‌کند یا خیر. حفاظت مبتنی بر هشدار به این معنی است که هر کاربری می‌تواند داده‌ها را در منطقه ویرایش کند، به جز ویرایش که هشداری را نمایش می‌دهد و از کاربر می‌خواهد ویرایش را تأیید کند. به طور پیش‌فرض، محدوده‌ها یا برگه‌های حفاظت‌شده مبتنی بر هشدار نیستند. برای تغییر به حالت هشدار، set Warning Only(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.
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

remove Editor(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',
);

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

پارامترها

نام نوع توضیحات
email Address String آدرس ایمیل کاربر برای حذف.

بازگشت

Protection — شیء نمایانگر تنظیمات حفاظت، برای زنجیره‌سازی.

مجوز

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

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

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

// 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

remove Editors(emailAddresses)

آرایه داده شده از کاربران را از لیست ویرایشگرهای صفحه یا محدوده محافظت شده حذف می‌کند. توجه داشته باشید که اگر هر یک از کاربران عضو یک گروه گوگل باشند که مجوز ویرایش دارد، یا اگر همه کاربران دامنه مجوز ویرایش داشته باشند، آن کاربران همچنان می‌توانند منطقه حفاظت شده را ویرایش کنند. نه مالک صفحه گسترده و نه کاربر فعلی قابل حذف نیستند.

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

پارامترها

نام نوع توضیحات
email Addresses String[] آرایه‌ای از آدرس‌های ایمیل کاربرانی که باید حذف شوند.

بازگشت

Protection - شیء نشان دهنده تنظیمات حفاظت، برای زنجیره سازی

مجوز

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

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

remove Target Audience(audienceId)

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

پارامترها

نام نوع توضیحات
audience Id String شناسه مخاطب هدف برای حذف.

بازگشت

Protection — شیء نمایانگر تنظیمات حفاظت، برای زنجیره‌سازی.

مجوز

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

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

set Description(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

set Domain Edit(editable)

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

پارامترها

نام نوع توضیحات
editable Boolean اگر همه کاربران دامنه‌ای که مالک صفحه‌گسترده هستند، باید مجوز ویرایش محدوده یا صفحه محافظت‌شده را داشته باشند، true و در غیر این صورت false برمی‌گرداند.

بازگشت

Protection - شیء نشان دهنده تنظیمات حفاظت، برای زنجیره سازی

مجوز

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

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

set Named Range(namedRange)

محدوده‌ی محافظت‌شده را با یک محدوده‌ی نام‌گذاری‌شده‌ی موجود مرتبط می‌کند. اگر محدوده‌ی نام‌گذاری‌شده ناحیه‌ای متفاوت از محدوده‌ی محافظت‌شده‌ی فعلی را پوشش دهد، این روش، محافظت را به محدوده‌ی نام‌گذاری‌شده منتقل می‌کند. محدوده‌ی نام‌گذاری‌شده باید در همان برگه‌ای باشد که محدوده‌ی محافظت‌شده‌ی فعلی قرار دارد. توجه داشته باشید که اسکریپت‌ها باید صریحاً این روش را برای مرتبط کردن یک محدوده‌ی محافظت‌شده با یک محدوده‌ی نام‌گذاری‌شده فراخوانی کنند؛ فراخوانی Range.protect() برای ایجاد یک محافظت از Range که اتفاقاً یک محدوده‌ی نام‌گذاری‌شده است، بدون فراخوانی set Range Name(rangeName) ، برای مرتبط کردن آنها کافی نیست. با این حال، ایجاد یک محدوده‌ی محافظت‌شده از یک محدوده‌ی نام‌گذاری‌شده در رابط کاربری Google Sheets، آنها را به طور خودکار مرتبط می‌کند.

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

پارامترها

نام نوع توضیحات
named Range Named Range محدوده نامگذاری شده موجود برای مرتبط کردن با محدوده حفاظت شده.

بازگشت

Protection — شیء نمایانگر تنظیمات حفاظت، برای زنجیره‌سازی.

مجوز

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

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

set Range(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

set Range Name(rangeName)

محدوده‌ی محافظت‌شده را با یک محدوده‌ی نام‌گذاری‌شده‌ی موجود مرتبط می‌کند. اگر محدوده‌ی نام‌گذاری‌شده ناحیه‌ای متفاوت از محدوده‌ی محافظت‌شده‌ی فعلی را پوشش دهد، این روش، محافظت را به محدوده‌ی نام‌گذاری‌شده منتقل می‌کند. محدوده‌ی نام‌گذاری‌شده باید در همان برگه‌ای باشد که محدوده‌ی محافظت‌شده‌ی فعلی قرار دارد. توجه داشته باشید که اسکریپت‌ها باید صریحاً این روش را برای مرتبط کردن یک محدوده‌ی محافظت‌شده با یک محدوده‌ی نام‌گذاری‌شده فراخوانی کنند؛ فراخوانی Range.protect() برای ایجاد یک محافظت از Range که اتفاقاً یک محدوده‌ی نام‌گذاری‌شده است، بدون فراخوانی set Range Name(rangeName) ، برای مرتبط کردن آنها کافی نیست. با این حال، ایجاد یک محدوده‌ی محافظت‌شده از یک محدوده‌ی نام‌گذاری‌شده در رابط کاربری Google Sheets، آنها را به طور خودکار مرتبط می‌کند.

// 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.

پارامترها

نام نوع توضیحات
range Name String نام محدوده نامگذاری شده برای محافظت.

بازگشت

Protection - شیء نشان دهنده تنظیمات حفاظت، برای زنجیره سازی

مجوز

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

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

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

پارامترها

نام نوع توضیحات
ranges Range[] آرایه‌ای از محدوده‌ها که باید درون یک برگه محافظت‌شده، بدون محافظت رها شوند.

بازگشت

Protection - شیء نشان دهنده تنظیمات حفاظت، برای زنجیره سازی

مجوز

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

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

set Warning Only(warningOnly)

تعیین می‌کند که آیا این محدوده‌ی محافظت‌شده از محافظت «مبتنی بر هشدار» استفاده می‌کند یا خیر. محافظت مبتنی بر هشدار به این معنی است که هر کاربری می‌تواند داده‌ها را در این محدوده ویرایش کند، به جز ویرایش که هشداری را نمایش می‌دهد و از کاربر می‌خواهد ویرایش را تأیید کند. به طور پیش‌فرض، محدوده‌ها یا برگه‌های محافظت‌شده مبتنی بر هشدار نیستند. برای بررسی وضعیت هشدار، is Warning Only() استفاده کنید.

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

پارامترها

نام نوع توضیحات
warning Only Boolean

بازگشت

Protection — شیء نمایانگر تنظیمات حفاظت، برای زنجیره‌سازی.

مجوز

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

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