Jared Ackley
Jared Ackley

Reputation: 11

How to change the Owner of a google sheet automatically when a copy of the document is made

I am trying to set Owner permissions to a document once it is copied. The company I work for has a checklist document that is initiated to monitor tasks. We have a master document and when an employee needs to initiate their own checklist, they make a copy of the master and then edit the copy.

The problem is that when this happens they automatically become the owner of the file and then can edit any protected fields. I would like to set an onOpen() so that when the checklist is copied, they will be removed from Owner status and I will replace them.

I have tried using setUser but it does not seem like it is a valid method.

function onOpen() {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getOwner();

  var owner = ss.setOwner("email")

}

I receive the following:

TypeError: Cannot call method "setOwner" of null. (line 5, file "Set Owner").

I was hoping I would be able to run some script that will change the owner of the copied spreadsheet to myself once the document is opened so that all protections remain.

Upvotes: 0

Views: 477

Answers (1)

jbra95
jbra95

Reputation: 909

The setOwner method is part of the DriveApp.

Try this:

function setOwner(){

  var emailAddress = '[email protected]';
  var id = SpreadsheetApp.getActiveSpreadsheet().getId();
  var file = DriveApp.getFileById(id).setOwner(emailAddress);
}

Documentation

Edit note: You'll also need to enable Advanced Google services - https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services

Upvotes: 1

Related Questions