Class Protection

הגנה

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

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

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

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

Methods

שיטהסוג הערך שמוחזרתיאור קצר
addEditor(emailAddress)Protectionהפונקציה מוסיפה את המשתמש שצוין לרשימת העורכים של הגיליון או הטווח המוגנים.
addEditor(user)Protectionהפונקציה מוסיפה את המשתמש שצוין לרשימת העורכים של הגיליון או הטווח המוגנים.
addEditors(emailAddresses)Protectionמוסיפה את מערך המשתמשים שצוין לרשימת העורכים של הגיליון או הטווח המוגנים.
addTargetAudience(audienceId)Protectionהפונקציה מוסיפה את קהל היעד שצוין כבעל הרשאת עריכה בטווח התאים המוגן.
canDomainEdit()Booleanההגדרה קובעת אם לכל המשתמשים בדומיין שבבעלותו הגיליון האלקטרוני יש הרשאה לערוך את הטווח או הגיליון המוגנים.
canEdit()Booleanהמדיניות קובעת אם למשתמש יש הרשאה לערוך את הטווח או הגיליון המוגנים.
getDescription()Stringמחזירה את התיאור של הטווח או הגיליון המוגנים.
getEditors()User[]מחזירה את רשימת העורכים של הטווח או הגיליון המוגנים.
getProtectionType()ProtectionTypeמחזירה את סוג האזור המוגן, RANGE או SHEET.
getRange()Rangeמחזירה את הטווח שמוגן.
getRangeName()String|nullמחזירה את השם של טווח התאים המוגן אם הוא משויך לטווח תאים בעל שם.
getTargetAudiences()TargetAudience[]הפונקציה מחזירה את מזהי קהלי היעד שיכולים לערוך את הטווח המוגן.
getUnprotectedRanges()Range[]מחזירה מערך של טווחים לא מוגנים בגיליון מוגן.
isWarningOnly()Booleanקובעת אם האזור המוגן משתמש בהגנה מבוססת-אזהרה.
remove()voidהסרת ההגנה מהטווח או מהגיליון.
removeEditor(emailAddress)Protectionהפונקציה מסירה את המשתמש שצוין מרשימת העורכים של הגיליון או הטווח המוגנים.
removeEditor(user)Protectionהפונקציה מסירה את המשתמש שצוין מרשימת העורכים של הגיליון או הטווח המוגנים.
removeEditors(emailAddresses)Protectionמסירה את מערך המשתמשים שצוין מרשימת העורכים של הגיליון או הטווח המוגנים.
removeTargetAudience(audienceId)Protectionמסירה את קהל היעד שצוין כבעל הרשאת עריכה בטווח התאים המוגן.
setDescription(description)Protectionהגדרת התיאור של הטווח או הגיליון המוגנים.
setDomainEdit(editable)Protectionההגדרה קובעת אם לכל המשתמשים בדומיין שבבעלותו הגיליון האלקטרוני יש הרשאה לערוך את הטווח או הגיליון המוגנים.
setNamedRange(namedRange)Protectionמשייך את הטווח המוגן לטווח קיים בעל שם.
setRange(range)Protectionמשנה את הטווח שמוגן.
setRangeName(rangeName)Protectionמשייך את הטווח המוגן לטווח קיים בעל שם.
setUnprotectedRanges(ranges)Protectionהפונקציה מבטלת את ההגנה של מערך הטווחים הנתון בגיליון מוגן.
setWarningOnly(warningOnly)Protectionמגדיר אם הטווח המוגן הזה משתמש בהגנה מבוססת-אזהרה.

תיעוד מפורט

addEditor(emailAddress)

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

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

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

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

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

addEditor(user)

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

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

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

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

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

addEditors(emailAddresses)

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

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

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

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

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

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

addTargetAudience(audienceId)

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

פרמטרים

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

חזרה

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

אישור

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

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

canDomainEdit()

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

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

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

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

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

חזרה

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

אישור

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

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

canEdit()

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

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

חזרה

Booleantrue אם למשתמש יש הרשאה לערוך את הטווח או הגיליון המוגנים; false אם לא

אישור

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

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

getDescription()

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

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

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

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

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

חזרה

String — תיאור הטווח או הגיליון המוגנים, או מחרוזת ריקה אם לא הוגדר תיאור.

אישור

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

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

getEditors()

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

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

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

חזרה

User[] — מערך של משתמשים עם הרשאה לערוך את הטווח או הגיליון המוגנים

אישור

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

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

getProtectionType()

מחזירה את סוג האזור המוגן, RANGE או SHEET.

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

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

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

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

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

חזרה

ProtectionType — סוג האזור המוגן, RANGE או SHEET.

אישור

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

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

getRange()

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

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

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

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

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

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

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

חזרה

Range – הטווח שמוגן.

אישור

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

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

getRangeName()

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

getTargetAudiences()

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

חזרה

TargetAudience[] — מערך של מזהים של קהלי יעד.

אישור

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

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

getUnprotectedRanges()

מחזירה מערך של טווחים לא מוגנים בגיליון מוגן. אם אובייקט Protection תואם לטווח מוגן במקום לגיליון מוגן, השיטה הזו מחזירה מערך ריק. כדי לשנות את הטווחים הלא מוגנים, משתמשים ב-setUnprotectedRanges(ranges) כדי להגדיר מערך חדש של טווחים. כדי להגן מחדש על הגיליון כולו, מגדירים מערך ריק.

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

חזרה

Range[] – מערך של טווחים לא מוגנים בגיליון מוגן

אישור

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

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

isWarningOnly()

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

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

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

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

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

const protectedSheetWarningStatus = sampleProtectedSheet.isWarningOnly();

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

חזרה

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

אישור

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

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

remove()

הסרת ההגנה מהטווח או מהגיליון.

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

אישור

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

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

removeEditor(emailAddress)

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

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

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

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

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

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

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

removeEditor(user)

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

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

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

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

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

removeEditors(emailAddresses)

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

removeTargetAudience(audienceId)

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

פרמטרים

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

חזרה

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

אישור

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

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

setDescription(description)

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

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

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

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

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

setDomainEdit(editable)

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

פרמטרים

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

חזרה

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

אישור

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

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

setNamedRange(namedRange)

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

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

פרמטרים

שםסוגתיאור
rangeNameStringהשם של טווח התאים בעל השם שרוצים להגן עליו.

חזרה

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

אישור

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

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

setUnprotectedRanges(ranges)

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

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

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

פרמטרים

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

חזרה

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

אישור

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

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

setWarningOnly(warningOnly)

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

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

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

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

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

פרמטרים

שםסוגתיאור
warningOnlyBoolean

חזרה

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

אישור

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

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