google-apps-script - 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.
解决方案
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
推荐阅读
- javascript - Vue.js - 如何按对象获取 JSON 数据对象?
- json - 如何将 JSON 文件从后端传递到前端
- python - panda DatetimeIndex 返回一个浮点值而不是 Integer
- javascript - 使用 html 本机 window.open 时,window.opener 始终为 null
- azure - 网络错误导致媒体下载中途失败 azure 媒体服务
- angular - 角度、材质侧导航和粘性工具栏
- javascript - 匹配任何后跟非数字的字符
- java - MainActivity.java:使用或覆盖已弃用的 API。使用 -Xlint:deprecation 重新编译以获取详细信息
- python - 如何从字符串列表中随机选择不包括数字的标记?
- sql - EXcel 宏不返回 SQL