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 .
Page
// 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
| Metoda | Zwracany typ | Krótki opis |
|---|---|---|
add | Protection | Dodaje podanego użytkownika do listy edytujących chroniony arkusz lub zakres. |
add | Protection | Dodaje podanego użytkownika do listy edytujących chroniony arkusz lub zakres. |
add | Protection | Dodaje podaną tablicę użytkowników do listy edytujących chroniony arkusz lub zakres. |
add | Protection | Dodaje określoną grupę odbiorców jako edytującego zakres chroniony. |
can | Boolean | Określa, czy wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, mają uprawnienia do edytowania chronionego zakresu lub arkusza. |
can | Boolean | Określa, czy użytkownik ma uprawnienia do edytowania chronionego zakresu lub arkusza. |
get | String | Pobiera opis chronionego zakresu lub arkusza. |
get | User[] | Pobiera listę edytorów chronionego zakresu lub arkusza. |
get | Protection | Pobiera typ obszaru chronionego, czyli RANGE lub SHEET. |
get | Range | Zwraca zakres, który jest chroniony. |
get | String|null | Zwraca nazwę chronionego zakresu, jeśli jest on powiązany z zakresem nazwanym. |
get | Target | Zwraca identyfikatory odbiorców docelowych, którzy mogą edytować zakres chroniony. |
get | Range[] | Pobiera tablicę niechronionych zakresów w chronionym arkuszu. |
is | Boolean | Określa, czy obszar chroniony korzysta z ochrony „opartej na ostrzeżeniach”. |
remove() | void | Usuwa ochronę zakresu lub arkusza. |
remove | Protection | Usuwa danego użytkownika z listy edytujących chroniony arkusz lub zakres. |
remove | Protection | Usuwa danego użytkownika z listy edytujących chroniony arkusz lub zakres. |
remove | Protection | Usuwa z listy edytujących chroniony arkusz lub zakres podaną tablicę użytkowników. |
remove | Protection | Usuwa określoną grupę odbiorców jako edytującego zakres chroniony. |
set | Protection | Ustawia opis chronionego zakresu lub arkusza. |
set | Protection | Określa, czy wszyscy użytkownicy w domenie, do której należy arkusz kalkulacyjny, mają uprawnienia do edytowania chronionego zakresu lub arkusza. |
set | Protection | Powiązanie chronionego zakresu z istniejącym nazwanym zakresem. |
set | Protection | Dostosowuje zakres, który jest chroniony. |
set | Protection | Powiązanie chronionego zakresu z istniejącym nazwanym zakresem. |
set | Protection | Usuwa ochronę z podanej tablicy zakresów w chronionym arkuszu. |
set | Protection | Określa, czy ten chroniony zakres korzysta z ochrony „opartej na ostrzeżeniach”. |
Szczegółowa dokumentacja
add Editor(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
| Nazwa | Typ | Opis |
|---|---|---|
email | String | Adres 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
add Editor(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
| Nazwa | Typ | Opis |
|---|---|---|
user | User | Reprezentacja 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
add Editors(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
| Nazwa | Typ | Opis |
|---|---|---|
email | String[] | 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
add Target Audience(audienceId)
Dodaje określoną grupę odbiorców jako edytującego zakres chroniony.
Parametry
| Nazwa | Typ | Opis |
|---|---|---|
audience | String | Identyfikator 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
can Domain Edit()
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
can Edit()
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
get Description()
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
get Editors()
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
get Protection Type()
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
Protection – 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
get Range()
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
get Range Name()
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ę set, 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 set 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
get Target Audiences()
Zwraca identyfikatory odbiorców docelowych, którzy mogą edytować zakres chroniony.
Powrót
Target – 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
get Unprotected Ranges()
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 set, 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
is Warning Only()
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 set.
// 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
remove Editor(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
| Nazwa | Typ | Opis |
|---|---|---|
email | String | Adres 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
remove Editor(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
| Nazwa | Typ | Opis |
|---|---|---|
user | User | Reprezentacja 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
remove Editors(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
| Nazwa | Typ | Opis |
|---|---|---|
email | String[] | 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
remove Target Audience(audienceId)
Usuwa określoną grupę odbiorców jako edytującego zakres chroniony.
Parametry
| Nazwa | Typ | Opis |
|---|---|---|
audience | String | Identyfikator 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
set Description(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
| Nazwa | Typ | Opis |
|---|---|---|
description | String | Opis 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
set Domain Edit(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
| Nazwa | Typ | Opis |
|---|---|---|
editable | Boolean | true – 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
set Named Range(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 set 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
| Nazwa | Typ | Opis |
|---|---|---|
named | Named | Istnieją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
set Range(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
| Nazwa | Typ | Opis |
|---|---|---|
range | Range | Nowy 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
set Range Name(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 set 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
| Nazwa | Typ | Opis |
|---|---|---|
range | String | Nazwa 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
set Unprotected Ranges(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
| Nazwa | Typ | Opis |
|---|---|---|
ranges | Range[] | 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
set Warning Only(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 is.
// 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
| Nazwa | Typ | Opis |
|---|---|---|
warning | Boolean |
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