เข้าถึงและแก้ไขช่วงและชีตที่ป้องกันไว้ ช่วงที่ป้องกันจะป้องกันได้ทั้งช่วงของเซลล์แบบคงที่
หรือช่วงที่ตั้งชื่อ แผ่นงานที่ป้องกันอาจมีภูมิภาคที่ไม่ได้ป้องกัน สำหรับสเปรดชีตที่สร้างด้วย Google ชีตเวอร์ชันเก่า ให้ใช้คลาส
แทน
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); }
เมธอด
| วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
|---|---|---|
add | Protection | เพิ่มผู้ใช้ที่ระบุลงในรายชื่อผู้แก้ไขสำหรับชีตหรือช่วงที่ป้องกัน |
add | Protection | เพิ่มผู้ใช้ที่ระบุลงในรายชื่อผู้แก้ไขสำหรับชีตหรือช่วงที่ป้องกัน |
add | Protection | เพิ่มอาร์เรย์ของผู้ใช้ที่ระบุลงในรายชื่อผู้แก้ไขสำหรับชีตหรือช่วงที่ป้องกัน |
add | Protection | เพิ่มกลุ่มเป้าหมายที่ระบุเป็นผู้แก้ไขช่วงที่มีการป้องกัน |
can | Boolean | กำหนดว่าผู้ใช้ทั้งหมดในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์แก้ไขช่วงหรือชีตที่ป้องกันหรือไม่ |
can | Boolean | กำหนดว่าผู้ใช้มีสิทธิ์แก้ไขช่วงหรือชีตที่ได้รับการป้องกันหรือไม่ |
get | String | รับคำอธิบายของช่วงหรือชีตที่ป้องกัน |
get | User[] | รับรายชื่อเอดิเตอร์สำหรับช่วงหรือชีตที่ป้องกันไว้ |
get | Protection | รับประเภทของพื้นที่คุ้มครอง ซึ่งอาจเป็น RANGE หรือ SHEET |
get | Range | รับช่วงที่ได้รับการป้องกัน |
get | String|null | รับชื่อของช่วงที่ป้องกันหากเชื่อมโยงกับช่วงที่ตั้งชื่อไว้ |
get | Target | แสดงรหัสของกลุ่มเป้าหมายที่สามารถแก้ไขช่วงที่ได้รับการป้องกัน |
get | Range[] | รับอาร์เรย์ของช่วงที่ไม่ได้ป้องกันภายในชีตที่ป้องกัน |
is | Boolean | กำหนดว่าพื้นที่ที่ได้รับการปกป้องใช้การป้องกัน "ตามคำเตือน" หรือไม่ |
remove() | void | ยกเลิกการป้องกันช่วงหรือชีต |
remove | Protection | นำผู้ใช้ที่ระบุออกจากรายชื่อผู้แก้ไขชีตหรือช่วงที่ได้รับการป้องกัน |
remove | Protection | นำผู้ใช้ที่ระบุออกจากรายชื่อผู้แก้ไขชีตหรือช่วงที่ได้รับการป้องกัน |
remove | Protection | นำอาร์เรย์ของผู้ใช้ที่ระบุออกจากรายชื่อผู้แก้ไขชีตหรือช่วงที่ได้รับการปกป้อง |
remove | Protection | นำกลุ่มเป้าหมายที่ระบุออกจากการเป็นผู้แก้ไขช่วงที่มีการป้องกัน |
set | Protection | ตั้งค่าคำอธิบายของช่วงหรือชีตที่ป้องกัน |
set | Protection | กำหนดว่าผู้ใช้ทั้งหมดในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์แก้ไขช่วงหรือชีตที่ป้องกันหรือไม่ |
set | Protection | เชื่อมโยงช่วงที่ป้องกันกับช่วงที่ตั้งชื่อไว้ที่มีอยู่ |
set | Protection | ปรับช่วงที่ได้รับการป้องกัน |
set | Protection | เชื่อมโยงช่วงที่ป้องกันกับช่วงที่ตั้งชื่อไว้ที่มีอยู่ |
set | Protection | ยกเลิกการป้องกันอาร์เรย์ของช่วงที่ระบุภายในชีตที่ป้องกันไว้ |
set | Protection | ตั้งค่าว่าจะใช้การป้องกัน "ตามคำเตือน" กับช่วงที่ป้องกันนี้หรือไม่ |
เอกสารโดยละเอียด
add Editor(emailAddress)
เพิ่มผู้ใช้ที่ระบุลงในรายชื่อผู้แก้ไขสำหรับชีตหรือช่วงที่ป้องกัน เมธอดนี้ไม่ได้ให้สิทธิ์แก่ผู้ใช้ในการแก้ไขสเปรดชีตโดยอัตโนมัติ หากต้องการให้สิทธิ์เพิ่มเติม ให้เรียกใช้ 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()); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
email | String | อีเมลของผู้ใช้ที่จะเพิ่ม |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Editor(user)
เพิ่มผู้ใช้ที่ระบุลงในรายชื่อผู้แก้ไขสำหรับชีตหรือช่วงที่ป้องกัน เมธอดนี้ไม่ได้ให้สิทธิ์แก่ผู้ใช้ในการแก้ไขสเปรดชีตโดยอัตโนมัติ หากต้องการให้สิทธิ์เพิ่มเติม ให้เรียกใช้ 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()); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
user | User | การแสดงผู้ใช้ที่จะเพิ่ม |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Editors(emailAddresses)
เพิ่มอาร์เรย์ของผู้ใช้ที่ระบุลงในรายชื่อผู้แก้ไขสำหรับชีตหรือช่วงที่ป้องกัน เมธอดนี้ไม่ได้ให้สิทธิ์แก่ผู้ใช้ในการแก้ไขสเปรดชีตโดยอัตโนมัติ หากต้องการให้สิทธิ์ดังกล่าวด้วย ให้เรียกใช้ 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()); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
email | String[] | อาร์เรย์ของอีเมลของผู้ใช้ที่จะเพิ่ม |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
add Target Audience(audienceId)
เพิ่มกลุ่มเป้าหมายที่ระบุเป็นผู้แก้ไขช่วงที่มีการป้องกัน
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
audience | String | รหัสของกลุ่มเป้าหมายที่จะเพิ่ม |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
can Domain Edit()
กำหนดว่าผู้ใช้ทั้งหมดในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์แก้ไขช่วงหรือชีตที่ป้องกันหรือไม่ แสดงข้อยกเว้นหากผู้ใช้ไม่มีสิทธิ์แก้ไข ช่วงหรือชีตที่ได้รับการป้องกัน
// 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());
รีเทิร์น
Boolean — true หากผู้ใช้ทั้งหมดในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์
แก้ไขช่วงหรือชีตที่ป้องกัน false หากไม่มีสิทธิ์
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
can Edit()
กำหนดว่าผู้ใช้มีสิทธิ์แก้ไขช่วงหรือชีตที่ได้รับการป้องกันหรือไม่ เจ้าของสเปรดชีตจะแก้ไขช่วงและแผ่นงานที่ป้องกันไว้ได้เสมอ
// 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(); } }
รีเทิร์น
Boolean — true หากผู้ใช้มีสิทธิ์แก้ไขช่วงหรือชีตที่ได้รับการป้องกัน false หากไม่มีสิทธิ์
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Description()
รับคำอธิบายของช่วงหรือชีตที่ป้องกัน หากไม่ได้ตั้งค่าคำอธิบายไว้ เมธอดนี้จะ แสดงสตริงเปล่า
// 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);
รีเทิร์น
String — คำอธิบายของช่วงหรือชีตที่ได้รับการป้องกัน หรือสตริงว่างหากไม่ได้ตั้งค่าคำอธิบาย
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Editors()
รับรายชื่อเอดิเตอร์สำหรับช่วงหรือชีตที่ป้องกันไว้ ส่งข้อยกเว้นหากผู้ใช้ไม่มีสิทธิ์แก้ไขช่วงหรือชีตที่ได้รับการป้องกัน
// 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); }
รีเทิร์น
User[] - อาร์เรย์ของผู้ใช้ที่มีสิทธิ์แก้ไขช่วงหรือชีตที่ได้รับการป้องกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Protection Type()
รับประเภทของพื้นที่คุ้มครอง ซึ่งอาจเป็น RANGE หรือ 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());
รีเทิร์น
Protection — ประเภทของพื้นที่คุ้มครอง ไม่ว่าจะเป็น RANGE หรือ SHEET
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Range()
รับช่วงที่ได้รับการป้องกัน หากการป้องกันมีผลกับชีตแทนช่วง วิธีนี้จะแสดงช่วงที่ครอบคลุมทั้งชีต
// 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());
รีเทิร์น
Range - ช่วงที่กำลังป้องกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Range Name()
รับชื่อของช่วงที่ป้องกันหากเชื่อมโยงกับช่วงที่ตั้งชื่อไว้ แสดงผล null หากการป้องกันไม่ได้เชื่อมโยงกับช่วงที่ตั้งชื่อไว้ โปรดทราบว่าสคริปต์ต้องเรียกใช้ set อย่างชัดเจนเพื่อเชื่อมโยงช่วงที่ป้องกันกับช่วงที่ตั้งชื่อไว้ การเรียกใช้ Range.protect() เพื่อสร้างการป้องกันจาก Range ที่เป็นช่วงที่ตั้งชื่อไว้โดยไม่ได้เรียกใช้ set ไม่เพียงพอที่จะเชื่อมโยงช่วงดังกล่าว อย่างไรก็ตาม การสร้างช่วงที่ป้องกันจากช่วงที่ตั้งชื่อไว้ใน UI ของ Google ชีตจะเชื่อมโยงช่วงเหล่านั้นโดยอัตโนมัติ
// 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.
รีเทิร์น
String|null — ชื่อของช่วงที่มีชื่อที่ได้รับการป้องกัน หรือ null หากช่วงที่ได้รับการป้องกันไม่ได้
เชื่อมโยงกับช่วงที่มีชื่อ
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Target Audiences()
แสดงรหัสของกลุ่มเป้าหมายที่สามารถแก้ไขช่วงที่ได้รับการป้องกัน
รีเทิร์น
Target - อาร์เรย์ของรหัสกลุ่มเป้าหมาย
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Unprotected Ranges()
รับอาร์เรย์ของช่วงที่ไม่ได้ป้องกันภายในชีตที่ป้องกัน หากออบเจ็กต์ Protection
สอดคล้องกับช่วงที่ได้รับการป้องกันแทนที่จะเป็นชีตที่ได้รับการป้องกัน เมธอดนี้จะแสดงผลอาร์เรย์ว่าง
หากต้องการเปลี่ยนช่วงที่ไม่ได้ป้องกัน ให้ใช้ set เพื่อตั้งค่าอาร์เรย์ช่วงใหม่ หรือหากต้องการป้องกันทั้งชีตอีกครั้ง ให้ตั้งค่าอาร์เรย์เปล่า
// 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);
รีเทิร์น
Range[] - อาร์เรย์ของช่วงที่ไม่ได้ป้องกันภายในชีตที่ป้องกัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
is Warning Only()
กำหนดว่าพื้นที่ที่ได้รับการปกป้องใช้การป้องกัน "ตามคำเตือน" หรือไม่ การป้องกันตามคำเตือน
หมายความว่าผู้ใช้ทุกคนสามารถแก้ไขข้อมูลในพื้นที่ได้ ยกเว้นการแก้ไขที่ทำให้เกิดคำเตือนที่ขอให้
ผู้ใช้ยืนยันการแก้ไข โดยค่าเริ่มต้น ช่วงหรือชีตที่ป้องกันจะไม่มีการแจ้งเตือน หากต้องการเปลี่ยนเป็นสถานะคำเตือน ให้ใช้ 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);
รีเทิร์น
Boolean — true หากช่วงหรือชีตที่ป้องกันใช้การป้องกันที่อิงตามคำเตือนเท่านั้น
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove()
ยกเลิกการป้องกันช่วงหรือชีต
// 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(); }
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editor(emailAddress)
นำผู้ใช้ที่ระบุออกจากรายชื่อผู้แก้ไขชีตหรือช่วงที่ได้รับการป้องกัน โปรดทราบว่าหาก ผู้ใช้เป็นสมาชิกของกลุ่ม Google ที่มีสิทธิ์แก้ไข หรือหากผู้ใช้ทั้งหมดในโดเมน มีสิทธิ์แก้ไข ผู้ใช้จะยังคงแก้ไขพื้นที่ที่ป้องกันได้ ระบบจะนำทั้งเจ้าของ สเปรดชีตและผู้ใช้ปัจจุบันออกไม่ได้
// 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()); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
email | String | อีเมลของผู้ใช้ที่จะนำออก |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editor(user)
นำผู้ใช้ที่ระบุออกจากรายชื่อผู้แก้ไขชีตหรือช่วงที่ได้รับการป้องกัน โปรดทราบว่าหาก ผู้ใช้เป็นสมาชิกของกลุ่ม Google ที่มีสิทธิ์แก้ไข หรือหากผู้ใช้ทั้งหมดในโดเมน มีสิทธิ์แก้ไข ผู้ใช้จะยังแก้ไขพื้นที่ที่ป้องกันได้เช่นกัน ระบบจะนำทั้งเจ้าของสเปรดชีตและผู้ใช้ปัจจุบันออกไม่ได้
// 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()); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
user | User | การแสดงผู้ใช้ที่จะนำออก |
รีเทิร์น
Protection - ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Editors(emailAddresses)
นำอาร์เรย์ของผู้ใช้ที่ระบุออกจากรายชื่อผู้แก้ไขชีตหรือช่วงที่ได้รับการปกป้อง โปรดทราบว่าหากผู้ใช้เป็นสมาชิกของกลุ่ม Google ที่มีสิทธิ์แก้ไข หรือหากผู้ใช้ทั้งหมดในโดเมนมีสิทธิ์แก้ไข ผู้ใช้เหล่านั้นจะยังคงแก้ไขพื้นที่ที่ป้องกันได้ ระบบจะนำเจ้าของสเปรดชีตหรือผู้ใช้ปัจจุบันออกไม่ได้
// 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); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
email | String[] | อาร์เรย์ของอีเมลของผู้ใช้ที่จะนำออก |
รีเทิร์น
Protection - ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Target Audience(audienceId)
นำกลุ่มเป้าหมายที่ระบุออกจากการเป็นผู้แก้ไขช่วงที่มีการป้องกัน
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
audience | String | รหัสของกลุ่มเป้าหมายที่จะนำออก |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Description(description)
ตั้งค่าคำอธิบายของช่วงหรือชีตที่ป้องกัน
// 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);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
description | String | คำอธิบายของช่วงหรือชีตที่ป้องกันไว้ |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Domain Edit(editable)
กำหนดว่าผู้ใช้ทั้งหมดในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์แก้ไขช่วงหรือชีตที่ป้องกันหรือไม่ โปรดทราบว่าผู้ใช้ที่มีสิทธิ์แก้ไขอย่างชัดเจนจะสามารถ แก้ไขพื้นที่ที่ได้รับการป้องกันได้โดยไม่คำนึงถึงการตั้งค่านี้ แสดงข้อยกเว้นหากสเปรดชีตไม่ได้อยู่ในโดเมน Google Workspace (กล่าวคือ หากบัญชี gmail.com เป็นเจ้าของ)
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
editable | Boolean | true หากผู้ใช้ทั้งหมดในโดเมนที่เป็นเจ้าของสเปรดชีตควรมี
สิทธิ์แก้ไขช่วงหรือชีตที่ป้องกัน false หากไม่ |
รีเทิร์น
Protection - ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Named Range(namedRange)
เชื่อมโยงช่วงที่ป้องกันกับช่วงที่ตั้งชื่อไว้ที่มีอยู่ หากช่วงที่มีชื่อครอบคลุมพื้นที่
ที่แตกต่างจากช่วงที่ป้องกันปัจจุบัน วิธีนี้จะย้ายการป้องกันเพื่อให้ครอบคลุม
ช่วงที่มีชื่อแทน ช่วงที่มีชื่อต้องอยู่ในชีตเดียวกันกับช่วงที่ป้องกันปัจจุบัน โปรดทราบว่าสคริปต์ต้องเรียกใช้เมธอดนี้อย่างชัดเจนเพื่อเชื่อมโยงช่วงที่ป้องกันกับ
ช่วงที่มีชื่อ การเรียกใช้ Range.protect() เพื่อสร้างการป้องกันจาก Range
ซึ่งเป็นช่วงที่มีชื่อโดยไม่ได้เรียกใช้ set นั้นไม่
เพียงพอที่จะเชื่อมโยงช่วงดังกล่าว อย่างไรก็ตาม การสร้างช่วงที่ป้องกันจากช่วงที่ตั้งชื่อไว้ใน UI ของ Google ชีตจะเชื่อมโยงช่วงทั้ง 2 โดยอัตโนมัติ
// 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());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
named | Named | ช่วงที่มีชื่ออยู่แล้วที่จะเชื่อมโยงกับช่วงที่ป้องกัน |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Range(range)
ปรับช่วงที่ได้รับการป้องกัน หากช่วงที่ระบุครอบคลุมพื้นที่ที่แตกต่างจากช่วงที่ป้องกันปัจจุบัน วิธีนี้จะย้ายการป้องกันไปครอบคลุมช่วงใหม่แทน
// 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());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
range | Range | ช่วงใหม่ที่จะป้องกันไม่ให้มีการแก้ไข |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Range Name(rangeName)
เชื่อมโยงช่วงที่ป้องกันกับช่วงที่ตั้งชื่อไว้ที่มีอยู่ หากช่วงที่มีชื่อครอบคลุมพื้นที่
ที่แตกต่างจากช่วงที่ป้องกันปัจจุบัน วิธีนี้จะย้ายการป้องกันเพื่อให้ครอบคลุม
ช่วงที่มีชื่อแทน ช่วงที่มีชื่อต้องอยู่ในชีตเดียวกันกับช่วงที่ป้องกันปัจจุบัน โปรดทราบว่าสคริปต์ต้องเรียกใช้เมธอดนี้อย่างชัดเจนเพื่อเชื่อมโยงช่วงที่ป้องกันกับ
ช่วงที่มีชื่อ การเรียกใช้ Range.protect() เพื่อสร้างการป้องกันจาก Range
ซึ่งเป็นช่วงที่มีชื่อโดยไม่ได้เรียกใช้ set นั้นไม่
เพียงพอที่จะเชื่อมโยงช่วงดังกล่าว อย่างไรก็ตาม การสร้างช่วงที่ป้องกันจากช่วงที่ตั้งชื่อไว้ใน UI ของ Google ชีตจะเชื่อมโยงช่วงทั้ง 2 โดยอัตโนมัติ
// 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.
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
range | String | ชื่อของช่วงที่ตั้งชื่อแล้วที่จะป้องกัน |
รีเทิร์น
Protection - ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Unprotected Ranges(ranges)
ยกเลิกการป้องกันอาร์เรย์ของช่วงที่ระบุภายในชีตที่ป้องกันไว้ ส่งข้อยกเว้นหากออบเจ็กต์
Protectionสอดคล้องกับช่วงที่ได้รับการป้องกันแทนที่จะเป็นชีตที่ได้รับการป้องกัน หรือหาก
ช่วงใดช่วงหนึ่งไม่ได้อยู่ในชีตที่ได้รับการป้องกัน หากต้องการเปลี่ยนช่วงที่ไม่ป้องกัน ให้ตั้งค่าอาร์เรย์ของช่วงใหม่ หรือหากต้องการป้องกันทั้งชีตอีกครั้ง ให้ตั้งค่าอาร์เรย์ว่าง
// 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); }
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
ranges | Range[] | อาร์เรย์ของช่วงที่จะปล่อยให้ไม่มีการป้องกันภายในชีตที่ป้องกันไว้ |
รีเทิร์น
Protection - ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Warning Only(warningOnly)
ตั้งค่าว่าจะใช้การป้องกัน "ตามคำเตือน" กับช่วงที่ป้องกันนี้หรือไม่ การป้องกันแบบคำเตือน
หมายความว่าผู้ใช้ทุกคนสามารถแก้ไขข้อมูลในพื้นที่ได้ ยกเว้นการแก้ไขที่ทำให้เกิดคำเตือน
ซึ่งขอให้ผู้ใช้ยืนยันการแก้ไข โดยค่าเริ่มต้น ช่วงหรือชีตที่ป้องกันจะไม่ได้อิงตามคำเตือน หากต้องการตรวจสอบสถานะคำเตือน ให้ใช้ 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());
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
warning | Boolean |
รีเทิร์น
Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets