0

I am currently working on trying to make duplicates with permissions using google spreadsheet. There was a link that I saw that explained how to approach this(Google Sheet Duplicate with Permission). I used scenario 2 in which it explains the permission for it. The code below is what the responder came up with and is something that I want to implement for the situation that I am trying to work on.

function duplicateSheetWithProtections() {
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    sheet = ss.getSheetByName('Template');
    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());
            p2.addEditors(p.getEditors());
           // p2.setDomainEdit(p.canDomainEdit()); //  only if using an Apps domain 
        } 
    }
} 

However, I am running into an issue where I get an error in line 4 from the response the user put in the link I have attached. In line 4 I get an error that says

TypeError: Cannot read property 'copyTo' of null (line 4, file "Code")

I have been trying to debug the problem, but I still not understanding why the problem still occurs. Would definitely like to know what I missed or did wrong.

1 Answer 1

0

The referred error message means that ss.getSheetByName('Template') in

sheet = ss.getSheetByName('Template');

returned null. This occurred because your spreadsheet doesn't include a sheet named Template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.