Class Protection

보호

보호된 범위 및 시트에 액세스하고 수정합니다. 보호된 범위는 셀의 정적 범위 또는 이름이 지정된 범위를 보호할 수 있습니다. 보호된 시트에는 보호되지 않은 영역이 포함될 수 있습니다. 이전 버전의 Google Sheets로 만든 스프레드시트에는 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[]보호된 범위를 수정할 수 있는 공유 대상 그룹의 ID를 반환합니다.
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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

addTargetAudience(audienceId)

지정된 공유 대상 그룹을 보호된 범위의 편집자로 추가합니다.

매개변수

이름유형설명
audienceIdString추가할 공유 대상 그룹의 ID입니다.

리턴

Protection - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

리턴

Boolean - 스프레드시트를 소유한 도메인의 모든 사용자가 보호된 범위 또는 시트를 수정할 권한이 있는 경우 true, 권한이 없으면 false입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

리턴

Boolean - 사용자에게 보호된 범위 또는 시트를 수정할 권한이 있는 경우 true, 권한이 없으면 false

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 보호된 범위나 시트에 대한 설명 또는 설명이 설정되지 않은 경우 빈 문자열입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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[] - 보호된 범위 또는 시트를 수정할 권한이 있는 사용자의 배열

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 보호 중인 범위입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

getRangeName()

보호된 범위가 이름이 지정된 범위와 연결된 경우 보호된 범위의 이름을 가져옵니다. 보호가 이름이 지정된 범위와 연결되어 있지 않으면 null를 반환합니다. 스크립트는 setRangeName(rangeName)를 명시적으로 호출하여 보호된 범위를 명명된 범위와 연결해야 합니다. setRangeName(rangeName)를 호출하지 않고 Range.protect()를 호출하여 이름이 지정된 범위인 Range로부터 보호를 만드는 것만으로는 충분하지 않습니다. 그러나 Google Sheets UI에서 이름이 지정된 범위에서 보호된 범위를 만들면 자동으로 연결됩니다.

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

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

getTargetAudiences()

보호된 범위를 수정할 수 있는 공유 대상 그룹의 ID를 반환합니다.

리턴

TargetAudience[] - 공유 대상 그룹 ID의 배열입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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[] - 보호된 시트 내의 보호되지 않은 범위 배열

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

리턴

Boolean - 보호된 범위 또는 시트에서 경고 기반 보호만 사용하는 경우 true

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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: 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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.
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);
}

매개변수

이름유형설명
emailAddressesString[]삭제할 사용자의 이메일 주소 배열입니다.

리턴

Protection: 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

removeTargetAudience(audienceId)

보호된 범위의 편집자로 지정된 공유 대상 그룹을 삭제합니다.

매개변수

이름유형설명
audienceIdString삭제할 공유 대상 그룹의 ID입니다.

리턴

Protection - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

setDomainEdit(editable)

스프레드시트를 소유한 도메인의 모든 사용자에게 보호된 범위 또는 시트를 수정할 권한이 있는지 여부를 설정합니다. 명시적인 수정 권한이 있는 모든 사용자는 이 설정과 관계없이 보호 구역을 수정할 수 있습니다. 스프레드시트가 Google Workspace 도메인에 속하지 않는 경우 (즉, gmail.com 계정에서 소유한 경우) 예외가 발생합니다.

매개변수

이름유형설명
editableBoolean스프레드시트를 소유한 도메인의 모든 사용자가 보호된 범위 또는 시트를 수정할 수 있는 권한을 보유해야 하는 경우 true, 그렇지 않은 경우 false입니다.

리턴

Protection: 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

setNamedRange(namedRange)

보호된 범위를 기존의 이름이 지정된 범위와 연결합니다. 이름이 지정된 범위가 현재 보호되는 범위와 다른 영역을 포함하는 경우, 이 메서드는 대신 명명된 범위를 포함하도록 보호를 이동합니다. 이름이 지정된 범위는 현재 보호된 범위와 동일한 시트에 있어야 합니다. 스크립트는 보호된 범위를 명명된 범위와 연결하려면 이 메서드를 명시적으로 호출해야 합니다. setRangeName(rangeName)를 호출하지 않고 Range.protect()를 호출하여 이름이 지정된 범위인 Range로부터 보호를 만드는 것만으로는 연결할 수 없습니다. 그러나 Google Sheets UI에서 이름이 지정된 범위에서 보호된 범위를 만들면 자동으로 연결됩니다.

// 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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

setRangeName(rangeName)

보호된 범위를 기존의 이름이 지정된 범위와 연결합니다. 이름이 지정된 범위가 현재 보호되는 범위와 다른 영역을 포함하는 경우, 이 메서드는 대신 명명된 범위를 포함하도록 보호를 이동합니다. 이름이 지정된 범위는 현재 보호된 범위와 동일한 시트에 있어야 합니다. 스크립트는 보호된 범위를 명명된 범위와 연결하려면 이 메서드를 명시적으로 호출해야 합니다. setRangeName(rangeName)를 호출하지 않고 Range.protect()를 호출하여 이름이 지정된 범위인 Range로부터 보호를 만드는 것만으로는 연결할 수 없습니다. 그러나 Google Sheets UI에서 이름이 지정된 범위에서 보호된 범위를 만들면 자동으로 연결됩니다.

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

매개변수

이름유형설명
rangeNameString보호할 이름이 지정된 범위의 이름입니다.

리턴

Protection: 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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

매개변수

이름유형설명
rangesRange[]보호된 시트 내에서 보호되지 않은 상태로 둘 범위 배열입니다.

리턴

Protection: 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • 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 - 연결을 위한 보호 설정을 나타내는 객체입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

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