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[]उन टारगेट ऑडियंस के आईडी दिखाता है जो सुरक्षित रेंज में बदलाव कर सकती हैं.
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 — चेन वाला ऑब्जेक्ट, जो सुरक्षा सेटिंग को दिखाता है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

  • 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 — चेन वाला ऑब्जेक्ट, जो सुरक्षा सेटिंग को दिखाता है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

  • 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 — चेन वाला ऑब्जेक्ट, जो सुरक्षा सेटिंग को दिखाता है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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

addTargetAudience(audienceId)

तय की गई टारगेट ऑडियंस को, सुरक्षित रेंज के एडिटर के तौर पर जोड़ता है.

पैरामीटर

नामTypeब्यौरा
audienceIdStringजोड़ने के लिए टारगेट ऑडियंस का आईडी.

रिटर्न

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

रिटर्न

Booleantrue अगर स्प्रेडशीट के मालिकाना हक वाले डोमेन के सभी उपयोगकर्ताओं के पास, सुरक्षित की गई रेंज या शीट में बदलाव करने की अनुमति है; अगर उनके पास अनुमति नहीं है, तो 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();
  }
}

रिटर्न

Booleantrue, अगर उपयोगकर्ता के पास सुरक्षित की गई रेंज या शीट में बदलाव करने की अनुमति है, तो 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) को कॉल करना ज़रूरी है. किसी Range से सुरक्षा बनाने के लिए Range.protect() को कॉल करना, लेकिन setRangeName(rangeName) को कॉल किए बिना, उस रेंज से सुरक्षा बनाने के लिए Range.protect() को कॉल करना काफ़ी नहीं है. हालांकि, Google Sheets के यूज़र इंटरफ़ेस (यूआई) में नाम वाली रेंज से सुरक्षित रेंज बनाने पर, वे अपने-आप जुड़ जाती हैं.

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

उन टारगेट ऑडियंस के आईडी दिखाता है जो सुरक्षित रेंज में बदलाव कर सकती हैं.

रिटर्न

TargetAudience[] — टारगेट ऑडियंस के आईडी का कलेक्शन.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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

रिटर्न

Booleantrue, अगर सुरक्षित की गई रेंज या शीट में सिर्फ़ चेतावनी पर आधारित सुरक्षा की सुविधा का इस्तेमाल किया जा रहा है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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

पैरामीटर

नामTypeब्यौरा
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());
}

पैरामीटर

नामTypeब्यौरा
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);
}

पैरामीटर

नामTypeब्यौरा
emailAddressesString[]जिन उपयोगकर्ताओं को हटाना है उनके ईमेल पतों की कैटगरी.

रिटर्न

Protection — चेन करने के लिए, सुरक्षा सेटिंग को दिखाने वाला ऑब्जेक्ट

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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

removeTargetAudience(audienceId)

सुरक्षित की गई रेंज के एडिटर के तौर पर, टारगेट ऑडियंस को हटाता है.

पैरामीटर

नामTypeब्यौरा
audienceIdStringहटाई जाने वाली टारगेट ऑडियंस का आईडी.

रिटर्न

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

पैरामीटर

नामTypeब्यौरा
descriptionStringसुरक्षित की गई रेंज या शीट का ब्यौरा.

रिटर्न

Protection — चेन वाला ऑब्जेक्ट, जो सुरक्षा सेटिंग को दिखाता है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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

setDomainEdit(editable)

इससे यह सेट किया जाता है कि स्प्रेडशीट के मालिकाना हक वाले डोमेन के सभी उपयोगकर्ताओं के पास, सुरक्षित रेंज या शीट में बदलाव करने की अनुमति है या नहीं. ध्यान दें कि जिन उपयोगकर्ताओं के पास बदलाव करने की अनुमति है वे इस सेटिंग के बावजूद, सुरक्षित जगह में बदलाव कर सकते हैं. अगर स्प्रेडशीट किसी Google Workspace डोमेन (यानी कि अगर उस पर gmail.com खाता है) से जुड़ी न हो, तो यह एक अपवाद है.

पैरामीटर

नामTypeब्यौरा
editableBooleantrue अगर स्प्रेडशीट के मालिकाना हक वाले डोमेन के सभी उपयोगकर्ताओं के पास, सुरक्षित की गई रेंज या शीट में बदलाव करने की अनुमति होनी चाहिए. अगर ऐसा नहीं है, तो false.

रिटर्न

Protection — चेन करने के लिए, सुरक्षा सेटिंग को दिखाने वाला ऑब्जेक्ट

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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

setNamedRange(namedRange)

सुरक्षित की गई रेंज को नाम वाली किसी मौजूदा रेंज से जोड़ता है. अगर नाम वाली रेंज में मौजूदा सुरक्षित रेंज से किसी अलग इलाके को कवर किया जाता है, तो यह तरीका, सुरक्षा को मूव करने के बजाय नाम वाली रेंज को कवर करता है. नाम वाली रेंज उसी शीट पर होनी चाहिए जिस पर मौजूदा सुरक्षित रेंज है. ध्यान दें कि किसी सुरक्षित रेंज को किसी नाम वाली रेंज से जोड़ने के लिए, स्क्रिप्ट को इस तरीके को साफ़ तौर पर कॉल करना होगा. setRangeName(rangeName) को कॉल किए बिना, Range से सुरक्षा बनाने के लिए Range.protect() को कॉल करना काफ़ी नहीं है. हालांकि, Google Sheets के यूज़र इंटरफ़ेस (यूआई) में नाम वाली रेंज से सुरक्षित रेंज बनाने से, वे अपने-आप जुड़ जाती हैं.

// 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 — चेन वाला ऑब्जेक्ट, जो सुरक्षा सेटिंग को दिखाता है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

  • 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 — चेन वाला ऑब्जेक्ट, जो सुरक्षा सेटिंग को दिखाता है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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

setRangeName(rangeName)

सुरक्षित की गई रेंज को नाम वाली किसी मौजूदा रेंज से जोड़ता है. अगर नाम वाली रेंज में मौजूदा सुरक्षित रेंज से किसी अलग इलाके को कवर किया जाता है, तो यह तरीका, सुरक्षा को मूव करने के बजाय नाम वाली रेंज को कवर करता है. नाम वाली रेंज उसी शीट पर होनी चाहिए जिस पर मौजूदा सुरक्षित रेंज है. ध्यान दें कि किसी सुरक्षित रेंज को किसी नाम वाली रेंज से जोड़ने के लिए, स्क्रिप्ट को इस तरीके को साफ़ तौर पर कॉल करना होगा. setRangeName(rangeName) को कॉल किए बिना, Range से सुरक्षा बनाने के लिए Range.protect() को कॉल करना काफ़ी नहीं है. हालांकि, Google Sheets के यूज़र इंटरफ़ेस (यूआई) में नाम वाली रेंज से सुरक्षित रेंज बनाने से, वे अपने-आप जुड़ जाती हैं.

// 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 — चेन करने के लिए, सुरक्षा सेटिंग को दिखाने वाला ऑब्जेक्ट

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

  • 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 — चेन करने के लिए, सुरक्षा सेटिंग को दिखाने वाला ऑब्जेक्ट

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

  • 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 — चेन वाला ऑब्जेक्ट, जो सुरक्षा सेटिंग को दिखाता है.

अनुमति देना

इस तरीके का इस्तेमाल करने वाली स्क्रिप्ट को, इनमें से एक या ज़्यादा स्कोप से अनुमति देने की ज़रूरत होती है:

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