Class Protection

Ochrona

Uzyskiwanie dostępu do chronionych zakresów i arkuszy oraz ich modyfikowanie. Chroniony zakres może obejmować statyczny zakres komórek lub zakres nazwany. Chroniony arkusz może zawierać niechronione regiony. W przypadku arkuszy kalkulacyjnych utworzonych w starszej wersji Arkuszy Google użyj klasy 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);
}

Metody

MetodaZwracany typKrótki opis
addEditor(emailAddress)ProtectionDodaje podanego użytkownika do listy edytujących chroniony arkusz lub zakres.
addEditor(user)ProtectionDodaje podanego użytkownika do listy edytujących chroniony arkusz lub zakres.
addEditors(emailAddresses)ProtectionDodaje podaną tablicę użytkowników do listy edytujących chroniony arkusz lub zakres.
addTargetAudience(audienceId)ProtectionDodaje określoną grupę odbiorców jako edytującego zakres chroniony.
canDomainEdit()BooleanOkreśla, czy wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, mają uprawnienia do edytowania chronionego zakresu lub arkusza.
canEdit()BooleanOkreśla, czy użytkownik ma uprawnienia do edytowania chronionego zakresu lub arkusza.
getDescription()StringPobiera opis chronionego zakresu lub arkusza.
getEditors()User[]Pobiera listę edytorów chronionego zakresu lub arkusza.
getProtectionType()ProtectionTypePobiera typ obszaru chronionego, czyli RANGE lub SHEET.
getRange()RangeZwraca zakres, który jest chroniony.
getRangeName()String|nullZwraca nazwę chronionego zakresu, jeśli jest on powiązany z zakresem nazwanym.
getTargetAudiences()TargetAudience[]Zwraca identyfikatory odbiorców docelowych, którzy mogą edytować zakres chroniony.
getUnprotectedRanges()Range[]Pobiera tablicę niechronionych zakresów w chronionym arkuszu.
isWarningOnly()BooleanOkreśla, czy obszar chroniony korzysta z ochrony „opartej na ostrzeżeniach”.
remove()voidUsuwa ochronę zakresu lub arkusza.
removeEditor(emailAddress)ProtectionUsuwa danego użytkownika z listy edytujących chroniony arkusz lub zakres.
removeEditor(user)ProtectionUsuwa danego użytkownika z listy edytujących chroniony arkusz lub zakres.
removeEditors(emailAddresses)ProtectionUsuwa z listy edytujących chroniony arkusz lub zakres podaną tablicę użytkowników.
removeTargetAudience(audienceId)ProtectionUsuwa określoną grupę odbiorców jako edytującego zakres chroniony.
setDescription(description)ProtectionUstawia opis chronionego zakresu lub arkusza.
setDomainEdit(editable)ProtectionOkreśla, czy wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, mają uprawnienia do edytowania chronionego zakresu lub arkusza.
setNamedRange(namedRange)ProtectionPowiązanie chronionego zakresu z istniejącym nazwanym zakresem.
setRange(range)ProtectionDostosowuje zakres, który jest chroniony.
setRangeName(rangeName)ProtectionPowiązanie chronionego zakresu z istniejącym nazwanym zakresem.
setUnprotectedRanges(ranges)ProtectionUsuwa ochronę z podanej tablicy zakresów w chronionym arkuszu.
setWarningOnly(warningOnly)ProtectionOkreśla, czy ten chroniony zakres korzysta z ochrony „opartej na ostrzeżeniach”.

Szczegółowa dokumentacja

addEditor(emailAddress)

Dodaje podanego użytkownika do listy edytujących chroniony arkusz lub zakres. Ta metoda nie przyznaje automatycznie użytkownikowi uprawnień do edytowania samego arkusza kalkulacyjnego. Aby to zrobić, wywołaj dodatkowo funkcję 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());
}

Parametry

NazwaTypOpis
emailAddressStringAdres e-mail użytkownika, którego chcesz dodać.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

addEditor(user)

Dodaje podanego użytkownika do listy edytujących chroniony arkusz lub zakres. Ta metoda nie przyznaje automatycznie użytkownikowi uprawnień do edytowania samego arkusza kalkulacyjnego. Aby to zrobić, wywołaj dodatkowo funkcję 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());
}

Parametry

NazwaTypOpis
userUserReprezentacja użytkownika, którego chcesz dodać.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

addEditors(emailAddresses)

Dodaje podaną tablicę użytkowników do listy edytujących chroniony arkusz lub zakres. Ta metoda nie przyznaje automatycznie użytkownikom uprawnień do edytowania samego arkusza kalkulacyjnego. Aby to zrobić, wywołaj dodatkowo metodę 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());
}

Parametry

NazwaTypOpis
emailAddressesString[]Tablica adresów e-mail użytkowników, których chcesz dodać.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

addTargetAudience(audienceId)

Dodaje określoną grupę odbiorców jako edytującego zakres chroniony.

Parametry

NazwaTypOpis
audienceIdStringIdentyfikator grupy odbiorców do dodania.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

canDomainEdit()

Określa, czy wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, mają uprawnienia do edytowania chronionego zakresu lub arkusza. Zgłasza wyjątek, jeśli użytkownik nie ma uprawnień do edytowania zakresu chronionego lub arkusza.

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

Powrót

Boolean – true, jeśli wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, mają uprawnienia do edytowania chronionego zakresu lub arkusza; false, jeśli nie mają takich uprawnień.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

canEdit()

Określa, czy użytkownik ma uprawnienia do edytowania chronionego zakresu lub arkusza. Właściciel arkusza kalkulacyjnego zawsze może edytować chronione zakresy i arkusze.

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

Powrót

Boolean – true, jeśli użytkownik ma uprawnienia do edytowania chronionego zakresu lub arkusza; false, jeśli nie ma takich uprawnień.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

getDescription()

Pobiera opis chronionego zakresu lub arkusza. Jeśli opis nie jest ustawiony, ta metoda zwraca pusty ciąg znaków.

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

Powrót

String – opis chronionego zakresu lub arkusza albo pusty ciąg znaków, jeśli nie ustawiono opisu.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

getEditors()

Pobiera listę edytorów chronionego zakresu lub arkusza. Zgłasza wyjątek, jeśli użytkownik nie ma uprawnień do edytowania zakresu chronionego lub arkusza.

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

Powrót

User[] – tablica użytkowników z uprawnieniami do edytowania chronionego zakresu lub arkusza.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

getProtectionType()

Pobiera typ obszaru chronionego, czyli RANGE lub 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());

Powrót

ProtectionType – typ obszaru chronionego: RANGE lub SHEET.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

getRange()

Zwraca zakres, który jest chroniony. Jeśli ochrona dotyczy arkusza, a nie zakresu, ta metoda zwraca zakres obejmujący cały arkusz.

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

Powrót

Range – zakres, który jest chroniony.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

getRangeName()

Zwraca nazwę chronionego zakresu, jeśli jest on powiązany z zakresem nazwanym. Zwraca null, jeśli ochrona nie jest powiązana z nazwanym zakresem. Pamiętaj, że skrypty muszą jawnie wywoływać funkcję setRangeName(rangeName), aby powiązać chroniony zakres z nazwanym zakresem. Wywołanie funkcji Range.protect() w celu utworzenia ochrony z zakresu Range, który jest nazwanym zakresem, bez wywołania funkcji setRangeName(rangeName) nie wystarczy do powiązania tych zakresów. Jednak utworzenie chronionego zakresu z nazwanego zakresu w interfejsie Arkuszy Google automatycznie je ze sobą powiąże.

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

Powrót

String|null – nazwa chronionego zakresu nazwanego lub null, jeśli chroniony zakres nie jest powiązany z zakresem nazwanym.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

getTargetAudiences()

Zwraca identyfikatory odbiorców docelowych, którzy mogą edytować zakres chroniony.

Powrót

TargetAudience[] – tablica identyfikatorów grup odbiorców.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

getUnprotectedRanges()

Pobiera tablicę niechronionych zakresów w chronionym arkuszu. Jeśli Protection obiekt odpowiada chronionemu zakresowi zamiast chronionemu arkuszowi, ta metoda zwraca pustą tablicę. Aby zmienić niechronione zakresy, użyj setUnprotectedRanges(ranges), aby ustawić nowy zakres tablicy; aby ponownie chronić cały arkusz, ustaw pustą tablicę.

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

Powrót

Range[] – tablica niechronionych zakresów w chronionym arkuszu.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

isWarningOnly()

Określa, czy obszar chroniony korzysta z ochrony „opartej na ostrzeżeniach”. Ochrona oparta na ostrzeżeniach oznacza, że każdy użytkownik może edytować dane w danym obszarze, ale przed wprowadzeniem zmian wyświetla się ostrzeżenie z prośbą o potwierdzenie edycji. Domyślnie chronione zakresy lub arkusze nie są oparte na ostrzeżeniach. Aby zmienić stan na ostrzeżenie, użyj 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);

Powrót

Boolean – true, jeśli chroniony zakres lub arkusz korzysta tylko z ochrony opartej na ostrzeżeniach.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

remove()

Usuwa ochronę zakresu lub arkusza.

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

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

removeEditor(emailAddress)

Usuwa danego użytkownika z listy edytujących chroniony arkusz lub zakres. Pamiętaj, że jeśli użytkownik jest członkiem grupy Google, która ma uprawnienia do edycji, lub jeśli wszyscy użytkownicy w domenie mają uprawnienia do edycji, użytkownik nadal będzie mógł edytować chroniony obszar. Nie można usunąć właściciela arkusza kalkulacyjnego ani bieżącego użytkownika.

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

Parametry

NazwaTypOpis
emailAddressStringAdres e-mail użytkownika, którego chcesz usunąć.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

removeEditor(user)

Usuwa danego użytkownika z listy edytujących chroniony arkusz lub zakres. Pamiętaj, że jeśli użytkownik jest członkiem grupy Google, która ma uprawnienia do edycji, lub jeśli wszyscy użytkownicy w domenie mają uprawnienia do edycji, użytkownik nadal będzie mógł edytować obszar chroniony. Nie można usunąć właściciela arkusza ani bieżącego użytkownika.

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

Parametry

NazwaTypOpis
userUserReprezentacja użytkownika do usunięcia.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, do łączenia w łańcuch

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

removeEditors(emailAddresses)

Usuwa z listy edytujących chroniony arkusz lub zakres podaną tablicę użytkowników. Pamiętaj, że jeśli którykolwiek z użytkowników jest członkiem grupy Google, która ma uprawnienia do edycji, lub jeśli wszyscy użytkownicy w domenie mają uprawnienia do edycji, ci użytkownicy nadal będą mogli edytować chroniony obszar. Nie można usunąć właściciela arkusza kalkulacyjnego ani bieżącego użytkownika.

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

Parametry

NazwaTypOpis
emailAddressesString[]Tablica adresów e-mail użytkowników do usunięcia.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, do łączenia w łańcuch

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

removeTargetAudience(audienceId)

Usuwa określoną grupę odbiorców jako edytującego zakres chroniony.

Parametry

NazwaTypOpis
audienceIdStringIdentyfikator grupy odbiorców do usunięcia.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

setDescription(description)

Ustawia opis chronionego zakresu lub arkusza.

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

Parametry

NazwaTypOpis
descriptionStringOpis chronionego zakresu lub arkusza.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

setDomainEdit(editable)

Określa, czy wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, mają uprawnienia do edytowania chronionego zakresu lub arkusza. Pamiętaj, że wszyscy użytkownicy, którzy mają wyraźne uprawnienia do edycji, mogą edytować chroniony obszar niezależnie od tego ustawienia. Zgłasza wyjątek, jeśli arkusz kalkulacyjny nie należy do domeny Google Workspace (czyli jeśli jest własnością konta gmail.com).

Parametry

NazwaTypOpis
editableBooleantrue – jeśli wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, powinni mieć uprawnienia do edytowania chronionego zakresu lub arkusza; false – w przeciwnym razie.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, do łączenia w łańcuch

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

setNamedRange(namedRange)

Powiązanie chronionego zakresu z istniejącym nazwanym zakresem. Jeśli nazwany zakres obejmuje inny obszar niż bieżący chroniony zakres, ta metoda przenosi ochronę, aby obejmowała nazwany zakres. Nazwany zakres musi znajdować się w tym samym arkuszu co bieżący chroniony zakres. Pamiętaj, że skrypty muszą jawnie wywoływać tę metodę, aby powiązać chroniony zakres z nazwanym zakresem. Wywołanie Range.protect() w celu utworzenia ochrony z zakresu Range, który jest nazwanym zakresem, bez wywołania setRangeName(rangeName) nie wystarczy do powiązania tych zakresów. Jednak utworzenie chronionego zakresu z nazwanego zakresu w interfejsie Arkuszy Google automatycznie je ze sobą powiąże.

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

Parametry

NazwaTypOpis
namedRangeNamedRangeIstniejący zakres nazwany, który ma być powiązany z zakresem chronionym.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

setRange(range)

Dostosowuje zakres, który jest chroniony. Jeśli podany zakres obejmuje inny obszar niż bieżący chroniony zakres, ta metoda przenosi ochronę na nowy zakres.

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

Parametry

NazwaTypOpis
rangeRangeNowy zakres, który ma być chroniony przed zmianami.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

setRangeName(rangeName)

Powiązanie chronionego zakresu z istniejącym nazwanym zakresem. Jeśli nazwany zakres obejmuje inny obszar niż bieżący chroniony zakres, ta metoda przenosi ochronę, aby obejmowała nazwany zakres. Nazwany zakres musi znajdować się w tym samym arkuszu co bieżący chroniony zakres. Pamiętaj, że skrypty muszą jawnie wywoływać tę metodę, aby powiązać chroniony zakres z nazwanym zakresem. Wywołanie Range.protect() w celu utworzenia ochrony z zakresu Range, który jest nazwanym zakresem, bez wywołania setRangeName(rangeName) nie wystarczy do powiązania tych zakresów. Utworzenie chronionego zakresu z nazwanego zakresu w interfejsie Arkuszy Google automatycznie je ze sobą powiąże.

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

Parametry

NazwaTypOpis
rangeNameStringNazwa zakresu nazwanego, który ma być chroniony.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, do łączenia w łańcuch

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

setUnprotectedRanges(ranges)

Usuwa ochronę z podanej tablicy zakresów w chronionym arkuszu. Zgłasza wyjątek, jeśli obiekt Protection odpowiada chronionemu zakresowi zamiast chronionemu arkuszowi lub jeśli którykolwiek z zakresów nie znajduje się w chronionym arkuszu. Aby zmienić niechronione zakresy, ustaw nową tablicę zakresów. Aby ponownie chronić cały arkusz, ustaw pustą tablicę.

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

Parametry

NazwaTypOpis
rangesRange[]Tablica zakresów, które mają pozostać niechronione w chronionym arkuszu.

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, do łączenia w łańcuch

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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

setWarningOnly(warningOnly)

Określa, czy ten chroniony zakres korzysta z ochrony „opartej na ostrzeżeniach”. Ochrona oparta na ostrzeżeniach oznacza, że każdy użytkownik może edytować dane w danym obszarze, ale przed wprowadzeniem zmian wyświetli się ostrzeżenie z prośbą o potwierdzenie edycji. Domyślnie chronione zakresy lub arkusze nie są oparte na ostrzeżeniach. Aby sprawdzić stan ostrzeżenia, użyj numeru 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());

Parametry

NazwaTypOpis
warningOnlyBoolean

Powrót

Protection – obiekt reprezentujący ustawienia ochrony, używany do łączenia w łańcuch.

Autoryzacja

Skrypty korzystające z tej metody wymagają autoryzacji z użyciem co najmniej jednego z tych zakresów:

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