Class Protection

การปกป้อง

เข้าถึงและแก้ไขช่วงและชีตที่มีการป้องกัน ช่วงที่มีการป้องกันจะป้องกันช่วงคงที่ ของเซลล์หรือช่วงที่ตั้งชื่อแล้ว ชีตที่มีการป้องกันอาจมีส่วนที่ไม่มีการป้องกัน สำหรับสเปรดชีตที่สร้างด้วย Google ชีตเวอร์ชันเก่า ให้ใช้คลาส PageProtection แทน

// Protect range A1:B10, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var protection = range.protect().setDescription('Sample protected range');

// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}
// Remove all range protections in the spreadsheet that the user has permission to edit.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Protect the active sheet, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');

// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

วิธีการ

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
addEditor(emailAddress)Protectionเพิ่มผู้ใช้ที่ต้องการไปยังรายชื่อเอดิเตอร์สำหรับชีตหรือช่วงที่ป้องกันไว้
addEditor(user)Protectionเพิ่มผู้ใช้ที่ต้องการไปยังรายชื่อเอดิเตอร์สำหรับชีตหรือช่วงที่ป้องกันไว้
addEditors(emailAddresses)Protectionเพิ่มอาร์เรย์ของผู้ใช้ที่ระบุในรายชื่อเครื่องมือแก้ไขสำหรับชีตหรือช่วงที่มีการป้องกัน
addTargetAudience(audienceId)Protectionเพิ่มกลุ่มเป้าหมายที่ระบุเป็นเอดิเตอร์ของช่วงที่มีการป้องกัน
canDomainEdit()Booleanตรวจสอบว่าผู้ใช้ทุกคนในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์แก้ไขช่วงหรือชีตที่มีการป้องกันหรือไม่
canEdit()Booleanกำหนดว่าผู้ใช้มีสิทธิ์แก้ไขช่วงหรือแผ่นงานที่มีการป้องกันหรือไม่
getDescription()Stringรับคำอธิบายของช่วงหรือแผ่นงานที่มีการป้องกัน
getEditors()User[]เรียกดูรายชื่อเครื่องมือแก้ไขสำหรับช่วงหรือแผ่นงานที่มีการป้องกัน
getProtectionType()ProtectionTypeรับประเภทของพื้นที่ที่มีการป้องกันเป็น RANGE หรือ SHEET
getRange()Rangeรับช่วงที่มีการป้องกัน
getRangeName()Stringเรียกชื่อของช่วงที่มีการป้องกัน ถ้าเชื่อมโยงกับช่วงที่ตั้งชื่อแล้ว
getTargetAudiences()TargetAudience[]แสดงผลรหัสของกลุ่มเป้าหมายที่แก้ไขช่วงที่มีการป้องกันได้
getUnprotectedRanges()Range[]รับอาร์เรย์ของช่วงที่ไม่มีการป้องกันภายในชีตที่มีการป้องกัน
isWarningOnly()Booleanระบุว่าพื้นที่ที่ป้องกันใช้การป้องกันแบบ "ตามคำเตือน" หรือไม่
remove()voidยกเลิกการป้องกันช่วงหรือชีต
removeEditor(emailAddress)Protectionนำผู้ใช้ที่ระบุออกจากรายชื่อเอดิเตอร์ของชีตหรือช่วงที่ป้องกันไว้
removeEditor(user)Protectionนำผู้ใช้ที่ระบุออกจากรายชื่อเอดิเตอร์ของชีตหรือช่วงที่ป้องกันไว้
removeEditors(emailAddresses)Protectionนำอาร์เรย์ของผู้ใช้ที่ระบุออกจากรายการเครื่องมือแก้ไขสำหรับชีตหรือช่วงที่มีการป้องกัน
removeTargetAudience(audienceId)Protectionนำกลุ่มเป้าหมายที่ระบุออกจากการเป็นผู้แก้ไขของช่วงที่มีการป้องกัน
setDescription(description)Protectionตั้งค่าคำอธิบายของช่วงหรือแผ่นงานที่มีการป้องกัน
setDomainEdit(editable)Protectionตั้งค่าว่าผู้ใช้ทุกคนในโดเมนที่เป็นเจ้าของสเปรดชีตจะมีสิทธิ์แก้ไขช่วงหรือชีตที่มีการป้องกันหรือไม่
setNamedRange(namedRange)Protectionเชื่อมโยงช่วงที่มีการป้องกันกับช่วงที่ตั้งชื่อแล้วที่มีอยู่
setRange(range)Protectionปรับช่วงที่มีการป้องกัน
setRangeName(rangeName)Protectionเชื่อมโยงช่วงที่มีการป้องกันกับช่วงที่ตั้งชื่อแล้วที่มีอยู่
setUnprotectedRanges(ranges)Protectionยกเลิกการป้องกันอาร์เรย์ของช่วงที่ระบุภายในชีตที่มีการป้องกัน
setWarningOnly(warningOnly)Protectionตั้งค่าว่าช่วงที่ป้องกันนี้ใช้การป้องกัน "ตามคำเตือน" หรือไม่

เอกสารประกอบโดยละเอียด

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressStringอีเมลของผู้ใช้ที่จะเพิ่ม

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
userUserตัวแทนของผู้ใช้ที่จะเพิ่ม

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressesString[]อาร์เรย์อีเมลของผู้ใช้ที่จะเพิ่ม

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

addTargetAudience(audienceId)

เพิ่มกลุ่มเป้าหมายที่ระบุเป็นเอดิเตอร์ของช่วงที่มีการป้องกัน

พารามิเตอร์

ชื่อTypeคำอธิบาย
audienceIdStringรหัสของกลุ่มเป้าหมายที่จะเพิ่ม

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

canDomainEdit()

ตรวจสอบว่าผู้ใช้ทุกคนในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์แก้ไขช่วงหรือชีตที่มีการป้องกันหรือไม่ ระบบแสดงข้อผิดพลาดหากผู้ใช้ไม่มีสิทธิ์แก้ไขช่วงหรือชีตที่มีการป้องกัน

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

รีเทิร์น

Booleantrue หากผู้ใช้ทุกคนในโดเมนที่เป็นเจ้าของสเปรดชีตมีสิทธิ์แก้ไขช่วงหรือชีตที่ป้องกันไว้ falseหากผู้ใช้ไม่มีสิทธิ์

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

canEdit()

กำหนดว่าผู้ใช้มีสิทธิ์แก้ไขช่วงหรือแผ่นงานที่มีการป้องกันหรือไม่ เจ้าของสเปรดชีตสามารถแก้ไขช่วงและชีตที่มีการป้องกันเสมอ

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

รีเทิร์น

Booleantrue หากผู้ใช้มีสิทธิ์แก้ไขช่วงหรือชีตที่ป้องกันไว้ ค่า false หากไม่มีสิทธิ์

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getDescription()

รับคำอธิบายของช่วงหรือแผ่นงานที่มีการป้องกัน หากไม่มีการตั้งค่าคำอธิบายไว้ เมธอดจะแสดงสตริงว่าง

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

getEditors()

เรียกดูรายชื่อเครื่องมือแก้ไขสำหรับช่วงหรือแผ่นงานที่มีการป้องกัน ระบบแสดงข้อผิดพลาดหากผู้ใช้ไม่มีสิทธิ์แก้ไขช่วงหรือชีตที่มีการป้องกัน

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

รีเทิร์น

User[] — อาร์เรย์ของผู้ใช้ที่มีสิทธิ์แก้ไขช่วงหรือชีตที่มีการป้องกัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getProtectionType()

รับประเภทของพื้นที่ที่มีการป้องกันเป็น 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());

รีเทิร์น

ProtectionType — ประเภทของพื้นที่ที่มีการป้องกัน อาจเป็น RANGE หรือ SHEET

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getRange()

รับช่วงที่มีการป้องกัน หากมีการใช้การป้องกันกับชีตแทนช่วง เมธอดนี้จะแสดงผลช่วงที่ครอบคลุมทั้งชีต

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

getRangeName()

เรียกชื่อของช่วงที่มีการป้องกัน ถ้าเชื่อมโยงกับช่วงที่ตั้งชื่อแล้ว แสดงผล null หากการป้องกันไม่ได้เชื่อมโยงกับช่วงที่ตั้งชื่อไว้ โปรดทราบว่าสคริปต์ต้องเรียกใช้ setRangeName(rangeName) อย่างชัดเจนเพื่อเชื่อมโยงช่วงที่มีการป้องกันกับช่วงที่ตั้งชื่อแล้ว การเรียก Range.protect() เพื่อสร้างการป้องกันจาก Range ที่เกิดขึ้นเป็นช่วงที่ตั้งชื่อแล้วโดยไม่ได้เรียก setRangeName(rangeName) นั้นไม่เพียงพอที่จะเชื่อมโยง แต่การสร้างช่วงที่ป้องกันจากช่วงที่ตั้งชื่อแล้วใน UI ของ Google ชีตจะเชื่อมโยงช่วงนั้นโดยอัตโนมัติ

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

รีเทิร์น

String — ชื่อของช่วงที่ตั้งชื่อที่มีการป้องกัน หรือ null หากช่วงที่มีการป้องกันไม่เชื่อมโยงกับช่วงที่ตั้งชื่อแล้ว

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getTargetAudiences()

แสดงผลรหัสของกลุ่มเป้าหมายที่แก้ไขช่วงที่มีการป้องกันได้

รีเทิร์น

TargetAudience[] — อาร์เรย์ของรหัสของกลุ่มเป้าหมาย

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getUnprotectedRanges()

รับอาร์เรย์ของช่วงที่ไม่มีการป้องกันภายในชีตที่มีการป้องกัน หากออบเจ็กต์ Protection ตรงกับช่วงที่ป้องกันไว้แทนที่จะเป็นชีตที่ป้องกันไว้ เมธอดนี้จะแสดงอาร์เรย์ที่ว่างเปล่า หากต้องการเปลี่ยนช่วงที่ไม่ป้องกัน ให้ใช้ setUnprotectedRanges(ranges) เพื่อกำหนดอาร์เรย์ของช่วงใหม่ หากต้องการป้องกันแผ่นงานทั้งหมดอีกครั้ง ให้ตั้งค่าอาร์เรย์ว่าง

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

รีเทิร์น

Range[] — อาร์เรย์ของช่วงที่ไม่มีการป้องกันภายในชีตที่มีการป้องกัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

isWarningOnly()

ระบุว่าพื้นที่ที่ป้องกันใช้การป้องกันแบบ "ตามคำเตือน" หรือไม่ การป้องกันตามคำเตือนหมายความว่าผู้ใช้ทุกคนสามารถแก้ไขข้อมูลในพื้นที่นั้นได้ ยกเว้นการแก้ไขจะแสดงคำเตือนที่ขอให้ผู้ใช้ยืนยันการแก้ไข โดยค่าเริ่มต้น ช่วงหรือชีตที่มีการป้องกันจะไม่อิงตามคำเตือน หากต้องการเปลี่ยนสถานะคำเตือน ให้ใช้ 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);

รีเทิร์น

Booleantrue หากช่วงหรือชีตที่ป้องกันใช้การป้องกันตามคำเตือนเท่านั้น

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 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.
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();
}

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

removeEditor(emailAddress)

นำผู้ใช้ที่ระบุออกจากรายชื่อเอดิเตอร์ของชีตหรือช่วงที่ป้องกันไว้ โปรดทราบว่าหากผู้ใช้เป็นสมาชิกของ Google Group ที่มีสิทธิ์แก้ไข หรือหากผู้ใช้ทั้งหมดในโดเมนมีสิทธิ์แก้ไข ผู้ใช้จะยังแก้ไขพื้นที่ที่ป้องกันไว้ได้ ทั้งเจ้าของสเปรดชีตและผู้ใช้ปัจจุบันไม่สามารถนำออกได้

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressStringอีเมลของผู้ใช้ที่จะนำออก

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

removeEditor(user)

นำผู้ใช้ที่ระบุออกจากรายชื่อเอดิเตอร์ของชีตหรือช่วงที่ป้องกันไว้ โปรดทราบว่าหากผู้ใช้เป็นสมาชิกของ Google Group ที่มีสิทธิ์แก้ไข หรือหากผู้ใช้ทั้งหมดในโดเมนมีสิทธิ์แก้ไข ผู้ใช้จะยังแก้ไขพื้นที่ที่ป้องกันได้เช่นกัน ทั้งเจ้าของสเปรดชีตและผู้ใช้ปัจจุบันไม่สามารถนำออกได้

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
userUserตัวแทนของผู้ใช้ที่จะนำออก

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับเชน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

removeEditors(emailAddresses)

นำอาร์เรย์ของผู้ใช้ที่ระบุออกจากรายการเครื่องมือแก้ไขสำหรับชีตหรือช่วงที่มีการป้องกัน โปรดทราบว่าถ้าผู้ใช้รายใดเป็นสมาชิก Google Group ที่มีสิทธิ์แก้ไข หรือหากผู้ใช้ทั้งหมดในโดเมนมีสิทธิ์แก้ไข ผู้ใช้เหล่านั้นจะยังคงสามารถแก้ไขพื้นที่ที่มีการป้องกันได้ ทั้งเจ้าของสเปรดชีตและผู้ใช้ปัจจุบันไม่สามารถนำออกได้

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
emailAddressesString[]อาร์เรย์อีเมลของผู้ใช้ที่จะนำออก

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับเชน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

removeTargetAudience(audienceId)

นำกลุ่มเป้าหมายที่ระบุออกจากการเป็นผู้แก้ไขของช่วงที่มีการป้องกัน

พารามิเตอร์

ชื่อTypeคำอธิบาย
audienceIdStringรหัสของกลุ่มเป้าหมายที่จะนำออก

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
descriptionStringคำอธิบายของช่วงหรือแผ่นงานที่มีการป้องกัน

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setDomainEdit(editable)

ตั้งค่าว่าผู้ใช้ทุกคนในโดเมนที่เป็นเจ้าของสเปรดชีตจะมีสิทธิ์แก้ไขช่วงหรือชีตที่มีการป้องกันหรือไม่ โปรดทราบว่าผู้ใช้ที่มีสิทธิ์แก้ไขอย่างชัดแจ้งจะแก้ไขพื้นที่ที่มีการป้องกันได้โดยไม่ต้องคำนึงถึงการตั้งค่านี้ หากสเปรดชีตไม่ได้เป็นของโดเมน Google Workspace (ซึ่งก็คือบัญชีที่เป็นของบัญชี gmail.com)

พารามิเตอร์

ชื่อTypeคำอธิบาย
editableBooleantrue หากผู้ใช้ทุกคนในโดเมนที่เป็นเจ้าของสเปรดชีตควรมีสิทธิ์แก้ไขช่วงหรือชีตที่ป้องกันไว้ กรณีที่ไม่มีสิทธิ์ false

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับเชน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setNamedRange(namedRange)

เชื่อมโยงช่วงที่มีการป้องกันกับช่วงที่ตั้งชื่อแล้วที่มีอยู่ หากช่วงที่ตั้งชื่อแล้วครอบคลุมพื้นที่ที่แตกต่างจากช่วงที่ป้องกันอยู่ในปัจจุบัน เมธอดนี้จะย้ายการป้องกันไปครอบคลุมช่วงที่ตั้งชื่อแทน ช่วงที่ตั้งชื่อแล้วต้องอยู่ในชีตเดียวกันกับช่วงที่ป้องกันปัจจุบัน โปรดทราบว่าสคริปต์ต้องเรียกใช้เมธอดนี้อย่างชัดเจนเพื่อเชื่อมโยงช่วงที่มีการป้องกันกับช่วงที่ตั้งชื่อแล้ว การเรียก Range.protect() เพื่อสร้างการป้องกันจาก Range ที่เกิดขึ้นเป็นช่วงที่ตั้งชื่อแล้วโดยไม่ได้เรียก setRangeName(rangeName) นั้นไม่เพียงพอที่จะเชื่อมโยง แต่การสร้างช่วงที่ป้องกันจากช่วงที่ตั้งชื่อแล้วใน UI ของ 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');

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
namedRangeNamedRangeช่วงที่ตั้งชื่อแล้วที่มีอยู่เพื่อเชื่อมโยงกับช่วงที่มีการป้องกัน

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
rangeRangeช่วงใหม่เพื่อป้องกันการแก้ไข

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setRangeName(rangeName)

เชื่อมโยงช่วงที่มีการป้องกันกับช่วงที่ตั้งชื่อแล้วที่มีอยู่ หากช่วงที่ตั้งชื่อแล้วครอบคลุมพื้นที่ที่แตกต่างจากช่วงที่ป้องกันอยู่ในปัจจุบัน เมธอดนี้จะย้ายการป้องกันไปครอบคลุมช่วงที่ตั้งชื่อแทน ช่วงที่ตั้งชื่อแล้วต้องอยู่ในชีตเดียวกันกับช่วงที่ป้องกันปัจจุบัน โปรดทราบว่าสคริปต์ต้องเรียกใช้เมธอดนี้อย่างชัดเจนเพื่อเชื่อมโยงช่วงที่มีการป้องกันกับช่วงที่ตั้งชื่อแล้ว การเรียก Range.protect() เพื่อสร้างการป้องกันจาก Range ที่เกิดขึ้นเป็นช่วงที่ตั้งชื่อแล้วโดยไม่ได้เรียก setRangeName(rangeName) นั้นไม่เพียงพอที่จะเชื่อมโยง แต่การสร้างช่วงที่ป้องกันจากช่วงที่ตั้งชื่อแล้วใน UI ของ Google ชีตจะเชื่อมโยงช่วงเหล่านั้นโดยอัตโนมัติ

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
rangeNameStringชื่อของช่วงที่ตั้งชื่อแล้วซึ่งจะป้องกัน

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับเชน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setUnprotectedRanges(ranges)

ยกเลิกการป้องกันอาร์เรย์ของช่วงที่ระบุภายในชีตที่มีการป้องกัน ระบบแสดงข้อยกเว้นหากออบเจ็กต์ Protection สอดคล้องกับช่วงที่มีการป้องกันแทนที่จะเป็นชีตที่มีการป้องกัน หรือหากช่วงใดก็ตามไม่อยู่ในชีตที่ป้องกันไว้ หากต้องการเปลี่ยนช่วงที่ไม่มีการป้องกัน ให้ตั้งค่าอาร์เรย์ของช่วงใหม่ หากต้องการป้องกันแผ่นงานทั้งหมดอีกครั้ง ให้ตั้งค่าอาร์เรย์ว่างเปล่า

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
rangesRange[]อาร์เรย์ของช่วงที่ไม่มีการป้องกันภายในชีตที่มีการป้องกัน

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงการตั้งค่าการป้องกันสำหรับเชน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setWarningOnly(warningOnly)

ตั้งค่าว่าช่วงที่ป้องกันนี้ใช้การป้องกัน "ตามคำเตือน" หรือไม่ การป้องกันตามคำเตือนหมายความว่าผู้ใช้ทุกคนสามารถแก้ไขข้อมูลในพื้นที่นั้นได้ ยกเว้นการแก้ไขจะแสดงคำเตือนที่ขอให้ผู้ใช้ยืนยันการแก้ไข โดยค่าเริ่มต้น ช่วงหรือชีตที่มีการป้องกันจะไม่อิงตามคำเตือน หากต้องการตรวจสอบสถานะคําเตือน ให้ใช้ 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());

พารามิเตอร์

ชื่อTypeคำอธิบาย
warningOnlyBoolean

รีเทิร์น

Protection — ออบเจ็กต์ที่แสดงถึงการตั้งค่าการป้องกันสำหรับสายโซ่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีการนี้ต้องได้รับสิทธิ์จากขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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