简体   繁体   中英

Google Sheets determining if sheet is protected via scripting

I have written a script for a Spreadsheet with a lot of sheets. After a sheet is completed, it is duplicated and set to protected. I would like my script to run during onEdit() on any sheet which is not protected. Is there a way to determine if a sheet is protected?

Here is an example using getProtections:

 function onEdit(e) {
     var sheet = e.source.getActiveSheet()
     var protect = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)
     if (protect != "Protection") {
         // Run your code here. For example:
         SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A1").setValue("Unprotected");
     }
 }

Create a function named onEdit(e) and using the Spreadsheet getSheets() method you can access all sheets and determine if they have a protectionType of SHEET. If they do then just return immediate and don't process them in the onEdit(e) . The current sheet name can be determined using this code e.source.getActiveSheet().getName() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM