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

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

问题描述

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

<块引用>

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

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

我试图了解为什么我不能使用 Google App Script 更改所有者,但我可以通过 Google Drive 手动更改.


详情:

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


电子表格内容:

<代码> |一个 |乙|1|名字 |姓氏 |2|约翰 |能源部 |

谷歌应用脚​​本:

/*** 添加一个菜单项以允许生成文档** @param {Object} e 简单 onOpen 触发器的事件参数.*/函数 onOpen(e) {var menuitems = [];menuitems.push({名称:发票",函数名称:创建发票";});var ss = SpreadsheetApp.getActiveSpreadsheet();ss.addMenu(生成", menuitems);}/*** 此函数从单元格 A2:A3 读取数据以动态生成文档**/函数创建发票(){var this_folder = cwd();var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");var content = sheet.getRange(A2:A3").getValues()[0].join(");//创建一个包含给定内容的文件var file = DriveApp.createFile("Invoice", content);//将文件添加到共享文件夹this_folder.addFile(file);//从用户的根目录中删除文件DriveApp.getRootFolder().removeFile(file);//记录文档的当前所有者Logger.log(file.getOwner().getName());//将文档的所有者从活动用户更改为 Bob.file.setOwner(bob@gmail.com");}/*** 这是一个辅助函数,用于获取活动电子表格的目录.**/函数 cwd() {var this_file = DriveApp.getFileById(SpreadsheetApp.getActive().getId());返回 this_file.getParents().next();}


复制步骤:

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

<块引用>

不允许操作.

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


附加信息:

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

解决方案

来自问题(强调我的)

<块引用>

Alice 使用电子表格(她具有编辑权限)并调用电子表格上的生成 > 发票"菜单操作来创建发票.由于她调用了该函数,该文件是以 Alice 作为所有者创建的,但 Bob 希望成为所有发票的所有者.Bob 已尝试在文档创建后立即使用 Google 应用脚本设置文档的所有者,但它返回了上面记录的错误.

Bob"不能转让文件的所有权,因为他不是所有者.更简单的方法是让文件的实际所有者Alice"转让文件所有权.

为防止出现此问题,Bob"可以使用 Google Apps 脚本设置创建一个 Web 应用程序,而不是使用自定义菜单,而该应用程序将作为他执行.

G Suite 帐户的另一种替代方法是创建一个具有域范围授权的服务帐户,以冒充Alice"来转移文件的所有权.有关详细信息,请参阅 Peter Herman 对 在 Google Apps 脚本中将文件的所有权转让给其他用户

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 seconds]

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

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


Details:

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.


Spreadsheet contents:

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

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. From a gmail account, share a folder with another gmail account.
  2. Create the Spreadsheet in the shared folder with the main gmail account:
  3. Add the Google App Script to the spreadsheet (AFTER changeing the setOwner line to the appropriate email address).
  4. Grant write access to the other gmail account.
  5. Login and load the document from the other gmail account.
  6. Invoke the script (Generate > Invoice). The first time you do this it will ask for permissions.
  7. The script should error out with the following error:

Action not allowed.

  1. If you open the Script and review the logs after you invoke the script it will show that the other gmail account owns the document and should be able to change it.


Additional Information:

  1. If I remove the "file.setOwner()" method, the file is created and I can manually set change the owner through the web interface.
  2. Using the script and setup, I have dynamically created folders and been able to change the owner using Google App Script. This problem only appears to effect the setOwner() method on Files.

解决方案

From the question (emphasis mine)

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.

"Bob" can't transfer the ownership of the files because he is not the owner. The easier way is to ask "Alice", the actual file owner, to transfer the file ownership.

To prevent this problem, instead of using a custom menu "Bob" could create a web application with Google Apps Script setting it be be executed as him.

Another alternative for G Suite accounts is to create a service account with domain-wide delegation of authority to impersonate "Alice" to do the transfer of the ownership of the files. For details see the Peter Herman's answer to Transfer ownership of a file to another user in Google Apps Script

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

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