首页 > 解决方案 > 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.

标签: google-apps-script

解决方案


setOwner 方法是 DriveApp 的一部分。

尝试这个:

function setOwner(){

  var emailAddress = 'user@domain.com';
  var id = SpreadsheetApp.getActiveSpreadsheet().getId();
  var file = DriveApp.getFileById(id).setOwner(emailAddress);
}

文档

编辑说明:您还需要启用高级 Google 服务 - https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services


推荐阅读