Class Protection

Perlindungan

Mengakses dan mengubah rentang dan sheet dilindungi. Rentang terlindungi dapat melindungi rentang sel statis atau rentang bernama. Sheet yang dilindungi mungkin menyertakan bagian yang tidak dilindungi. Untuk spreadsheet yang dibuat dengan Google Spreadsheet versi lama, gunakan class PageProtection sebagai gantinya.

// Protect range A1:B10, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var 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.
var 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.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Protect the active sheet, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var 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.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

Metode

MetodeJenis hasil yang ditampilkanDeskripsi singkat
addEditor(emailAddress)ProtectionMenambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang dilindungi.
addEditor(user)ProtectionMenambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang dilindungi.
addEditors(emailAddresses)ProtectionMenambahkan array pengguna tertentu ke daftar editor untuk sheet atau rentang dilindungi.
addTargetAudience(audienceId)ProtectionMenambahkan target audiens yang ditentukan sebagai editor rentang dilindungi.
canDomainEdit()BooleanMenentukan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet dilindungi.
canEdit()BooleanMenentukan apakah pengguna memiliki izin untuk mengedit rentang atau sheet dilindungi.
getDescription()StringMendapatkan deskripsi rentang atau sheet dilindungi.
getEditors()User[]Mendapatkan daftar editor untuk sheet atau rentang dilindungi.
getProtectionType()ProtectionTypeMendapatkan jenis area yang dilindungi, RANGE atau SHEET.
getRange()RangeMendapatkan rentang yang dilindungi.
getRangeName()StringMendapatkan nama rentang dilindungi jika dikaitkan dengan rentang bernama.
getTargetAudiences()TargetAudience[]Menampilkan ID target audiens yang dapat mengedit rentang dilindungi.
getUnprotectedRanges()Range[]Mendapatkan array rentang yang tidak dilindungi dalam sheet dilindungi.
isWarningOnly()BooleanMenentukan apakah area yang dilindungi menggunakan perlindungan "berbasis peringatan".
remove()voidTidak melindungi rentang atau sheet.
removeEditor(emailAddress)ProtectionMenghapus pengguna tertentu dari daftar editor untuk sheet atau rentang dilindungi.
removeEditor(user)ProtectionMenghapus pengguna tertentu dari daftar editor untuk sheet atau rentang dilindungi.
removeEditors(emailAddresses)ProtectionMenghapus array pengguna tertentu dari daftar editor untuk sheet atau rentang dilindungi.
removeTargetAudience(audienceId)ProtectionMenghapus target audiens tertentu sebagai editor rentang dilindungi.
setDescription(description)ProtectionMenetapkan deskripsi sheet atau rentang dilindungi.
setDomainEdit(editable)ProtectionMenetapkan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet yang dilindungi.
setNamedRange(namedRange)ProtectionMengaitkan rentang dilindungi dengan rentang bernama yang sudah ada.
setRange(range)ProtectionMenyesuaikan rentang yang sedang dilindungi.
setRangeName(rangeName)ProtectionMengaitkan rentang dilindungi dengan rentang bernama yang sudah ada.
setUnprotectedRanges(ranges)ProtectionTidak melindungi array rentang yang diberikan dalam sheet yang dilindungi.
setWarningOnly(warningOnly)ProtectionMenetapkan apakah rentang terlindungi ini menggunakan perlindungan "berbasis peringatan" atau tidak.

Dokumentasi mendetail

addEditor(emailAddress)

Menambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang dilindungi. Metode ini tidak secara otomatis memberikan izin kepada pengguna untuk mengedit spreadsheet itu sendiri. Untuk melakukannya, 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

NamaJenisDeskripsi
emailAddressStringAlamat email pengguna yang akan ditambahkan.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

addEditor(user)

Menambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang dilindungi. Metode ini tidak secara otomatis memberikan izin kepada pengguna untuk mengedit spreadsheet itu sendiri. Untuk melakukannya, 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

NamaJenisDeskripsi
userUserRepresentasi pengguna yang akan ditambahkan.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

addEditors(emailAddresses)

Menambahkan array pengguna tertentu ke daftar editor untuk sheet atau rentang dilindungi. Metode ini tidak secara otomatis memberikan izin kepada pengguna untuk mengedit spreadsheet itu sendiri; untuk melakukannya, 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

NamaJenisDeskripsi
emailAddressesString[]Array alamat email pengguna yang akan ditambahkan.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

addTargetAudience(audienceId)

Menambahkan target audiens yang ditentukan sebagai editor rentang dilindungi.

Parameter

NamaJenisDeskripsi
audienceIdStringID target audiens yang akan ditambahkan.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

canDomainEdit()

Menentukan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet dilindungi. Menampilkan pengecualian jika pengguna tidak memiliki izin untuk mengedit rentang atau sheet 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());

Return

Booleantrue jika semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet 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

canEdit()

Menentukan apakah pengguna memiliki izin untuk mengedit rentang atau sheet dilindungi. Pemilik spreadsheet selalu dapat mengedit rentang dan sheet yang dilindungi.

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

Return

Booleantrue jika pengguna memiliki izin untuk mengedit rentang atau sheet 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

getDescription()

Mendapatkan deskripsi rentang atau sheet 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);

Return

String — Deskripsi sheet atau rentang 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

getEditors()

Mendapatkan daftar editor untuk sheet atau rentang dilindungi. Menampilkan pengecualian jika pengguna tidak memiliki izin untuk mengedit rentang atau sheet dilindungi.

// Protect the active sheet, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var 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.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

Return

User[] — array pengguna dengan izin untuk mengedit rentang atau sheet 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

getProtectionType()

Mendapatkan jenis area yang dilindungi, RANGE atau 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());

Return

ProtectionType — Jenis kawasan yang dilindungi, 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

getRange()

Mendapatkan rentang yang dilindungi. Jika perlindungan berlaku pada 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());

Return

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

getRangeName()

Mendapatkan nama rentang dilindungi jika dikaitkan dengan rentang bernama. Menampilkan null jika perlindungan tidak terkait dengan rentang bernama. Perhatikan bahwa skrip harus secara eksplisit memanggil setRangeName(rangeName) untuk mengaitkan rentang dilindungi dengan rentang bernama; memanggil Range.protect() untuk membuat perlindungan dari Range yang kebetulan berupa rentang bernama, tanpa memanggil setRangeName(rangeName), 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.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var 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.

Return

String — nama rentang bernama yang dilindungi, atau null jika rentang dilindungi tidak terkait 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

getTargetAudiences()

Menampilkan ID target audiens yang dapat mengedit rentang dilindungi.

Return

TargetAudience[] — 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

getUnprotectedRanges()

Mendapatkan array rentang yang tidak dilindungi dalam sheet dilindungi. Jika objek Protection terkait dengan rentang dilindungi, bukan sheet yang dilindungi, metode ini akan menampilkan array kosong. Untuk mengubah rentang yang tidak dilindungi, gunakan setUnprotectedRanges(ranges) 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.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect();
var unprotected = protection.getUnprotectedRanges();
unprotected.push(sheet.getRange('E2:F5'));
protection.setUnprotectedRanges(unprotected);

Return

Range[] — array rentang yang tidak dilindungi dalam sheet 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

isWarningOnly()

Menentukan apakah area yang dilindungi menggunakan perlindungan "berbasis peringatan". Perlindungan berbasis peringatan berarti setiap pengguna dapat mengedit data di area tersebut, kecuali saat pengeditan akan menampilkan peringatan yang meminta pengguna untuk mengonfirmasi hasil edit. Secara default, rentang atau sheet dilindungi tidak berbasis peringatan. Untuk beralih ke status peringatan, gunakan 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);

Return

Booleantrue jika rentang atau sheet 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()

Tidak melindungi rentang atau sheet.

// Remove all range protections in the spreadsheet that the user has permission to edit.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove sheet protection from the active sheet, if the user has permission to edit it.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection && 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

removeEditor(emailAddress)

Menghapus pengguna tertentu dari daftar editor untuk sheet atau rentang dilindungi. Perhatikan bahwa jika pengguna adalah anggota Google Grup yang memiliki izin edit, atau jika semua pengguna di domain memiliki izin edit, pengguna masih dapat mengedit area yang dilindungi. Baik pemilik {i>spreadsheet<i} maupun 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

NamaJenisDeskripsi
emailAddressStringAlamat email pengguna yang akan dihapus.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

removeEditor(user)

Menghapus pengguna tertentu dari daftar editor untuk sheet atau rentang dilindungi. Perhatikan bahwa jika pengguna adalah anggota Google Grup yang memiliki izin edit, atau jika semua pengguna di domain memiliki izin edit, pengguna tersebut juga dapat mengedit area yang dilindungi. Pemilik spreadsheet atau 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

NamaJenisDeskripsi
userUserRepresentasi pengguna yang akan dihapus.

Return

Protection — objek yang mewakili setelan perlindungan untuk perantaian

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

removeEditors(emailAddresses)

Menghapus array pengguna tertentu dari daftar editor untuk sheet atau rentang dilindungi. Perhatikan bahwa jika ada pengguna yang merupakan anggota Google Grup yang memiliki izin edit, atau jika semua pengguna di domain memiliki izin edit, pengguna tersebut masih dapat mengedit area yang dilindungi. Pemilik spreadsheet atau pengguna saat ini tidak dapat dihapus.

// Protect the active sheet, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var 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.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

Parameter

NamaJenisDeskripsi
emailAddressesString[]Array alamat email pengguna yang akan dihapus.

Return

Protection — objek yang mewakili setelan perlindungan untuk perantaian

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

removeTargetAudience(audienceId)

Menghapus target audiens tertentu sebagai editor rentang dilindungi.

Parameter

NamaJenisDeskripsi
audienceIdStringID target audiens yang akan dihapus.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

setDescription(description)

Menetapkan deskripsi sheet atau rentang 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

NamaJenisDeskripsi
descriptionStringDeskripsi rentang atau sheet dilindungi.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

setDomainEdit(editable)

Menetapkan apakah semua pengguna di domain yang memiliki spreadsheet memiliki izin untuk mengedit rentang atau sheet yang dilindungi. Perhatikan bahwa setiap pengguna yang memiliki izin edit eksplisit dapat mengedit area yang dilindungi terlepas dari setelan ini. Menampilkan pengecualian jika spreadsheet bukan milik domain Google Workspace (yaitu, jika dimiliki oleh akun gmail.com).

Parameter

NamaJenisDeskripsi
editableBooleantrue jika semua pengguna di domain yang memiliki spreadsheet harus memiliki izin untuk mengedit rentang atau sheet dilindungi; false jika tidak.

Return

Protection — objek yang mewakili setelan perlindungan untuk perantaian

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

setNamedRange(namedRange)

Mengaitkan rentang dilindungi dengan rentang bernama yang sudah ada. Jika rentang bernama mencakup area yang berbeda dari rentang dilindungi saat ini, metode ini akan memindahkan perlindungan untuk menutupi rentang bernama. Rentang bernama harus berada di sheet yang sama dengan rentang dilindungi saat ini. Perhatikan bahwa skrip harus secara eksplisit memanggil metode ini untuk mengaitkan rentang dilindungi dengan rentang bernama; memanggil Range.protect() untuk membuat perlindungan dari Range yang kebetulan berupa rentang bernama, tanpa memanggil setRangeName(rangeName), 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

NamaJenisDeskripsi
namedRangeNamedRangeRentang bernama yang sudah ada untuk dikaitkan dengan rentang dilindungi.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

setRange(range)

Menyesuaikan rentang yang sedang dilindungi. Jika rentang yang diberikan mencakup area yang berbeda dari rentang 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

NamaJenisDeskripsi
rangeRangeRentang baru yang akan dilindungi dari hasil edit.

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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

setRangeName(rangeName)

Mengaitkan rentang dilindungi dengan rentang bernama yang sudah ada. Jika rentang bernama mencakup area yang berbeda dari rentang dilindungi saat ini, metode ini akan memindahkan perlindungan untuk menutupi rentang bernama. Rentang bernama harus berada di sheet yang sama dengan rentang dilindungi saat ini. Perhatikan bahwa skrip harus secara eksplisit memanggil metode ini untuk mengaitkan rentang dilindungi dengan rentang bernama; memanggil Range.protect() untuk membuat perlindungan dari Range yang kebetulan berupa rentang bernama, tanpa memanggil setRangeName(rangeName), 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.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var 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

NamaJenisDeskripsi
rangeNameStringNama rentang bernama yang akan dilindungi.

Return

Protection — objek yang mewakili setelan perlindungan untuk perantaian

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

setUnprotectedRanges(ranges)

Tidak melindungi array rentang yang diberikan dalam sheet yang dilindungi. Menampilkan pengecualian jika objek Protection sesuai dengan rentang dilindungi, bukan sheet dilindungi, atau jika rentang tersebut tidak berada di sheet 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.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');
var 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.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

Parameter

NamaJenisDeskripsi
rangesRange[]Array rentang yang dibiarkan tidak dilindungi dalam sheet yang dilindungi.

Return

Protection — objek yang mewakili setelan perlindungan untuk perantaian

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

setWarningOnly(warningOnly)

Menetapkan apakah rentang terlindungi 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 hasil edit. Secara default, rentang atau sheet yang dilindungi tidak berbasis peringatan. Untuk memeriksa status peringatan, gunakan 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());

Parameter

NamaJenisDeskripsi
warningOnlyBoolean

Return

Protection — Objek yang mewakili setelan perlindungan untuk perantaian.

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