File.setOwner()返回“不允许动作".将所有权转让给编辑者时 [英] File.setOwner() returns "Action not allowed" when Transferring ownership to an Editor

查看:79
本文介绍了File.setOwner()返回“不允许动作".将所有权转让给编辑者时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google脚本,该脚本用于从Google电子表格内部存储的数据生成文档.生成文档后,我尝试将所有者更改为其他gmail帐户,但我不断收到以下错误:

I have a google script that is used to generate a document from data stored inside of a google spreadsheet. After the document is generated, I am trying to change the owner to a different gmail account but I keep getting the following error:

[15-09-23 09:55:09:480 PDT] File.setOwner([bob@gmail.com])[1.03秒]

[15-09-23 09:55:09:480 PDT] File.setOwner([bob@gmail.com]) [1.03 seconds]

[15-09-23 09:55:09:566 PDT]执行失败:不允许执行操作(第82行,文件"Helpers")[5.401秒,总运行时间]

[15-09-23 09:55:09:566 PDT] Execution failed: Action not allowed (line 82, file "Helpers") [5.401 seconds total runtime]

我试图了解为什么我不能使用Google App脚本更改所有者,但可以通过Google云端硬盘手动进行操作.

I am trying to understand why I can't change the owner using Google App Script, but I can manually through Google Drive.

详细信息:

Bob(Bob@gmail.com)与他的爱丽丝(Alice@gmail.com)共享了他的云端硬盘中的目录.共享目录内部是一个电子表格,该电子表格用于根据电子表格的内容生成发票.爱丽丝使用电子表格(她具有编辑权限)并调用生成>发票"电子表格上的菜单操作以创建发票.由于她调用了该函数,因此以Alice作为所有者创建了文件,但是Bob希望成为所有发票的所有者. Bob试图在创建文档后立即使用Google App脚本设置文档的所有者,但它会返回上面记录的错误.

Bob (Bob@gmail.com) has shared a directory on his Drive with Alice (Alice@gmail.com). Inside the shared directory is a spreadsheet that is used to generate invoices from the content of the spreadsheet. Alice uses the spreadsheet (she has edit permissions) and invokes the "Generate > Invoice" menu action on the spreadsheet to create the invoice. Since she invoked the function, the file is created with Alice as the owner, but Bob wants to be the owner of all invoices. Bob has tried to set the owner of the document using Google App Script right after the document is created but it returns the error documented above.

电子表格内容:

 |     A      |     B     |
1| First Name | Last Name |
2| John       | Doe       |

Google App脚本:

Google App Script:

/**
 * Adds a menu-item to allow the generation of documents
 *
 * @param {Object} e The event parameter for a simple onOpen trigger.
 */
function onOpen(e) {
  var menuitems = [];
  menuitems.push({
    name: "Invoice",
    functionName: "createInvoice"
  });
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.addMenu("Generate", menuitems);
}


/**
 * This function reads data from cells A2:A3 to dynamically generate a document
 *
 */
function createInvoice() {
  var this_folder = cwd();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var content = sheet.getRange("A2:A3").getValues()[0].join(" ");

  // Create a file with the given content
  var file = DriveApp.createFile("Invoice", content);

  // Add the file to the shared folder
  this_folder.addFile(file);

  // Remove the file from the user's root directory
  DriveApp.getRootFolder().removeFile(file);

  // Log the current owner of the document
  Logger.log(file.getOwner().getName());

  // Change the owner of the document from the active user to Bob. 
  file.setOwner("bob@gmail.com");
}


/**
 * This is a helper function used to get the directory of the active spreadsheet.
 *
 */
function cwd() {
  var this_file = DriveApp.getFileById(SpreadsheetApp.getActive().getId());
  return this_file.getParents().next();
}


复制步骤:


Steps to Reproduce:

  1. 从一个gmail帐户中,与另一个gmail帐户共享一个文件夹.
  2. 使用主要的gmail帐户在共享文件夹中创建电子表格:
  3. 将Google App脚本添加到电子表格中(在将setOwner行更改为适当的电子邮件地址之后).
  4. 授予对其他gmail帐户的写访问权限.
  5. 登录并从其他gmail帐户加载文档.
  6. 调用脚本(生成>发票).第一次执行此操作时,系统会要求您提供权限.
  7. 脚本应错误并显示以下错误:

不允许采取行动.

Action not allowed.

  1. 如果您在调用脚本后打开脚本并查看日志,则会显示其他gmail帐户拥有该文档,并且应该能够对其进行更改.


其他信息:


Additional Information:

  1. 如果我删除了"file.setOwner()",方法中,文件已创建,我可以通过Web界面手动设置更改所有者.
  2. 使用脚本和设置,我已经动态创建了文件夹,并且能够使用Google App Script更改所有者.此问题似乎只影响文件上的setOwner()方法.

推荐答案

将文件放在Google云端硬盘的Team Drive文件夹中时,可能会发生此错误.

This error likely happens when the file is placed inside the Team Drive folder of Google Drive.

这篇关于File.setOwner()返回“不允许动作".将所有权转让给编辑者时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆