Truy cập và sửa đổi dải ô và trang tính được bảo vệ. Một 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. Một 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 bảng tính được tạo bằng phiên bản cũ của Google Trang tính, hãy sử dụng lớp .
Page
// Protect range A1:B10, then remove all other users from the list of editors. const ss = SpreadsheetApp.getActive(); const range = ss.getRange('A1:B10'); const protection = range.protect().setDescription('Sample protected range'); // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
// Remove all range protections in the spreadsheet that the user has permission // to edit. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i = 0; i < protections.length; i++) { const protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Protect the active sheet, then remove all other users from the list of // editors. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet'); // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
Phương thức
| Phương thức | Kiểu dữ liệu trả về | Mô tả ngắn |
|---|---|---|
add | Protection | Thêm người dùng đã cho vào danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ. |
add | Protection | Thêm người dùng đã cho vào danh sách người chỉnh sửa của trang tính hoặc dải ô được bảo vệ. |
add | Protection | 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ệ. |
add | Protection | Thêm đối tượng mục tiêu được chỉ định làm người chỉnh sửa dải ô được bảo vệ. |
can | Boolean | 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 được bảo vệ hay không. |
can | Boolean | 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. |
get | String | Lấy nội dung mô tả của dải ô hoặc trang tính được bảo vệ. |
get | User[] | Lấy danh sách người chỉnh sửa cho dải ô hoặc trang tính được bảo vệ. |
get | Protection | Lấy loại vùng được bảo vệ, là RANGE hoặc SHEET. |
get | Range | Lấy dải ô đang được bảo vệ. |
get | String|null | 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. |
get | Target | Trả về mã nhận dạng của những đối tượng mục tiêu có thể chỉnh sửa dải ô được bảo vệ. |
get | 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ệ. |
is | Boolean | Xác định xem khu vực được bảo vệ có đang sử dụng biện pháp bảo vệ "dựa trên cảnh báo" hay không. |
remove() | void | Huỷ bảo vệ dải ô hoặc trang tính. |
remove | Protection | Xoá 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ệ. |
remove | Protection | Xoá 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ệ. |
remove | Protection | 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ệ. |
remove | Protection | Xoá đối tượng mục tiêu được chỉ định khỏi vai trò người chỉnh sửa dải ô được bảo vệ. |
set | Protection | Đặt nội dung mô tả cho dải ô hoặc trang tính được bảo vệ. |
set | Protection | Đặt 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 được bảo vệ hay không. |
set | Protection | Liên kết dải ô được bảo vệ với một dải ô có tên hiện có. |
set | Protection | Điều chỉnh dải ô đang được bảo vệ. |
set | Protection | Liên kết dải ô được bảo vệ với một dải ô có tên hiện có. |
set | Protection | Huỷ bảo vệ mảng dải ô đã cho trong một trang tính được bảo vệ. |
set | Protection | Đặt xem dải ô được bảo vệ này có đang sử dụng chế độ bảo vệ "dựa trên cảnh báo" hay không. |
Tài liệu chi tiết
add Editor(emailAddress)
Thêm người dùng đã cho 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 bảng tính; để thực hiện việc đó, hãy gọi thêm 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ên | Loại | Mô tả |
|---|---|---|
email | String | Đị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 chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Editor(user)
Thêm người dùng đã cho 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 bảng tính; để thực hiện việc đó, hãy gọi thêm 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ên | Loại | Mô tả |
|---|---|---|
user | User | Thông tin về người dùng cần thêm. |
Cầu thủ trả bóng
Protection – Đối tượng đại diện cho chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Editors(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ệ. 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; để thực hiện việc đó, hãy gọi thêm 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ên | Loại | Mô tả |
|---|---|---|
email | String[] | Một mảng gồm đị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 chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Target Audience(audienceId)
Thêm đối tượng mục tiêu được chỉ định làm người chỉnh sửa dải ô được bảo vệ.
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
audience | String | Mã nhận dạng 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 chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
can Domain Edit()
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 được bảo vệ hay không. Gây ra 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 đượ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
Boolean – true 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 họ không có quyền.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
can Edit()
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. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i = 0; i < protections.length; i++) { const protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
Cầu thủ trả bóng
Boolean – true 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Description()
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ả, 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ả về dải ô hoặc trang tính được bảo vệ, hoặc một chuỗi trống nếu bạn không đặt nội dung mô tả.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Editors()
Lấy danh sách người chỉnh sửa cho dải ô hoặc trang tính được bảo vệ. Gây ra 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. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet'); // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Protection Type()
Lấy loại vùng được bảo vệ, là 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
Protection – Loại khu bảo tồn, có thể là RANGE hoặc SHEET.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Range()
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ì một dải ô, thì phương thức này sẽ trả về một dải ô trải rộng trên 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Range Name()
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 chế độ bảo vệ không được liên kết với một dải ô có tên. Xin lưu ý rằng các tập lệnh phải gọi rõ ràng set để liên kết một dải ô được bảo vệ với một dải ô có tên; việc gọi Range.protect() để tạo chế độ bảo vệ từ một Range trùng hợp là một dải ô có tên, mà không gọi set, 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ên trong giao diện người dùng Google Trang tính sẽ tự động liên kết chúng.
// Protect a named range in a spreadsheet and log the name of the protected // range. const ss = SpreadsheetApp.getActive(); const range = ss.getRange('A1:B10'); const protection = range.protect(); ss.setNamedRange('Test', range); // Create a named range. protection.setRangeName( 'Test'); // Associate the protection with the named range. Logger.log( protection.getRangeName()); // Verify the name of the protected range.
Cầu thủ trả bóng
String|null – tên của dải ô có 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ên
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Target Audiences()
Trả về mã nhận dạng của những đố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
Target – Một mảng gồm các mã nhận dạng của đối tượng mục tiêu.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Unprotected Ranges()
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 dùng set để đặ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. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect(); const 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
is Warning Only()
Xác định xem khu vực được bảo vệ có đang sử dụng biện pháp bảo vệ "dựa trên cảnh báo" hay không. Chế độ 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 vùng, 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 dải ô hoặc trang tính được bảo vệ không dựa trên cảnh báo. Để thay đổi thành trạng thái cảnh báo, hãy sử dụng set.
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Sets the warning status for the protected sheet as true. sampleProtectedSheet.setWarningOnly(true); const protectedSheetWarningStatus = sampleProtectedSheet.isWarningOnly(); // Logs the warning status of the protected sheet to the console. console.log(protectedSheetWarningStatus);
Cầu thủ trả bóng
Boolean – true 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
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove()
Huỷ bảo vệ dải ô hoặc trang tính.
// Remove all range protections in the spreadsheet that the user has permission // to edit. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i = 0; i < protections.length; i++) { const protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Remove sheet protection from the active sheet, if the user has permission to // edit it. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; if (protection?.canEdit()) { protection.remove(); }
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editor(emailAddress)
Xoá 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 người dùng là thành viên của một Nhóm trên 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 vùng được bảo vệ. Bạn không thể xoá chủ sở hữu bảng tính hoặc người dùng hiện tạ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', ); // 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ên | Loại | Mô tả |
|---|---|---|
email | String | Đị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 chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editor(user)
Xoá 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 người dùng là thành viên của một Nhóm trên 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 vùng được bảo vệ. Bạn không thể xoá chủ sở hữu bảng tính hoặc người dùng hiện tạ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', ); // 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ên | Loại | Mô tả |
|---|---|---|
user | User | Thông tin về người dùng cần xoá. |
Cầu thủ trả bóng
Protection – đối tượng đại diện cho chế độ cài đặt bảo vệ, để liên kết
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editors(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 trên 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 vùng được bảo vệ. Bạn không thể xoá chủ sở hữu bảng tính hoặc người dùng hiện tại.
// Protect the active sheet, then remove all other users from the list of // editors. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet'); // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
email | String[] | Một mảng địa chỉ email của những người dùng cần xoá. |
Cầu thủ trả bóng
Protection – đối tượng đại diện cho chế độ cài đặt bảo vệ, để liên kết
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Target Audience(audienceId)
Xoá đối tượng mục tiêu được chỉ định khỏi vai trò người chỉnh sửa dải ô được bảo vệ.
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
audience | String | Mã nhận dạng 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 chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Description(description)
Đặt nội dung mô tả cho 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ên | Loại | Mô tả |
|---|---|---|
description | String | Nội dung mô tả về 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 chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Domain Edit(editable)
Đặt 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 được bảo vệ hay không. Xin lưu ý rằng mọi người dùng 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. Gây ra một ngoại lệ nếu bảng tính không thuộc miền Google Workspace (tức là nếu bảng tính thuộc sở hữu của một tài khoản gmail.com).
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
editable | Boolean | true 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ầu thủ trả bóng
Protection – đối tượng đại diện cho chế độ cài đặt bảo vệ, để liên kết
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Named Range(namedRange)
Liên kết dải ô được bảo vệ với một dải ô có tên hiện có. Nếu dải ô được đặt tên bao gồm một vùng 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 biện pháp bảo vệ để bao gồm dải ô được đặt tên thay thế. Dải ô được đặt tên phải nằm trên cùng trang tính với dải ô được bảo vệ hiện tại. Xin lưu ý rằng các tập lệnh phải gọi rõ ràng phương thức này để liên kết một dải ô được bảo vệ với một dải ô có tên; việc gọi Range.protect() để tạo chế độ bảo vệ từ một Range (tình cờ là một dải ô có tên) mà không gọi set 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ên trong giao diện người dùng Google Trang tính sẽ tự động liên kết hai dải ô này.
// 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ên | Loại | Mô tả |
|---|---|---|
named | Named | Dả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 chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Range(range)
Điều chỉnh dải ô đang được bảo vệ. Nếu dải ô đã cho bao gồm một vùng 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 chế độ 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());
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
range | Range | Phạm vi mới cần bảo vệ khỏi nội dung chỉnh sửa. |
Cầu thủ trả bóng
Protection – Đối tượng đại diện cho chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Range Name(rangeName)
Liên kết dải ô được bảo vệ với một dải ô có tên hiện có. Nếu dải ô được đặt tên bao gồm một vùng 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 biện pháp bảo vệ để bao gồm dải ô được đặt tên thay thế. Dải ô được đặt tên phải nằm trên cùng trang tính với dải ô được bảo vệ hiện tại. Xin lưu ý rằng các tập lệnh phải gọi rõ ràng phương thức này để liên kết một dải ô được bảo vệ với một dải ô có tên; việc gọi Range.protect() để tạo chế độ bảo vệ từ một Range (tình cờ là một dải ô có tên) mà không gọi set 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ên trong giao diện người dùng Google Trang tính sẽ tự động liên kết hai dải ô này.
// Protect a named range in a spreadsheet and log the name of the protected // range. const ss = SpreadsheetApp.getActive(); const range = ss.getRange('A1:B10'); const protection = range.protect(); ss.setNamedRange('Test', range); // Create a named range. protection.setRangeName( 'Test'); // Associate the protection with the named range. Logger.log( protection.getRangeName()); // Verify the name of the protected range.
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
range | String | Tê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 chế độ cài đặt bảo vệ, để liên kết
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Unprotected Ranges(ranges)
Huỷ bảo vệ mảng dải ô đã cho trong một trang tính được bảo vệ. Gây ra 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 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. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet'); const unprotected = sheet.getRange('B2:C5'); protection.setUnprotectedRanges([unprotected]); // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
ranges | Range[] | Dãy ô 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 chế độ cài đặt bảo vệ, để liên kết
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Warning Only(warningOnly)
Đặt xem dải ô được bảo vệ này có đang sử dụng chế độ bảo vệ "dựa trên cảnh báo" hay không. Chế độ 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 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 sử dụng is.
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc123456/edit', ); // Gets the sheet 'Sheet1' by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet and sets the protection to warning-based. const sampleProtectedSheet = sheet.protect().setWarningOnly(true); // Logs whether the protected sheet is warning-based to the console. console.log(sampleProtectedSheet.isWarningOnly());
Thông số
| Tên | Loại | Mô tả |
|---|---|---|
warning | Boolean |
Cầu thủ trả bóng
Protection – Đối tượng đại diện cho chế độ cài đặt bảo vệ, để liên kết.
Ủy quyền
Các tập lệnh sử dụng phương thức này cần được uỷ quyền bằng một hoặc nhiều phạm vi sau đây:
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets