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ệ dải ô hoặc 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ệ. Cho bảng tính được tạo bằng phiên bản Google Trang tính cũ hơn, hãy sử dụng PageProtection thay thế.

// 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ứcLoại 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 cho 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 cho 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 người chỉnh sửa cho 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 hay không dải ô hoặc trang tính được bảo vệ.
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 dải ô hoặc trang tính được bảo vệ.
getEditors()User[]Lấy danh sách người 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 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 "dựa trên cảnh báo" hay không bảo vệ thiết bị.
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 cho trang tính hoặc dải ô được bảo vệ.
removeTargetAudience(audienceId)ProtectionXoá đối tượng mục tiêu được chỉ định với vai trò người chỉnh sửa của dải ô được bảo vệ.
setDescription(description)ProtectionĐặt mô tả của dải ô hoặc trang tính được bảo vệ.
setDomainEdit(editable)ProtectionĐặt xem 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ệ.
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)ProtectionĐặt xem dải ô được bảo vệ này có sử dụng "dựa trên cảnh báo" hay không bảo vệ thiết bị.

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 cho 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 bảng tính; để làm việc đó trong ngoài ra, 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());
}

Thông số

TênLoạiMô 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 yêu cầu ủy 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 cho 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 bảng tính; để làm việc đó trong ngoài ra, 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());
}

Thông số

TênLoạiMô tả
userUserẢnh đại diện cho 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 yêu cầu ủy 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 người chỉnh sửa cho trang tính hoặc dải ô được bảo vệ. Chiến dịch 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; việc cần làm 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());
}

Thông số

TênLoạiMô tả
emailAddressesString[]Một mảng địa chỉ email của những 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 yêu cầu ủy 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ệ.

Thông số

TênLoạiMô 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 yêu cầu ủy 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 hay không 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ệ.

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

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy 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. Chiến lược phát hành đĩa đơn chủ sở hữu bảng tính luôn có thể chỉnh sửa 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 yêu cầu ủy 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 dải ô hoặc trang tính được bảo vệ. Nếu bạn không đặt nội dung mô tả, thì 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 — Mô tả của dải ô hoặc trang tính được bảo vệ hoặc một chuỗi trống nếu không có mô tả đã được đặt.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy 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 người 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 dải ô hoặc trang tính được bảo vệ

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy 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 yêu cầu ủy 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 biện pháp bảo vệ áp dụng cho trang tính thay vì dải ô, phương thức này sẽ trả về một dải ô trải dài 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 yêu cầu ủy 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 dải ô được đặt tên. Lưu ý rằng tập lệnh phải rõ ràng gọi setRangeName(rangeName) để liên kết một dải ô được bảo vệ với một dải ô được đặt tên; đang gọi Range.protect() để tạo một biện pháp bảo vệ khỏi Range (tình cờ là một) dải ô được đặt tên, nếu không gọi setRangeName(rangeName), là không đủ để liên kết chúng. 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ẽ liên kết chúng một cách tự động.

// 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 phải là được liên kết với dải ô được đặt tên

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy 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ột dãy mã nhận dạng của đối tượng mục tiêu.

Ủy quyền

Tập lệnh sử dụng phương thức này yêu cầu ủy 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 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 dải ô được bảo vệ thay vì trang tính được bảo vệ, phương thức này sẽ trả về một giá trị trống . Để thay đổi các dải không được bảo vệ, hãy dùng setUnprotectedRanges(ranges) để đặt giá trị 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 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 yêu cầu ủy 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 "dựa trên cảnh báo" hay không bảo vệ thiết bị. 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 lời nhắc, một cảnh báo yêu cầu 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. Người nhận chuyển 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 biện pháp 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 yêu cầu ủy 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 yêu cầu ủy 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ệ. Lưu ý rằng nếu người dùng là thành viên của một nhóm trên Google Groups có quyền chỉnh sửa hoặc nếu tất cả người dùng trong miền có quyền chỉnh sửa, người dùng vẫn có thể chỉnh sửa khu vực được bảo vệ. Không phải chủ sở hữu của bảng tính cũng như người dùng hiện tại đều có thể bị xoá.

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

Thông số

TênLoạiMô 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 yêu cầu ủy 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ệ. Lưu ý rằng nếu người dùng là thành viên của một nhóm trên Google Groups có quyền chỉnh sửa hoặc nếu tất cả người dùng trong miền có quyền chỉnh sửa, người dùng vẫn có thể chỉnh sửa khu vực được bảo vệ. Cả hai chủ sở hữu của bảng tính cũng như người dùng hiện tại đều có thể bị xoá.

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

Thông số

TênLoạiMô tả
userUserHình ảnh đại diện cho 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 yêu cầu ủy 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 cho 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 trên Google Groups có quyền chỉnh sửa hoặc nếu tất cả Người dùng trong miền có quyền chỉnh sửa, những người dùng đó vẫn có thể chỉnh sửa . Cả chủ sở hữu của bảng tính lẫn người dùng hiện tại đều không thể bị xoá.

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

Thông số

TênLoạiMô tả
emailAddressesString[]Một mảng địa chỉ email của những người dùng mà bạn muố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 yêu cầu ủy 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)

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

Thông số

TênLoạiMô 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 yêu cầu ủy 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 mô tả củ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 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);

Thông số

TênLoạiMô tả
descriptionStringMô tả của dải ô hoặc 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 yêu cầu ủy 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)

Đặt xem 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ệ. 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 bảo vệ bất kể cài đặt này là gì. Gửi một ngoại lệ nếu bảng tính không thuộc một miền Google Workspace (nghĩa là miền đó thuộc sở hữu của một tài khoản gmail.com).

Thông số

TênLoạiMô tả
editableBooleantrue nếu tất cả người dùng trong miền sở hữu bảng tính đều 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ầ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 yêu cầu ủy 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 gồm vùng khác với dải ô được bảo vệ hiện tại, phương pháp này di chuyển tính năng bảo vệ để bao phủ dải ô được đặt tên thay thế. 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 dải ô. Lưu ý rằng các 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; đang gọi Range.protect() để tạo một biện pháp bảo vệ từ Range xảy ra là một dải ô được đặt tên, mà không gọi setRangeName(rangeName), sẽ không được đủ để liên kết chúng. Tuy nhiên, việc tạo dải ô được bảo vệ từ một dải ô được đặt tên trong Giao diện người dùng của Google Trang tính tự động liên kết các bảng 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');

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

Thông số

TênLoạiMô 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 yêu cầu ủy 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 phạm vi đã cho bao gồm một khu vực khác với dải ô được bảo vệ hiện tại, phương thức này sẽ di chuyển tính năng bảo vệ để bao phủ 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());

Thông số

TênLoạiMô tả
rangeRangeDải ô mới để tránh bị 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 yêu cầu ủy 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 gồm vùng khác với dải ô được bảo vệ hiện tại, phương pháp này di chuyển tính năng bảo vệ để bao phủ dải ô được đặt tên thay thế. 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 dải ô. Lưu ý rằng các 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; đang gọi Range.protect() để tạo một biện pháp bảo vệ từ Range xảy ra là một dải ô được đặt tên, mà không gọi setRangeName(rangeName), sẽ không được đủ để liên kết chúng. Tuy nhiên, việc tạo dải ô được bảo vệ từ một dải ô được đặt tên trong Giao diện người dùng của Google Trang tính tự động liên kết các bảng tính.

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

Thông số

TênLoạiMô tả
rangeNameStringTên của dải ô được đặt tên cầ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 yêu cầu ủy 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 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 bất kỳ dải ô nào không nằm trên trang tính được bảo vệ. Để thay đổi dải ô không được bảo vệ, hãy đặt một giá trị mới mảng dả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);
}

Thông số

TênLoạiMô tả
rangesRange[]Mảng dải ô không được bảo vệ trong 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 yêu cầu ủy 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)

Đặt xem dải ô được bảo vệ này có sử dụng "dựa trên cảnh báo" hay không bảo vệ thiết bị. Dựa trên cảnh báo tính năng bảo vệ 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 lời nhắc, 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. Để 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());

Thông số

TênLoạiMô 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 yêu cầu ủy 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