Mengakses dan mengubah rentang dan sheet yang dilindungi. Rentang yang dilindungi dapat melindungi rentang sel statis atau rentang bernama. Sheet yang dilindungi dapat mencakup wilayah yang tidak dilindungi. Untuk spreadsheet yang dibuat dengan Google Spreadsheet versi lama, gunakan class .
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); }
Metode
| Metode | Jenis hasil yang ditampilkan | Deskripsi singkat |
|---|---|---|
add | Protection | Menambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang yang dilindungi. |
add | Protection | Menambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang yang dilindungi. |
add | Protection | Menambahkan array pengguna tertentu ke daftar editor untuk sheet atau rentang yang dilindungi. |
add | Protection | Menambahkan target audiens tertentu sebagai editor rentang yang dilindungi. |
can | Boolean | Menentukan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet yang dilindungi. |
can | Boolean | Menentukan apakah pengguna memiliki izin untuk mengedit rentang atau sheet terlindungi. |
get | String | Mendapatkan deskripsi rentang atau sheet yang dilindungi. |
get | User[] | Mendapatkan daftar editor untuk rentang atau sheet yang dilindungi. |
get | Protection | Mendapatkan jenis area terlindungi, baik RANGE maupun SHEET. |
get | Range | Mendapatkan rentang yang dilindungi. |
get | String|null | Mendapatkan nama rentang yang dilindungi jika dikaitkan dengan rentang bernama. |
get | Target | Menampilkan ID target audiens yang dapat mengedit rentang terlindungi. |
get | Range[] | Mendapatkan array rentang yang tidak dilindungi dalam sheet yang dilindungi. |
is | Boolean | Menentukan apakah area yang dilindungi menggunakan perlindungan "berbasis peringatan". |
remove() | void | Membatalkan perlindungan rentang atau sheet. |
remove | Protection | Menghapus pengguna tertentu dari daftar editor untuk sheet atau rentang yang dilindungi. |
remove | Protection | Menghapus pengguna tertentu dari daftar editor untuk sheet atau rentang yang dilindungi. |
remove | Protection | Menghapus array pengguna tertentu dari daftar editor untuk sheet atau rentang yang dilindungi. |
remove | Protection | Menghapus audiens target tertentu sebagai editor rentang yang dilindungi. |
set | Protection | Menetapkan deskripsi rentang atau sheet yang dilindungi. |
set | Protection | Menetapkan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet yang dilindungi. |
set | Protection | Mengaitkan rentang terlindungi dengan rentang bernama yang ada. |
set | Protection | Menyesuaikan rentang yang dilindungi. |
set | Protection | Mengaitkan rentang terlindungi dengan rentang bernama yang ada. |
set | Protection | Membatalkan perlindungan array rentang tertentu dalam sheet yang dilindungi. |
set | Protection | Menetapkan apakah rentang yang dilindungi ini menggunakan perlindungan "berbasis peringatan" atau tidak. |
Dokumentasi mendetail
add Editor(emailAddress)
Menambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang yang dilindungi. Metode ini tidak
memberikan izin kepada pengguna secara otomatis untuk mengedit spreadsheet itu sendiri; untuk melakukannya
juga, panggil 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()); }
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
email | String | Alamat email pengguna yang akan ditambahkan. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Editor(user)
Menambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang yang dilindungi. Metode ini tidak
memberikan izin kepada pengguna secara otomatis untuk mengedit spreadsheet itu sendiri; untuk melakukannya
juga, panggil 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()); }
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
user | User | Representasi pengguna yang akan ditambahkan. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Editors(emailAddresses)
Menambahkan array pengguna tertentu ke daftar editor untuk sheet atau rentang yang dilindungi. Metode ini tidak otomatis memberi pengguna izin untuk mengedit spreadsheet itu sendiri; untuk melakukannya juga, panggil 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()); }
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
email | String[] | Array alamat email pengguna yang akan ditambahkan. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Target Audience(audienceId)
Menambahkan target audiens tertentu sebagai editor rentang yang dilindungi.
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
audience | String | ID target audiens yang akan ditambahkan. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
can Domain Edit()
Menentukan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet yang dilindungi. Menampilkan pengecualian jika pengguna tidak memiliki izin untuk mengedit rentang atau sheet yang dilindungi.
// 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());
Pulang pergi
Boolean — true jika semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk
mengedit rentang atau sheet yang dilindungi; false jika tidak.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
can Edit()
Menentukan apakah pengguna memiliki izin untuk mengedit rentang atau sheet terlindungi. Pemilik spreadsheet selalu dapat mengedit rentang dan sheet yang dilindungi.
// 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(); } }
Pulang pergi
Boolean — true jika pengguna memiliki izin untuk mengedit rentang atau sheet yang dilindungi; false jika tidak
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Description()
Mendapatkan deskripsi rentang atau sheet yang dilindungi. Jika tidak ada deskripsi yang ditetapkan, metode ini akan menampilkan string kosong.
// 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);
Pulang pergi
String — Deskripsi rentang atau sheet yang dilindungi, atau string kosong jika tidak ada deskripsi yang ditetapkan.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Editors()
Mendapatkan daftar editor untuk rentang atau sheet yang dilindungi. Menampilkan pengecualian jika pengguna tidak memiliki izin untuk mengedit rentang atau sheet yang dilindungi.
// 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); }
Pulang pergi
User[] — array pengguna dengan izin untuk mengedit rentang atau sheet yang dilindungi
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Protection Type()
Mendapatkan jenis area terlindungi, baik RANGE maupun 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());
Pulang pergi
Protection — Jenis kawasan lindung, RANGE atau SHEET.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Range()
Mendapatkan rentang yang dilindungi. Jika perlindungan berlaku untuk sheet, bukan rentang, metode ini akan menampilkan rentang yang mencakup seluruh 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'); // 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());
Pulang pergi
Range — Rentang yang dilindungi.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Range Name()
Mendapatkan nama rentang yang dilindungi jika dikaitkan dengan rentang bernama. Menampilkan null jika perlindungan tidak dikaitkan dengan rentang bernama. Perhatikan bahwa skrip harus secara eksplisit
memanggil set untuk mengaitkan rentang terlindungi dengan rentang bernama; memanggil
Range.protect() untuk membuat perlindungan dari Range yang kebetulan merupakan
rentang bernama, tanpa memanggil set, tidak cukup untuk mengaitkannya. Namun, membuat rentang yang dilindungi dari rentang bernama di UI Google Spreadsheet akan mengaitkannya secara otomatis.
// 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.
Pulang pergi
String|null — nama rentang bernama yang dilindungi, atau null jika rentang yang dilindungi tidak
dikaitkan dengan rentang bernama
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Target Audiences()
Menampilkan ID target audiens yang dapat mengedit rentang terlindungi.
Pulang pergi
Target — Array ID target audiens.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Unprotected Ranges()
Mendapatkan array rentang yang tidak dilindungi dalam sheet yang dilindungi. Jika objek Protection sesuai dengan rentang terlindungi, bukan sheet terlindungi, metode ini akan menampilkan array kosong. Untuk mengubah rentang yang tidak dilindungi, gunakan set untuk menetapkan
array rentang baru; untuk melindungi ulang seluruh sheet, tetapkan array kosong.
// 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);
Pulang pergi
Range[] — array rentang yang tidak dilindungi dalam sheet yang dilindungi
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
is Warning Only()
Menentukan apakah area yang dilindungi menggunakan perlindungan "berbasis peringatan". Perlindungan berbasis peringatan
berarti setiap pengguna dapat mengedit data di area tersebut, kecuali pengeditan akan memunculkan peringatan yang meminta
pengguna untuk mengonfirmasi pengeditan. Secara default, rentang atau sheet yang dilindungi tidak berbasis peringatan. Untuk
beralih ke status peringatan, gunakan 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);
Pulang pergi
Boolean — true jika rentang atau sheet yang dilindungi hanya menggunakan perlindungan berbasis peringatan.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove()
Membatalkan perlindungan rentang atau sheet.
// 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(); }
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editor(emailAddress)
Menghapus pengguna tertentu dari daftar editor untuk sheet atau rentang yang dilindungi. Perhatikan bahwa jika pengguna adalah anggota Grup Google yang memiliki izin edit, atau jika semua pengguna di domain memiliki izin edit, pengguna tersebut masih dapat mengedit area yang dilindungi. Pemilik spreadsheet dan pengguna saat ini tidak dapat dihapus.
// 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()); }
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
email | String | Alamat email pengguna yang akan dihapus. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editor(user)
Menghapus pengguna tertentu dari daftar editor untuk sheet atau rentang yang dilindungi. Perhatikan bahwa jika pengguna adalah anggota Grup Google yang memiliki izin edit, atau jika semua pengguna di domain memiliki izin edit, pengguna tersebut masih dapat mengedit area yang dilindungi. Pemilik spreadsheet dan pengguna saat ini tidak dapat dihapus.
// 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()); }
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
user | User | Representasi pengguna yang akan dihapus. |
Pulang pergi
Protection — objek yang mewakili setelan perlindungan, untuk pengaitan
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editors(emailAddresses)
Menghapus array pengguna tertentu dari daftar editor untuk sheet atau rentang yang dilindungi. Perhatikan bahwa jika salah satu pengguna adalah anggota Grup Google yang memiliki izin edit, atau jika semua pengguna di domain memiliki izin edit, pengguna tersebut tetap dapat mengedit area yang dilindungi. Pemilik spreadsheet maupun pengguna saat ini tidak dapat dihapus.
// 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); }
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
email | String[] | Array alamat email pengguna yang akan dihapus. |
Pulang pergi
Protection — objek yang mewakili setelan perlindungan, untuk pengaitan
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Target Audience(audienceId)
Menghapus audiens target tertentu sebagai editor rentang yang dilindungi.
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
audience | String | ID audiens target yang akan dihapus. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Description(description)
Menetapkan deskripsi rentang atau sheet yang dilindungi.
// 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);
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
description | String | Deskripsi rentang atau sheet yang dilindungi. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Domain Edit(editable)
Menetapkan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet yang dilindungi. Perhatikan bahwa semua pengguna yang memiliki izin edit eksplisit dapat mengedit area terlindungi, terlepas dari setelan ini. Menampilkan pengecualian jika spreadsheet tidak termasuk dalam domain Google Workspace (yaitu, jika dimiliki oleh akun gmail.com).
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
editable | Boolean | true jika semua pengguna di domain yang memiliki spreadsheet harus memiliki
izin untuk mengedit rentang atau sheet yang dilindungi; false jika tidak. |
Pulang pergi
Protection — objek yang mewakili setelan perlindungan, untuk pengaitan
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Named Range(namedRange)
Mengaitkan rentang terlindungi dengan rentang bernama yang ada. Jika rentang bernama mencakup area yang berbeda dari rentang terlindungi saat ini, metode ini akan memindahkan perlindungan untuk mencakup rentang bernama tersebut. Rentang bernama harus berada di sheet yang sama dengan rentang terlindungi
saat ini. Perhatikan bahwa skrip harus secara eksplisit memanggil metode ini untuk mengaitkan rentang yang dilindungi dengan rentang bernama; memanggil Range.protect() untuk membuat perlindungan dari Range
yang kebetulan merupakan rentang bernama, tanpa memanggil set, tidak
cukup untuk mengaitkannya. Namun, membuat rentang terlindungi dari rentang bernama di UI Google Spreadsheet akan mengaitkannya secara otomatis.
// 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());
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
named | Named | Rentang bernama yang ada untuk dikaitkan dengan rentang yang dilindungi. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Range(range)
Menyesuaikan rentang yang dilindungi. Jika rentang yang diberikan mencakup area yang berbeda dari rentang yang dilindungi saat ini, metode ini akan memindahkan perlindungan untuk mencakup rentang baru.
// 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());
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
range | Range | Rentang baru yang dilindungi dari pengeditan. |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Range Name(rangeName)
Mengaitkan rentang terlindungi dengan rentang bernama yang ada. Jika rentang bernama mencakup area yang berbeda dari rentang terlindungi saat ini, metode ini akan memindahkan perlindungan untuk mencakup rentang bernama tersebut. Rentang bernama harus berada di sheet yang sama dengan rentang terlindungi
saat ini. Perhatikan bahwa skrip harus secara eksplisit memanggil metode ini untuk mengaitkan rentang yang dilindungi dengan rentang bernama; memanggil Range.protect() untuk membuat perlindungan dari Range
yang kebetulan merupakan rentang bernama, tanpa memanggil set, tidak
cukup untuk mengaitkannya. Namun, membuat rentang terlindungi dari rentang bernama di UI Google Spreadsheet akan mengaitkannya secara otomatis.
// 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.
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
range | String | Nama rentang bernama yang akan dilindungi. |
Pulang pergi
Protection — objek yang mewakili setelan perlindungan, untuk pengaitan
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Unprotected Ranges(ranges)
Membatalkan perlindungan array rentang tertentu dalam sheet yang dilindungi. Menampilkan pengecualian jika objek
Protection sesuai dengan rentang yang dilindungi, bukan sheet yang dilindungi, atau jika
ada rentang yang tidak berada di sheet yang dilindungi. Untuk mengubah rentang yang tidak dilindungi, tetapkan array rentang baru; untuk melindungi ulang seluruh sheet, tetapkan array kosong.
// 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); }
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
ranges | Range[] | Array rentang yang tidak dilindungi dalam sheet yang dilindungi. |
Pulang pergi
Protection — objek yang mewakili setelan perlindungan, untuk pengaitan
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Warning Only(warningOnly)
Menetapkan apakah rentang yang dilindungi ini menggunakan perlindungan "berbasis peringatan" atau tidak. Perlindungan
berbasis peringatan berarti setiap pengguna dapat mengedit data di area tersebut, kecuali pengeditan akan memunculkan peringatan
yang meminta pengguna untuk mengonfirmasi pengeditan. Secara default, rentang atau sheet yang dilindungi tidak berbasis peringatan. Untuk memeriksa status peringatan, gunakan 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());
Parameter
| Nama | Jenis | Deskripsi |
|---|---|---|
warning | Boolean |
Pulang pergi
Protection — Objek yang mewakili setelan perlindungan, untuk pembuatan rantai.
Otorisasi
Skrip yang menggunakan metode ini memerlukan otorisasi dengan satu atau beberapa cakupan berikut:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets