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

פרמטרים

שםתיאורהתיאור
emailAddressStringכתובת האימייל של המשתמש שיש להוסיף.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

addEditor(user)

הוספת המשתמש הנתון לרשימת העורכים של הגיליון או הטווח המוגנים. השיטה הזו לא נותנת למשתמש באופן אוטומטי הרשאה לערוך את הגיליון האלקטרוני עצמו. כדי לעשות זאת, צריך לקרוא ל-Spreadsheet.addEditor(user).

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

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

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

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

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

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

פרמטרים

שםתיאורהתיאור
userUserייצוג של המשתמש להוספה.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

addEditors(emailAddresses)

הוספת מערך המשתמשים הנתון לרשימת העורכים של הגיליון או הטווח המוגנים. השיטה הזו לא נותנת למשתמשים באופן אוטומטי הרשאה לערוך את הגיליון האלקטרוני עצמו. כדי לעשות זאת, צריך גם לקרוא ל-Spreadsheet.addEditors(emailAddresses).

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

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

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

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

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

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

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

פרמטרים

שםתיאורהתיאור
emailAddressesString[]מערך של כתובות אימייל של המשתמשים להוספה.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

addTargetAudience(audienceId)

הוספת קהל היעד שצוין כעורך של הטווח המוגן.

פרמטרים

שםתיאורהתיאור
audienceIdStringהמזהה של קהל היעד שצריך להוסיף.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

canDomainEdit()

הפונקציה קובעת אם לכל המשתמשים בדומיין שהגיליון האלקטרוני נמצא בבעלותו יש הרשאה לערוך את הטווח או הגיליון המוגנים. הפונקציה מוסיפה החרגה אם למשתמש אין הרשאה לערוך את הטווח או הגיליון המוגנים.

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

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

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

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

החזרות

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 Sheets, המערכת תשייך אותם באופן אוטומטי.

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

החזרות

Booleantrue אם הטווח או הגיליון המוגנים משתמשים רק בהגנה מבוססת אזהרה.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

remove()

ביטול ההגנה על הטווח או הגיליון.

// Remove all range protections in the spreadsheet that the user has permission to edit.
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());
}

פרמטרים

שםתיאורהתיאור
emailAddressStringכתובת האימייל של המשתמש שיש להסיר.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

removeEditor(user)

מסיר את המשתמש הנתון מרשימת העורכים של הגיליון או הטווח המוגנים. שימו לב שאם המשתמש הוא חבר בקבוצת Google שיש לה הרשאת עריכה, או אם לכל המשתמשים בדומיין יש הרשאת עריכה, הוא עדיין יוכל לערוך גם את האזור המוגן. אי אפשר להסיר את הבעלים של הגיליון האלקטרוני או את המשתמש הנוכחי.

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

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

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

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

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

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

פרמטרים

שםתיאורהתיאור
userUserייצוג של המשתמש שיש להסיר.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

removeEditors(emailAddresses)

מסירה את מערך המשתמשים הנתון מרשימת העורכים של הגיליון או הטווח המוגנים. שימו לב שאם משתמש כלשהו הוא חבר בקבוצת Google עם הרשאת עריכה, או אם לכל המשתמשים בדומיין יש הרשאת עריכה, הם עדיין יוכלו לערוך את האזור המוגן. לא ניתן להסיר את הבעלים של הגיליון האלקטרוני או את המשתמש הנוכחי.

// Protect the active sheet, then remove all other users from the list of editors.
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);
}

פרמטרים

שםתיאורהתיאור
emailAddressesString[]מערך של כתובות אימייל של המשתמשים שיש להסיר.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

removeTargetAudience(audienceId)

הסרה של קהל היעד שצוין כעורך של הטווח המוגן.

פרמטרים

שםתיאורהתיאור
audienceIdStringהמזהה של קהל היעד שיש להסיר.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

setDescription(description)

הגדרת התיאור של הטווח או הגיליון המוגנים.

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

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

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

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

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

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

פרמטרים

שםתיאורהתיאור
descriptionStringהתיאור של הטווח או הגיליון המוגנים.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

setDomainEdit(editable)

קובעת אם לכל המשתמשים בדומיין שהגיליון האלקטרוני נמצא בבעלותו יש הרשאה לערוך את הטווח או הגיליון המוגנים. שימו לב: כל המשתמשים עם הרשאת עריכה מפורשת יכולים לערוך את האזור המוגן, ללא קשר להגדרה הזו. הפונקציה מחריגה את הגיליון האלקטרוני אם הגיליון האלקטרוני לא שייך לדומיין ב-Google Workspace (כלומר, בבעלות של חשבון gmail.com).

פרמטרים

שםתיאורהתיאור
editableBooleantrue אם לכל המשתמשים בדומיין שבבעלותו הגיליון האלקטרוני צריכה להיות הרשאה לערוך את הטווח או הגיליון המוגנים. false אם לא.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

setNamedRange(namedRange)

משייכת את הטווח המוגן לטווח בעל שם קיים. אם הטווח בעל השם מכסה אזור אחר מהטווח המוגן הנוכחי, השיטה הזו מעבירה את ההגנה כך שתכסה את הטווח בעל השם. הטווח בעל השם חייב להיות באותו גיליון כמו הטווח המוגן הנוכחי. שימו לב שסקריפטים חייבים לקרוא לשיטה הזו באופן מפורש כדי לשייך טווח מוגן לטווח בעל שם. לא מספיק לשייך את Range.protect() כדי ליצור הגנה מ-Range שהוא טווח בעל שם, בלי לקרוא ל-setRangeName(rangeName). אבל כשיוצרים טווח מוגן מטווח בעל שם בממשק המשתמש של 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());

פרמטרים

שםתיאורהתיאור
namedRangeNamedRangeהטווח בעל השם הקיים שיש לשייך לטווח המוגן.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

setRange(range)

התאמת הטווח המוגן. אם הטווח הנתון מכסה אזור אחר מהטווח המוגן הנוכחי, השיטה הזו מעבירה את ההגנה כך שתכסה את הטווח החדש.

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

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

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

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

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

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

פרמטרים

שםתיאורהתיאור
rangeRangeהטווח החדש שצריך להגן עליו מפני עריכות.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

setRangeName(rangeName)

משייכת את הטווח המוגן לטווח בעל שם קיים. אם הטווח בעל השם מכסה אזור אחר מהטווח המוגן הנוכחי, השיטה הזו מעבירה את ההגנה כך שתכסה את הטווח בעל השם. הטווח בעל השם חייב להיות באותו גיליון כמו הטווח המוגן הנוכחי. שימו לב שסקריפטים חייבים לקרוא לשיטה הזו באופן מפורש כדי לשייך טווח מוגן לטווח בעל שם. לא מספיק לשייך את Range.protect() כדי ליצור הגנה מ-Range שהוא טווח בעל שם, בלי לקרוא ל-setRangeName(rangeName). אבל כשיוצרים טווח מוגן מטווח בעל שם בממשק המשתמש של Google Sheets, המערכת משייכת אותם באופן אוטומטי.

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

פרמטרים

שםתיאורהתיאור
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);
}

פרמטרים

שםתיאורהתיאור
rangesRange[]מערך הטווחים שיש להשאיר לא מוגנים בתוך גיליון מוגן.

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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

setWarningOnly(warningOnly)

המדיניות הזו קובעת אם הטווח המוגן הזה משתמש בהגנה 'מבוססת אזהרה'. המשמעות של הגנה מבוססת אזהרה היא שכל משתמש יכול לערוך נתונים באזור, חוץ מהעובדה שתוצג אזהרה שבה תתבקשו לאשר את העריכה. כברירת מחדל, טווחים או גיליונות מוגנים לא מבוססים על אזהרה. כדי לבדוק את מצב האזהרה, יש להשתמש בisWarningOnly().

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

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

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

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

פרמטרים

שםתיאורהתיאור
warningOnlyBoolean

החזרות

Protection – האובייקט שמייצג את הגדרות ההגנה לשרשור.

הרשאות

לסקריפטים שמשתמשים בשיטה הזו נדרשת הרשאה באחד או יותר מההיקפים הבאים:

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