Class Protection

Protezione

Accedere e modificare intervalli e fogli protetti. Un intervallo protetto può proteggere un intervallo di celle o un intervallo denominato. Un foglio protetto potrebbe includere aree non protette. Per fogli di lavoro creati con la versione precedente di Fogli Google, utilizza l'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 per il foglio o l'intervallo protetto.
addEditor(user)ProtectionAggiunge l'utente specificato all'elenco di editor per il foglio o l'intervallo protetto.
addEditors(emailAddresses)ProtectionAggiunge l'array di utenti specificato all'elenco di 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 hanno l'autorizzazione alla modifica l'intervallo o il foglio protetto.
canEdit()BooleanDetermina se l'utente dispone dell'autorizzazione per modificare l'intervallo o il foglio protetto.
getDescription()StringRecupera la descrizione dell'intervallo o del foglio protetto.
getEditors()User[]Recupera l'elenco di editor per l'intervallo o il foglio protetti.
getProtectionType()ProtectionTypeConsente di acquisire 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 un sistema "basato su avvisi" protezione dei dati.
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 di utenti specificato 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)ProtectionConsente di impostare la descrizione dell'intervallo o del foglio protetto.
setDomainEdit(editable)ProtectionConsente di impostare se tutti gli utenti nel dominio proprietario del foglio di lavoro hanno l'autorizzazione per modificare il intervallo o foglio protetto.
setNamedRange(namedRange)ProtectionAssocia l'intervallo protetto a un intervallo denominato esistente.
setRange(range)ProtectionConsente di regolare l'intervallo da proteggere.
setRangeName(rangeName)ProtectionAssocia l'intervallo protetto a un intervallo denominato esistente.
setUnprotectedRanges(ranges)ProtectionRimuove la protezione dell'array specificato di intervalli all'interno di un foglio protetto.
setWarningOnly(warningOnly)ProtectionImposta se l'intervallo protetto utilizza o meno il criterio "basato su avvisi" protezione dei dati.

Documentazione dettagliata

addEditor(emailAddress)

Aggiunge l'utente specificato all'elenco di editor per il foglio o l'intervallo protetto. Questo metodo non concedi automaticamente all'utente l'autorizzazione a modificare il foglio di lavoro; per farlo in addizione, 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.

Invio

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

addEditor(user)

Aggiunge l'utente specificato all'elenco di editor per il foglio o l'intervallo protetto. Questo metodo non concedi automaticamente all'utente l'autorizzazione a modificare il foglio di lavoro; per farlo in addizione, 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.

Invio

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

addEditors(emailAddresses)

Aggiunge l'array di utenti specificato all'elenco di editor per il foglio o l'intervallo protetto. Questo non concede automaticamente agli utenti l'autorizzazione a modificare il foglio di lavoro; da fare che, inoltre, chiami 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.

Invio

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

addTargetAudience(audienceId)

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

Parametri

NomeTipoDescrizione
audienceIdStringL'ID del pubblico di destinazione da aggiungere.

Invio

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

canDomainEdit()

Determina se tutti gli utenti nel dominio proprietario del foglio di lavoro hanno l'autorizzazione alla modifica l'intervallo o il foglio protetto. Genera un'eccezione se l'utente non dispone dell'autorizzazione per la modifica 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());

Invio

Boolean - true se tutti gli utenti nel dominio proprietario del foglio di lavoro hanno l'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 dispone dell'autorizzazione per modificare l'intervallo o il foglio protetto. La proprietario dei fogli 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();
  }
}

Invio

Boolean - true se l'utente dispone 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

getDescription()

Recupera la descrizione dell'intervallo o del foglio protetto. Se non viene 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);

Invio

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

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

Recupera l'elenco di editor per l'intervallo o il foglio protetti. Genera un'eccezione se l'utente lo fa non disponi 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);
}

Invio

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

Consente di acquisire 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());

Invio

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 intervallo, questo metodo restituisce un intervallo che copre l'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());

Invio

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 indicare chiama setRangeName(rangeName) per associare un intervallo protetto a un intervallo denominato; chiamata Range.protect() per creare una protezione da un Range che è un un intervallo denominato, senza chiamare setRangeName(rangeName), non è sufficiente da associare che li rappresentano. Tuttavia, la creazione di un intervallo protetto da un intervallo denominato nell'interfaccia utente di Fogli Google 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.

Invio

String: il nome dell'intervallo denominato protetto o null se l'intervallo protetto non è associata 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.

Invio

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 un array di dati. Per modificare gli intervalli non protetti, utilizza setUnprotectedRanges(ranges) per impostare un 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);

Invio

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 un sistema "basato su avvisi" protezione dei dati. Protezione basata sugli avvisi significa che ogni utente può modificare i dati nell'area, ma la modifica mostra un avviso che chiede al per confermare la modifica. Per impostazione predefinita, i fogli o gli intervalli protetti non sono basati su avvisi. A passa 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);

Invio

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 dispone dell'autorizzazione di modifica, l'utente può comunque modificare l'area protetta. Né il proprietario del foglio di lavoro né l'utente corrente può essere rimosso.

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

Invio

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

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 dispone dell'autorizzazione di modifica, l'utente può comunque modificare anche l'area protetta. Né le il proprietario del foglio di lavoro o 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.

Invio

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 di utenti specificato dall'elenco degli editor per il foglio o l'intervallo protetto. Tieni presente che se alcuni utenti fanno parte di un gruppo Google che ha l'autorizzazione di modifica o se tutti utenti nel dominio dispongono dell'autorizzazione di modifica, possono comunque modificare geografica specifica. Né il proprietario del foglio di lavoro né l'utente corrente possono essere rimossi.

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

Invio

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.

Invio

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

setDescription(description)

Consente di impostare 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.

Invio

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

setDomainEdit(editable)

Consente di impostare se tutti gli utenti nel dominio proprietario del foglio di lavoro hanno l'autorizzazione per modificare il intervallo o foglio protetto. Tieni presente che gli utenti con autorizzazione di modifica esplicita possono modificare l'area protetta indipendentemente da questa impostazione. Genera un'eccezione se il foglio di lavoro non appartengono a un dominio Google Workspace (ovvero se è di proprietà di un account gmail.com).

Parametri

NomeTipoDescrizione
editableBooleantrue se tutti gli utenti nel dominio proprietario del foglio di lavoro devono avere l'autorizzazione per modificare l'intervallo o il foglio protetto; false in caso contrario.

Invio

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 una un'area diversa dall'intervallo protetto corrente, questo metodo sposta la protezione per coprire per l'intervallo denominato. L'intervallo denominato deve trovarsi sullo stesso foglio dell'intervallo attualmente protetto intervallo. Tieni presente che gli script devono chiamare esplicitamente questo metodo per associare un intervallo protetto a un intervallo denominato; chiamata Range.protect() per creare una protezione da un Range che è un intervallo denominato, senza chiamare setRangeName(rangeName), non è sufficienti per associarli. Tuttavia, la creazione di un intervallo protetto da un intervallo denominato nel nella UI di Fogli Google vengono associate 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.

Invio

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

setRange(range)

Consente di regolare l'intervallo da proteggere. Se l'intervallo specificato copre un'area diversa dalla nell'intervallo attualmente protetto, 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.

Invio

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

setRangeName(rangeName)

Associa l'intervallo protetto a un intervallo denominato esistente. Se l'intervallo denominato copre una un'area diversa dall'intervallo protetto corrente, questo metodo sposta la protezione per coprire per l'intervallo denominato. L'intervallo denominato deve trovarsi sullo stesso foglio dell'intervallo attualmente protetto intervallo. Tieni presente che gli script devono chiamare esplicitamente questo metodo per associare un intervallo protetto a un intervallo denominato; chiamata Range.protect() per creare una protezione da un Range che è un intervallo denominato, senza chiamare setRangeName(rangeName), non è sufficienti per associarli. Tuttavia, la creazione di un intervallo protetto da un intervallo denominato nel nella UI di Fogli Google vengono associate 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.

Invio

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)

Rimuove 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 se nessuno degli intervalli si trova nel foglio protetto. Per modificare gli intervalli non protetti, imposta un nuovo un 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 protetto all'interno di un foglio protetto.

Invio

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)

Imposta se l'intervallo protetto utilizza o meno il criterio "basato su avvisi" protezione dei dati. Basate su avvisi di protezione significa che ogni utente può modificare i dati nell'area, ad eccezione della modifica che mostra un avviso chiedendo all'utente di confermare la modifica. Per impostazione predefinita, i fogli o gli intervalli protetti non sono basato sugli avvisi. Per controllare lo stato di avviso, utilizza 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

Invio

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