Class Protection

Bảo vệ

Truy cập và sửa đổi dải ô và trang tính được bảo vệ. Dải ô được bảo vệ có thể bảo vệ một dải ô tĩnh hoặc một dải ô được đặt tên. Trang tính được bảo vệ có thể bao gồm các vùng không được bảo vệ. Đối với các trang tính tạo bằng phiên bản Google Trang tính cũ, hãy dùng lớp 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);
}

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
addEditor(emailAddress)ProtectionThêm người dùng cụ thể vào danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ.
addEditor(user)ProtectionThêm người dùng cụ thể vào danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ.
addEditors(emailAddresses)ProtectionThêm mảng người dùng đã cho vào danh sách trình chỉnh sửa của trang tính hoặc dải ô được bảo vệ.
addTargetAudience(audienceId)ProtectionThêm đối tượng mục tiêu được chỉ định làm người chỉnh sửa của dải ô được bảo vệ.
canDomainEdit()BooleanXác định xem tất cả người dùng trong miền sở hữu bảng tính có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ hay không.
canEdit()BooleanXác định xem người dùng có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ hay không.
getDescription()StringLấy nội dung mô tả của trang tính hoặc dải ô được bảo vệ.
getEditors()User[]Lấy danh sách trình chỉnh sửa cho dải ô hoặc trang tính được bảo vệ.
getProtectionType()ProtectionTypeLấy loại vùng được bảo vệ, RANGE hoặc SHEET.
getRange()RangeLấy dải ô đang được bảo vệ.
getRangeName()StringLấy tên của dải ô được bảo vệ nếu dải ô đó được liên kết với một dải ô được đặt tên.
getTargetAudiences()TargetAudience[]Trả về mã nhận dạng của các đối tượng mục tiêu có thể chỉnh sửa dải ô được bảo vệ.
getUnprotectedRanges()Range[]Lấy một mảng các dải ô không được bảo vệ trong một trang tính được bảo vệ.
isWarningOnly()BooleanXác định xem khu vực được bảo vệ có đang sử dụng chế độ bảo vệ "dựa trên cảnh báo" hay không.
remove()voidKhông bảo vệ dải ô hoặc trang tính.
removeEditor(emailAddress)ProtectionXoá người dùng cụ thể khỏi danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ.
removeEditor(user)ProtectionXoá người dùng cụ thể khỏi danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ.
removeEditors(emailAddresses)ProtectionXoá mảng người dùng đã cho khỏi danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ.
removeTargetAudience(audienceId)ProtectionXóa đối tượng mục tiêu được chỉ định với tư cách là người chỉnh sửa của dải ô được bảo vệ.
setDescription(description)ProtectionĐặt nội dung mô tả của trang tính hoặc dải ô được bảo vệ.
setDomainEdit(editable)ProtectionThiết lập liệu tất cả người dùng trong miền sở hữu bảng tính có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ hay không.
setNamedRange(namedRange)ProtectionLiên kết dải ô được bảo vệ với một dải ô được đặt tên hiện có.
setRange(range)ProtectionĐiều chỉnh dải ô đang được bảo vệ.
setRangeName(rangeName)ProtectionLiên kết dải ô được bảo vệ với một dải ô được đặt tên hiện có.
setUnprotectedRanges(ranges)ProtectionKhông bảo vệ mảng dải ô đã cho trong một trang tính được bảo vệ.
setWarningOnly(warningOnly)ProtectionThiết lập xem dải ô được bảo vệ này có đang sử dụng cơ chế bảo vệ "dựa trên cảnh báo" không.

Tài liệu chi tiết

addEditor(emailAddress)

Thêm người dùng cụ thể vào danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ. Phương thức này không tự động cấp cho người dùng quyền chỉnh sửa chính bảng tính. Để làm điều đó, hãy gọi 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());
}

Các tham số

TênLoạiNội dung mô tả
emailAddressStringĐịa chỉ email của người dùng cần thêm.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

addEditor(user)

Thêm người dùng cụ thể vào danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ. Phương thức này không tự động cấp cho người dùng quyền chỉnh sửa chính bảng tính. Để làm điều đó, hãy gọi 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());
}

Các tham số

TênLoạiNội dung mô tả
userUserĐại diện cho người dùng mà bạn cần thêm.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

addEditors(emailAddresses)

Thêm mảng người dùng đã cho vào danh sách trình chỉnh sửa của trang tính hoặc dải ô được bảo vệ. Phương thức này không tự động cấp cho người dùng quyền chỉnh sửa chính bảng tính; ngoài ra, hãy gọi 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());
}

Các tham số

TênLoạiNội dung mô tả
emailAddressesString[]Một mảng địa chỉ email của người dùng cần thêm.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

addTargetAudience(audienceId)

Thêm đối tượng mục tiêu được chỉ định làm người chỉnh sửa của dải ô được bảo vệ.

Các tham số

TênLoạiNội dung mô tả
audienceIdStringMã của đối tượng mục tiêu cần thêm.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

canDomainEdit()

Xác định xem tất cả người dùng trong miền sở hữu bảng tính có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ hay không. Gửi ngoại lệ nếu người dùng không có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ.

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

Cầu thủ trả bóng

Booleantrue nếu tất cả người dùng trong miền sở hữu bảng tính đều có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ; false nếu không có quyền này.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

canEdit()

Xác định xem người dùng có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ hay không. Chủ sở hữu bảng tính luôn có thể chỉnh sửa các dải ô và trang tính được bảo vệ.

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

Cầu thủ trả bóng

Booleantrue nếu người dùng có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ; false nếu không

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getDescription()

Lấy nội dung mô tả của trang tính hoặc dải ô được bảo vệ. Nếu không có phần mô tả nào được đặt, phương thức này sẽ trả về một chuỗi trống.

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

Cầu thủ trả bóng

String – Nội dung mô tả của trang tính hoặc dải ô được bảo vệ, hoặc một chuỗi trống nếu không đặt nội dung mô tả.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getEditors()

Lấy danh sách trình chỉnh sửa cho dải ô hoặc trang tính được bảo vệ. Gửi một ngoại lệ nếu người dùng không có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ.

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

Cầu thủ trả bóng

User[] – một mảng người dùng có quyền chỉnh sửa trang tính hoặc dải ô được bảo vệ

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getProtectionType()

Lấy loại vùng được bảo vệ, RANGE hoặc 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());

Cầu thủ trả bóng

ProtectionType – Loại khu vực được bảo vệ, RANGE hoặc SHEET.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getRange()

Lấy dải ô đang được bảo vệ. Nếu chế độ bảo vệ áp dụng cho trang tính thay vì một dải ô, phương thức này sẽ trả về một dải ô trải rộng ra toàn bộ trang tính.

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

Cầu thủ trả bóng

Range — Dải ô đang được bảo vệ.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getRangeName()

Lấy tên của dải ô được bảo vệ nếu dải ô đó được liên kết với một dải ô được đặt tên. Trả về null nếu biện pháp bảo vệ không liên kết với một dải ô được đặt tên. Lưu ý rằng tập lệnh phải gọi setRangeName(rangeName) một cách rõ ràng để liên kết một dải ô được bảo vệ với một dải ô được đặt tên; việc gọi Range.protect() để tạo biện pháp bảo vệ từ một Range xảy ra với một dải ô được đặt tên mà không cần gọi setRangeName(rangeName) là không đủ để liên kết các dải ô đó. Tuy nhiên, việc tạo một dải ô được bảo vệ từ một dải ô được đặt tên trong giao diện người dùng Google Trang tính sẽ tự động liên kết các dải ô đó.

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

Cầu thủ trả bóng

String – tên của dải ô được đặt tên được bảo vệ hoặc null nếu dải ô được bảo vệ không được liên kết với một dải ô được đặt tên

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getTargetAudiences()

Trả về mã nhận dạng của các đối tượng mục tiêu có thể chỉnh sửa dải ô được bảo vệ.

Cầu thủ trả bóng

TargetAudience[] – Mảng gồm các mã nhận dạng đối tượng mục tiêu.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

getUnprotectedRanges()

Lấy một mảng các dải ô không được bảo vệ trong một trang tính được bảo vệ. Nếu đối tượng Protection tương ứng với một dải ô được bảo vệ thay vì một trang tính được bảo vệ, thì phương thức này sẽ trả về một mảng trống. Để thay đổi các dải ô không được bảo vệ, hãy sử dụng setUnprotectedRanges(ranges) để đặt một mảng dải ô mới; để bảo vệ lại toàn bộ trang tính, hãy đặt một mảng trống.

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

Cầu thủ trả bóng

Range[] – một mảng các dải ô không được bảo vệ trong một trang tính được bảo vệ

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

isWarningOnly()

Xác định xem khu vực được bảo vệ có đang sử dụng chế độ bảo vệ "dựa trên cảnh báo" hay không. Bảo vệ dựa trên cảnh báo có nghĩa là mọi người dùng đều có thể chỉnh sửa dữ liệu trong khu vực, ngoại trừ việc chỉnh sửa sẽ nhắc nhở một cảnh báo yêu cầu người dùng xác nhận nội dung chỉnh sửa. Theo mặc định, dải ô hoặc trang tính được bảo vệ không dựa trên cảnh báo. Để thay đổi sang trạng thái cảnh báo, hãy sử dụng 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);

Cầu thủ trả bóng

Booleantrue nếu dải ô hoặc trang tính được bảo vệ chỉ sử dụng chế độ bảo vệ dựa trên cảnh báo.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

remove()

Không bảo vệ dải ô hoặc trang tính.

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

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

removeEditor(emailAddress)

Xoá người dùng cụ thể khỏi danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ. Xin lưu ý rằng nếu người dùng là thành viên của một Nhóm Google có quyền chỉnh sửa hoặc nếu tất cả người dùng trong miền đều có quyền chỉnh sửa, thì người dùng vẫn có thể chỉnh sửa khu vực được bảo vệ. Cả chủ sở hữu bảng tính và người dùng hiện tại đều không thể xoá được.

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

Các tham số

TênLoạiNội dung mô tả
emailAddressStringĐịa chỉ email của người dùng cần xoá.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

removeEditor(user)

Xoá người dùng cụ thể khỏi danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ. Xin lưu ý rằng nếu người dùng là thành viên của một Nhóm Google có quyền chỉnh sửa hoặc nếu tất cả người dùng trong miền đều có quyền chỉnh sửa, thì người dùng vẫn có thể chỉnh sửa khu vực được bảo vệ. Cả chủ sở hữu của bảng tính và người dùng hiện tại đều không thể xoá được.

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

Các tham số

TênLoạiNội dung mô tả
userUserBiểu thị người dùng cần xoá.

Cầu thủ trả bóng

Protection – đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

removeEditors(emailAddresses)

Xoá mảng người dùng đã cho khỏi danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ. Xin lưu ý rằng nếu bất kỳ người dùng nào là thành viên của một Nhóm Google có quyền chỉnh sửa, hoặc nếu tất cả người dùng trong miền đều có quyền chỉnh sửa, thì những người dùng đó vẫn có thể chỉnh sửa khu vực được bảo vệ. Cả chủ sở hữu của bảng tính và người dùng hiện tại đều không thể xoá được.

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

Các tham số

TênLoạiNội dung mô tả
emailAddressesString[]Một mảng địa chỉ email của người dùng cần xoá.

Cầu thủ trả bóng

Protection – đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

removeTargetAudience(audienceId)

Xóa đối tượng mục tiêu được chỉ định với tư cách là người chỉnh sửa của dải ô được bảo vệ.

Các tham số

TênLoạiNội dung mô tả
audienceIdStringMã của đối tượng mục tiêu cần xoá.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

setDescription(description)

Đặt nội dung mô tả của trang tính hoặc dải ô được bảo vệ.

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

Các tham số

TênLoạiNội dung mô tả
descriptionStringMô tả của trang tính hoặc dải ô được bảo vệ.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

setDomainEdit(editable)

Thiết lập liệu tất cả người dùng trong miền sở hữu bảng tính có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ hay không. Xin lưu ý rằng bất kỳ người dùng nào có quyền chỉnh sửa rõ ràng đều có thể chỉnh sửa khu vực được bảo vệ bất kể chế độ cài đặt này là gì. Gửi trường hợp ngoại lệ nếu bảng tính không thuộc một miền Google Workspace nào đó (tức là nếu bảng tính thuộc sở hữu của một tài khoản gmail.com).

Các tham số

TênLoạiNội dung mô tả
editableBooleanLà giá trị true nếu tất cả người dùng trong miền sở hữu bảng tính phải có quyền chỉnh sửa dải ô hoặc trang tính được bảo vệ; false nếu không có.

Cầu thủ trả bóng

Protection – đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

setNamedRange(namedRange)

Liên kết dải ô được bảo vệ với một dải ô được đặt tên hiện có. Nếu dải ô được đặt tên bao phủ một khu vực khác với dải ô được bảo vệ hiện tại, thì phương thức này sẽ di chuyển lớp bảo vệ để bao phủ dải ô được đặt tên. Dải ô được đặt tên phải nằm trên cùng một trang tính với dải ô được bảo vệ hiện tại. Lưu ý rằng tập lệnh phải gọi phương thức này một cách rõ ràng để liên kết một dải ô được bảo vệ với một dải ô được đặt tên. Việc gọi Range.protect() để tạo biện pháp bảo vệ từ một Range là một dải ô được đặt tên mà không cần gọi setRangeName(rangeName) là không đủ để liên kết các dải ô đó. Tuy nhiên, việc tạo một dải ô được bảo vệ từ một dải ô được đặt tên trong giao diện người dùng Google Trang tính sẽ tự động liên kết các dải ô đó.

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

Các tham số

TênLoạiNội dung mô tả
namedRangeNamedRangeDải ô được đặt tên hiện có để liên kết với dải ô được bảo vệ.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

setRange(range)

Điều chỉnh dải ô đang được bảo vệ. Nếu dải ô đã cho bao phủ một khu vực khác với dải ô được bảo vệ hiện tại, thì phương thức này sẽ di chuyển lớp bảo vệ để bao gồm dải ô mới.

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

Các tham số

TênLoạiNội dung mô tả
rangeRangeDải ô mới để bảo vệ khỏi các chỉnh sửa.

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

setRangeName(rangeName)

Liên kết dải ô được bảo vệ với một dải ô được đặt tên hiện có. Nếu dải ô được đặt tên bao phủ một khu vực khác với dải ô được bảo vệ hiện tại, thì phương thức này sẽ di chuyển lớp bảo vệ để bao phủ dải ô được đặt tên. Dải ô được đặt tên phải nằm trên cùng một trang tính với dải ô được bảo vệ hiện tại. Lưu ý rằng tập lệnh phải gọi phương thức này một cách rõ ràng để liên kết một dải ô được bảo vệ với một dải ô được đặt tên. Việc gọi Range.protect() để tạo biện pháp bảo vệ từ một Range là một dải ô được đặt tên mà không cần gọi setRangeName(rangeName) là không đủ để liên kết các dải ô đó. Tuy nhiên, việc tạo một dải ô được bảo vệ từ một dải ô được đặt tên trong giao diện người dùng Google Trang tính sẽ tự động liên kết các dải ô đó.

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

Các tham số

TênLoạiNội dung mô tả
rangeNameStringTên của dải ô được đặt tên được bảo vệ.

Cầu thủ trả bóng

Protection – đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

setUnprotectedRanges(ranges)

Không bảo vệ mảng dải ô đã cho trong một trang tính được bảo vệ. Gửi một trường hợp ngoại lệ nếu đối tượng Protection tương ứng với một dải ô được bảo vệ thay vì một trang tính được bảo vệ, hoặc nếu có bất kỳ dải ô nào không nằm trên trang tính được bảo vệ. Để thay đổi các dải ô không được bảo vệ, hãy đặt một mảng dải ô mới; để bảo vệ lại toàn bộ trang tính, hãy đặt một mảng trống.

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

Các tham số

TênLoạiNội dung mô tả
rangesRange[]Mảng dải ô cần để không được bảo vệ trong một trang tính được bảo vệ.

Cầu thủ trả bóng

Protection – đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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

setWarningOnly(warningOnly)

Thiết lập xem dải ô được bảo vệ này có đang sử dụng cơ chế bảo vệ "dựa trên cảnh báo" không. Biện pháp bảo vệ dựa trên cảnh báo có nghĩa là mọi người dùng đều có thể chỉnh sửa dữ liệu trong khu vực, ngoại trừ việc chỉnh sửa sẽ nhắc một cảnh báo yêu cầu người dùng xác nhận nội dung chỉnh sửa. Theo mặc định, các trang tính hoặc dải ô được bảo vệ không dựa trên cảnh báo. Để kiểm tra trạng thái cảnh báo, hãy dùng 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());

Các tham số

TênLoạiNội dung mô tả
warningOnlyBoolean

Cầu thủ trả bóng

Protection – Đối tượng đại diện cho các chế độ cài đặt bảo vệ, để tạo chuỗi.

Ủy quyền

Tập lệnh sử dụng phương thức này cần được uỷ quyền với một hoặc nhiều phạm vi sau:

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