Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

google apps script - How to duplicate a sheet with protected ranges?

I have built a template sheet that I will duplicate every week. I noticed that when I go to duplicate it, none the protected ranges are copied along with it. Is it possible to use a Google Apps script to copy the exact same protected ranges into the duplicated sheet? It would save me lots of time every week.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Note: A similar question was posted on Web Apps, so I am adapting a part of my answer there to the present case.


Using sheet.getProtections method, you can get the array of protections on a given sheet, and loop over them, creating their analogs on the target sheet. This is somewhat annoying because there seems to be no method to simply clone a protection to another range. (One can change the range of protection, but that would move it to the new range, instead of copying.)

So, in the function below I do the following:

  1. Get the A1 notation of each protected range with p.getRange().getA1Notation();
  2. Protect the corresponding range of the target sheet with p2 = sheet2.getRange(rangeNotation).protect();
  3. Set the properties of new protection p2 according to the properties of original protection p. This includes removing/adding editors if the protection is not just of the warning type.
function duplicateSheetWithProtections() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getSheetByName('Template');
  var sheet2 = sheet.copyTo(ss).setName('My Copy'); 
  var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  for (var i = 0; i < protections.length; i++) {
    var p = protections[i];
    var rangeNotation = p.getRange().getA1Notation();
    var p2 = sheet2.getRange(rangeNotation).protect();
    p2.setDescription(p.getDescription());
    p2.setWarningOnly(p.isWarningOnly());
    if (!p.isWarningOnly()) {
      p2.removeEditors(p2.getEditors());  // remove editors 
      p2.addEditors(p.getEditors());      // except those permitted for original
      // p2.setDomainEdit(p.canDomainEdit()); //  only if using an Apps domain 
    }
  }
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...