Class Protection

การปกป้อง

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

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

เมธอด

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
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|nullรับชื่อของช่วงที่ป้องกันหากเชื่อมโยงกับช่วงที่ตั้งชื่อไว้
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());
}

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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());
}

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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());
}

พารามิเตอร์

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

รีเทิร์น

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

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

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

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

addTargetAudience(audienceId)

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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.
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();
  }
}

รีเทิร์น

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

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

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

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

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

พารามิเตอร์

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

รีเทิร์น

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

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

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

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

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
userUserการแสดงผู้ใช้ที่จะนำออก

รีเทิร์น

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

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

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

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

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

พารามิเตอร์

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

รีเทิร์น

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

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

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

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

removeTargetAudience(audienceId)

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
descriptionStringคำอธิบายของช่วงหรือชีตที่ป้องกันไว้

รีเทิร์น

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

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

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

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

setDomainEdit(editable)

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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 ชีตจะเชื่อมโยงช่วงทั้ง 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());

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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());

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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 ชีตจะเชื่อมโยงช่วงทั้ง 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.

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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.
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);
}

พารามิเตอร์

ชื่อประเภทคำอธิบาย
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());

พารามิเตอร์

ชื่อประเภทคำอธิบาย
warningOnlyBoolean

รีเทิร์น

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

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

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

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