Class Protection

Protezione

Accedi e modifica intervalli e fogli protetti. Un intervallo protetto può proteggere un intervallo di celle statico o un intervallo denominato. Un foglio protetto può includere regioni non protette. Per i fogli di lavoro creati con la versione precedente di Fogli Google, utilizza invece la classe 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);
}

Metodi

MetodoTipo restituitoBreve descrizione
addEditor(emailAddress)ProtectionAggiunge l'utente specificato all'elenco di editor del foglio o dell'intervallo protetto.
addEditor(user)ProtectionAggiunge l'utente specificato all'elenco di editor del foglio o dell'intervallo protetto.
addEditors(emailAddresses)ProtectionAggiunge l'array specificato di utenti all'elenco degli editor per il foglio o l'intervallo protetto.
addTargetAudience(audienceId)ProtectionAggiunge il pubblico di destinazione specificato come editor dell'intervallo protetto.
canDomainEdit()BooleanDetermina se tutti gli utenti nel dominio proprietario del foglio di lavoro sono autorizzati a modificare l'intervallo o il foglio protetto.
canEdit()BooleanDetermina se l'utente è autorizzato a modificare l'intervallo o il foglio protetto.
getDescription()StringRecupera la descrizione dell'intervallo o del foglio protetto.
getEditors()User[]Restituisce l'elenco degli editor per l'intervallo o il foglio protetto.
getProtectionType()ProtectionTypeRecupera il tipo di area protetta, RANGE o SHEET.
getRange()RangeOttiene l'intervallo che viene protetto.
getRangeName()StringRestituisce il nome dell'intervallo protetto se è associato a un intervallo denominato.
getTargetAudiences()TargetAudience[]Restituisce gli ID dei segmenti di pubblico di destinazione che possono modificare l'intervallo protetto.
getUnprotectedRanges()Range[]Recupera un array di intervalli non protetti all'interno di un foglio protetto.
isWarningOnly()BooleanDetermina se l'area protetta utilizza una protezione "basata su avvisi".
remove()voidRimuove la protezione dell'intervallo o del foglio.
removeEditor(emailAddress)ProtectionRimuove l'utente specificato dall'elenco degli editor per il foglio o l'intervallo protetto.
removeEditor(user)ProtectionRimuove l'utente specificato dall'elenco degli editor per il foglio o l'intervallo protetto.
removeEditors(emailAddresses)ProtectionRimuove l'array specificato di utenti dall'elenco degli editor per il foglio o l'intervallo protetto.
removeTargetAudience(audienceId)ProtectionRimuove il pubblico di destinazione specificato come editor dell'intervallo protetto.
setDescription(description)ProtectionImposta la descrizione dell'intervallo o del foglio protetto.
setDomainEdit(editable)ProtectionConsente di stabilire se tutti gli utenti nel dominio a cui appartiene il foglio di lavoro sono autorizzati a modificare l'intervallo o il foglio protetto.
setNamedRange(namedRange)ProtectionAssocia l'intervallo protetto a un intervallo denominato esistente.
setRange(range)ProtectionRegola l'intervallo da proteggere.
setRangeName(rangeName)ProtectionAssocia l'intervallo protetto a un intervallo denominato esistente.
setUnprotectedRanges(ranges)ProtectionConsente di annullare la protezione dell'array specificato di intervalli all'interno di un foglio protetto.
setWarningOnly(warningOnly)ProtectionConsente di specificare se questo intervallo protetto utilizza o meno la protezione "basata su avvisi".

Documentazione dettagliata

addEditor(emailAddress)

Aggiunge l'utente specificato all'elenco di editor del foglio o dell'intervallo protetto. Questo metodo non concede automaticamente all'utente l'autorizzazione a modificare il foglio di lavoro stesso; per farlo, chiama 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());
}

Parametri

NomeTipoDescrizione
emailAddressStringL'indirizzo email dell'utente da aggiungere.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

addEditor(user)

Aggiunge l'utente specificato all'elenco di editor del foglio o dell'intervallo protetto. Questo metodo non concede automaticamente all'utente l'autorizzazione a modificare il foglio di lavoro stesso; per farlo, chiama 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());
}

Parametri

NomeTipoDescrizione
userUserUna rappresentazione dell'utente da aggiungere.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

addEditors(emailAddresses)

Aggiunge l'array specificato di utenti all'elenco degli editor per il foglio o l'intervallo protetto. Questo metodo non concede automaticamente agli utenti l'autorizzazione a modificare il foglio di lavoro stesso. A tale scopo, devi anche chiamare 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());
}

Parametri

NomeTipoDescrizione
emailAddressesString[]Un array di indirizzi email degli utenti da aggiungere.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

addTargetAudience(audienceId)

Aggiunge il pubblico di destinazione specificato come editor dell'intervallo protetto.

Parametri

NomeTipoDescrizione
audienceIdStringL'ID del pubblico di destinazione da aggiungere.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

canDomainEdit()

Determina se tutti gli utenti nel dominio proprietario del foglio di lavoro sono autorizzati a modificare l'intervallo o il foglio protetto. Genera un'eccezione se l'utente non dispone dell'autorizzazione per modificare l'intervallo o il foglio protetto.

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

Ritorni

Boolean: true se tutti gli utenti nel dominio a cui appartiene il foglio di lavoro dispongono dell'autorizzazione per modificare l'intervallo o il foglio protetto; false in caso contrario.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

canEdit()

Determina se l'utente è autorizzato a modificare l'intervallo o il foglio protetto. Il proprietario del foglio di lavoro è sempre in grado di modificare intervalli e fogli protetti.

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

Ritorni

Boolean: true se l'utente è autorizzato a modificare l'intervallo o il foglio protetto; in caso contrario false

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

getDescription()

Recupera la descrizione dell'intervallo o del foglio protetto. Se non è impostata alcuna descrizione, questo metodo restituisce una stringa vuota.

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

Ritorni

String: la descrizione dell'intervallo o del foglio protetto oppure una stringa vuota se non è impostata una descrizione.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

getEditors()

Restituisce l'elenco degli editor per l'intervallo o il foglio protetto. Genera un'eccezione se l'utente non dispone dell'autorizzazione per modificare l'intervallo o il foglio protetto.

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

Ritorni

User[]: un array di utenti con l'autorizzazione a modificare l'intervallo o il foglio protetto

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

getProtectionType()

Recupera il tipo di area protetta, RANGE o 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());

Ritorni

ProtectionType: il tipo di area protetta, RANGE o SHEET.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

getRange()

Ottiene l'intervallo che viene protetto. Se la protezione si applica al foglio anziché a un intervallo, questo metodo restituisce un intervallo che si estende sull'intero foglio.

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

Ritorni

Range: l'intervallo da proteggere.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

getRangeName()

Restituisce il nome dell'intervallo protetto se è associato a un intervallo denominato. Restituisce null se la protezione non è associata a un intervallo denominato. Tieni presente che gli script devono chiamare esplicitamente setRangeName(rangeName) per associare un intervallo protetto a un intervallo denominato. Chiamare Range.protect() per creare una protezione da un Range che sembra essere un intervallo denominato senza chiamare setRangeName(rangeName), non è sufficiente per associarli. Tuttavia, la creazione di un intervallo protetto da un intervallo denominato nell'interfaccia utente di Fogli Google li associa automaticamente.

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

Ritorni

String: il nome dell'intervallo denominato protetto oppure null se l'intervallo protetto non è associato a un intervallo denominato

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

getTargetAudiences()

Restituisce gli ID dei segmenti di pubblico di destinazione che possono modificare l'intervallo protetto.

Ritorni

TargetAudience[]: un array di ID dei segmenti di pubblico di destinazione.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

getUnprotectedRanges()

Recupera un array di intervalli non protetti all'interno di un foglio protetto. Se l'oggetto Protection corrisponde a un intervallo protetto anziché a un foglio protetto, questo metodo restituisce un array vuoto. Per modificare gli intervalli non protetti, utilizza setUnprotectedRanges(ranges) per impostare un nuovo array di intervalli; per proteggere nuovamente l'intero foglio, imposta un array vuoto.

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

Ritorni

Range[]: un array di intervalli non protetti all'interno di un foglio protetto

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

isWarningOnly()

Determina se l'area protetta utilizza una protezione "basata su avvisi". La protezione basata su avvisi significa che ogni utente può modificare i dati nell'area, ad eccezione del fatto che la modifica richiede un avviso che chiede all'utente di confermare la modifica. Per impostazione predefinita, gli intervalli o i fogli protetti non sono basati su avvisi. Per passare allo stato di avviso, utilizza 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);

Ritorni

Boolean: true se l'intervallo o il foglio protetto utilizza solo la protezione basata su avvisi.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

remove()

Rimuove la protezione dell'intervallo o del foglio.

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

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

removeEditor(emailAddress)

Rimuove l'utente specificato dall'elenco degli editor per il foglio o l'intervallo protetto. Tieni presente che se l'utente è membro di un gruppo Google con autorizzazione di modifica o se tutti gli utenti del dominio dispongono di questa autorizzazione, l'utente sarà comunque in grado di modificare l'area protetta. Non è possibile rimuovere né il proprietario del foglio di lavoro né l'utente corrente.

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

Parametri

NomeTipoDescrizione
emailAddressStringL'indirizzo email dell'utente da rimuovere.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

removeEditor(user)

Rimuove l'utente specificato dall'elenco degli editor per il foglio o l'intervallo protetto. Tieni presente che se l'utente è membro di un gruppo Google con autorizzazione di modifica o se tutti gli utenti del dominio dispongono di questa autorizzazione, l'utente è comunque in grado di modificare l'area protetta. Non è possibile rimuovere né il proprietario del foglio di lavoro né l'utente corrente.

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

Parametri

NomeTipoDescrizione
userUserUna rappresentazione dell'utente da rimuovere.

Ritorni

Protection: l'oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

removeEditors(emailAddresses)

Rimuove l'array specificato di utenti dall'elenco degli editor per il foglio o l'intervallo protetto. Tieni presente che, se gli utenti sono membri di un gruppo Google che dispone dell'autorizzazione di modifica o se tutti gli utenti del dominio dispongono di questa autorizzazione, tali utenti possono comunque modificare l'area protetta. Non è possibile rimuovere né il proprietario del foglio di lavoro né l'utente corrente.

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

Parametri

NomeTipoDescrizione
emailAddressesString[]Un array di indirizzi email degli utenti da rimuovere.

Ritorni

Protection: l'oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

removeTargetAudience(audienceId)

Rimuove il pubblico di destinazione specificato come editor dell'intervallo protetto.

Parametri

NomeTipoDescrizione
audienceIdStringL'ID del pubblico di destinazione da rimuovere.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

setDescription(description)

Imposta la descrizione dell'intervallo o del foglio protetto.

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

Parametri

NomeTipoDescrizione
descriptionStringLa descrizione dell'intervallo o del foglio protetto.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

setDomainEdit(editable)

Consente di stabilire se tutti gli utenti nel dominio a cui appartiene il foglio di lavoro sono autorizzati a modificare l'intervallo o il foglio protetto. Tieni presente che tutti gli utenti che dispongono dell'autorizzazione di modifica esplicita possono modificare l'area protetta indipendentemente da questa impostazione. Genera un'eccezione se il foglio di lavoro non appartiene a un dominio Google Workspace (ovvero, se è di proprietà di un account gmail.com).

Parametri

NomeTipoDescrizione
editableBooleantrue se tutti gli utenti nel dominio a cui appartiene il foglio di lavoro devono disporre dell'autorizzazione per modificare l'intervallo o il foglio protetto; in caso contrario false.

Ritorni

Protection: l'oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

setNamedRange(namedRange)

Associa l'intervallo protetto a un intervallo denominato esistente. Se l'intervallo denominato copre un'area diversa dall'intervallo protetto corrente, questo metodo sposta la protezione in modo da coprire l'intervallo denominato. L'intervallo denominato deve trovarsi sullo stesso foglio dell'intervallo protetto corrente. Tieni presente che gli script devono chiamare esplicitamente questo metodo per associare un intervallo protetto a un intervallo denominato. Chiamare Range.protect() per creare una protezione da un Range che è un intervallo denominato senza chiamare setRangeName(rangeName) non è sufficiente per associarli. Tuttavia, la creazione di un intervallo protetto da un intervallo denominato nell'interfaccia utente di Fogli Google li associa automaticamente.

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

Parametri

NomeTipoDescrizione
namedRangeNamedRangeL'intervallo denominato esistente da associare all'intervallo protetto.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

setRange(range)

Regola l'intervallo da proteggere. Se l'intervallo specificato copre un'area diversa dall'intervallo protetto corrente, questo metodo sposta la protezione in modo da coprire il nuovo intervallo.

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

Parametri

NomeTipoDescrizione
rangeRangeIl nuovo intervallo da proteggere dalle modifiche.

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

setRangeName(rangeName)

Associa l'intervallo protetto a un intervallo denominato esistente. Se l'intervallo denominato copre un'area diversa dall'intervallo protetto corrente, questo metodo sposta la protezione in modo da coprire l'intervallo denominato. L'intervallo denominato deve trovarsi sullo stesso foglio dell'intervallo protetto corrente. Tieni presente che gli script devono chiamare esplicitamente questo metodo per associare un intervallo protetto a un intervallo denominato. Chiamare Range.protect() per creare una protezione da un Range che è un intervallo denominato senza chiamare setRangeName(rangeName) non è sufficiente per associarli. Tuttavia, la creazione di un intervallo protetto da un intervallo denominato nell'interfaccia utente di Fogli Google li associa automaticamente.

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

Parametri

NomeTipoDescrizione
rangeNameStringIl nome dell'intervallo denominato da proteggere.

Ritorni

Protection: l'oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

setUnprotectedRanges(ranges)

Consente di annullare la protezione dell'array specificato di intervalli all'interno di un foglio protetto. Genera un'eccezione se l'oggetto Protection corrisponde a un intervallo protetto anziché a un foglio protetto o se uno qualsiasi degli intervalli non si trova sul foglio protetto. Per modificare gli intervalli non protetti, imposta un nuovo array di intervalli; per proteggere nuovamente l'intero foglio, imposta un array vuoto.

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

Parametri

NomeTipoDescrizione
rangesRange[]L'array di intervalli da lasciare non protetti all'interno di un foglio protetto.

Ritorni

Protection: l'oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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

setWarningOnly(warningOnly)

Consente di specificare se questo intervallo protetto utilizza o meno la protezione "basata su avvisi". La protezione basata sugli avvisi fa sì che ogni utente possa modificare i dati nell'area, ad eccezione del fatto che la modifica richiede un avviso che chiede all'utente di confermare la modifica. Per impostazione predefinita, gli intervalli o i fogli protetti non sono basati sugli avvisi. Per controllare lo stato di avviso, usa 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());

Parametri

NomeTipoDescrizione
warningOnlyBoolean

Ritorni

Protection: oggetto che rappresenta le impostazioni di protezione, per il concatenamento.

Autorizzazione

Gli script che utilizzano questo metodo richiedono l'autorizzazione con uno o più dei seguenti ambiti:

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