Class Protection

Perlindungan

Mengakses dan mengubah rentang dan sheet yang dilindungi. Rentang yang dilindungi dapat melindungi rentang sel statis atau rentang yang diberi nama. Sheet yang dilindungi dapat mencakup wilayah yang tidak dilindungi. Untuk spreadsheet yang dibuat dengan versi Google Spreadsheet lama, gunakan class PageProtection.

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

Dokumentasi mendetail

addEditor(emailAddress)

Menambahkan pengguna tertentu ke daftar editor untuk sheet atau rentang yang dilindungi. Metode ini tidak 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

NamaTypeDeskripsi
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 yang dilindungi. Metode ini tidak 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

NamaTypeDeskripsi
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 yang ditentukan ke daftar editor untuk sheet atau rentang yang dilindungi. Metode ini tidak otomatis memberi pengguna izin untuk mengedit spreadsheet itu sendiri; untuk melakukannya, panggil Spreadsheet.addEditors(emailAddresses) juga.

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

NamaTypeDeskripsi
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 audiens target yang ditentukan sebagai editor rentang yang dilindungi.

Parameter

NamaTypeDeskripsi
audienceIdStringID audiens target 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 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());

Return

Booleantrue 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

canEdit()

Menentukan apakah pengguna memiliki izin untuk mengedit rentang atau sheet yang 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 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

getDescription()

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

Return

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

getEditors()

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.
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 yang memiliki 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

getProtectionType()

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

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 yang dilindungi jika dikaitkan dengan rentang bernama. Menampilkan null jika perlindungan tidak dikaitkan dengan rentang bernama. Perhatikan bahwa skrip harus secara eksplisit memanggil setRangeName(rangeName) untuk mengaitkan rentang yang dilindungi dengan rentang bernama; memanggil Range.protect() untuk membuat perlindungan dari Range yang kebetulan berupa rentang yang diberi nama, tanpa memanggil setRangeName(rangeName), tidak cukup untuk mengaitkannya. Namun, membuat rentang 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.
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 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

getTargetAudiences()

Menampilkan ID audiens target yang dapat mengedit rentang yang dilindungi.

Return

TargetAudience[] — Array ID audiens target.

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 yang dilindungi. Jika objek Protection terkait dengan rentang yang dilindungi, bukan sheet yang dilindungi, metode ini akan menampilkan array kosong. Untuk mengubah rentang yang tidak dilindungi, gunakan setUnprotectedRanges(ranges) guna menetapkan array rentang baru; untuk melindungi kembali 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 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

isWarningOnly()

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

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 yang 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 spreadsheet 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

NamaTypeDeskripsi
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 yang dilindungi. Perhatikan bahwa jika pengguna adalah anggota Google Grup yang memiliki izin edit, atau jika semua pengguna di domain memiliki izin edit, pengguna juga masih dapat mengedit area yang dilindungi. Baik pemilik spreadsheet 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');

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

NamaTypeDeskripsi
userUserRepresentasi pengguna yang akan dihapus.

Return

Protection — objek yang mewakili setelan perlindungan, untuk 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

removeEditors(emailAddresses)

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

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

Return

Protection — objek yang mewakili setelan perlindungan, untuk 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

removeTargetAudience(audienceId)

Menghapus audiens target yang ditentukan sebagai editor rentang yang dilindungi.

Parameter

NamaTypeDeskripsi
audienceIdStringID audiens target 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 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

NamaTypeDeskripsi
descriptionStringDeskripsi rentang atau 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

setDomainEdit(editable)

Menetapkan apakah semua pengguna dalam 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

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

Return

Protection — objek yang mewakili setelan perlindungan, untuk 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

setNamedRange(namedRange)

Mengaitkan rentang yang dilindungi dengan rentang bernama yang ada. Jika rentang bernama mencakup area yang berbeda dari rentang yang dilindungi saat ini, metode ini akan memindahkan perlindungan untuk mencakup 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 yang 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 dilindungi 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

NamaTypeDeskripsi
namedRangeNamedRangeRentang bernama yang ada untuk dikaitkan dengan rentang 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

setRange(range)

Menyesuaikan rentang yang dilindungi. Jika rentang yang ditentukan mencakup area yang berbeda dari rentang yang dilindungi saat ini, metode ini akan memindahkan perlindungan ke mencakup rentang yang 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

NamaTypeDeskripsi
rangeRangeRentang baru untuk melindungi dari pengeditan.

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 yang dilindungi dengan rentang bernama yang ada. Jika rentang bernama mencakup area yang berbeda dari rentang yang dilindungi saat ini, metode ini akan memindahkan perlindungan untuk mencakup 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 yang 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 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.
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

NamaTypeDeskripsi
rangeNameStringNama rentang bernama yang akan dilindungi.

Return

Protection — objek yang mewakili setelan perlindungan, untuk 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

setUnprotectedRanges(ranges)

Tidak melindungi array yang ditentukan dalam sheet yang dilindungi. Menampilkan pengecualian jika objek Protection sesuai dengan rentang yang dilindungi, bukan sheet yang dilindungi atau jika salah satu rentang tidak berada dalam sheet yang dilindungi. Untuk mengubah rentang yang tidak dilindungi, tetapkan array rentang baru; untuk melindungi kembali 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

NamaTypeDeskripsi
rangesRange[]Array rentang yang tidak dilindungi dalam sheet yang dilindungi.

Return

Protection — objek yang mewakili setelan perlindungan, untuk 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

setWarningOnly(warningOnly)

Menetapkan apakah rentang yang dilindungi ini menggunakan perlindungan "berbasis peringatan". Perlindungan berbasis peringatan berarti bahwa setiap pengguna dapat mengedit data di area, kecuali pengeditan akan menampilkan 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

NamaTypeDeskripsi
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