Class Protection

הגנה

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

כדי לאחזר הגנות קיימות, משתמשים בפונקציות Sheet.getProtections(type) או Spreadsheet.getProtections(type).

// 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מסירה את מערך המשתמשים שצוין מרשימת עורכי Docs של הגיליון או הטווח המוגנים.
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)

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