SpreadsheetApp.openByUrl和openById会给出错误 [英] SpreadsheetApp.openByUrl and openById give errors

查看:370
本文介绍了SpreadsheetApp.openByUrl和openById会给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在使用Google电子表格和应用程序脚本编写的应用程序中遇到了问题。

摘要:
使用SpreadsheetApp.openByUrl()的任何脚本,和SpreadsheetApp.openById()在脚本编辑器中调用时可以正常工作,但在从内部调用时会发出错误,即使是内部具有该脚本的同一电子表格也是如此。



错误消息:

错误
您没有执行该操作的权限。 (行?)。



重现它的步骤:


  1. 制作新的Google电子表格

  2. 打开其脚本编辑器(工具菜单> 脚本编辑器...
    $ b
  3. 选择空白项目

  4. 粘贴此代码

  5. pre> function demo1(){
    returncat;


    函数demo2(){
    return [[bob,fred],[x,y]];


    函数demo3(){
    Logger.log(demo3-1)
    ss = SpreadsheetApp.openByUrl([== URL ==] )
    name = ss.getName()
    Logger.log(demo3-2+ name)
    返回名称
    }

    函数demo4 (){
    Logger.log(demo4-1)
    ss = SpreadsheetApp.openById([== id ==])
    name = ss.getName()
    Logger.log(demo4-2+ name)
    返回名称
    }


  6. 更改[== URL ==]和[== id ==]以引用您自己的文档。 保存脚本

  7. p>
  8. 运行菜单中选择 demo1 。这会引发一个关于特权的问题。授予它所要求的特权。 (请求访问您的Google Drive)

  9. >将以下单元格设置为具有以下公式:

     A1:= demo1()
    C2:= demo2()
    F1: = demo3()
    h1:= demo4()


注意前两项工作,所以电子表格看到了脚本项目并能够从中运行代码(因此它有权限从该脚本运行代码),但后两项不起作用并抱怨它没有执行该操作的权限。



那么我该如何解决这个问题?我做错了什么或者是否存在潜在的问题?

解决方案

这个错误是由于谷歌最近推出的一项更改禁止在自定义函数中使用SpreadsheetApp.openByUrl()(或SpreadsheetApp.openById()。
您仍然可以从其他上下文(如菜单项,触发器等)使用此方法。Google必须出于安全原因推出此更改他们将无法恢复到原来的行为,所以唯一可能的解决方法可能是将函数重写为'普通'GAS函数,而不是自定义函数。



此问题列于问题跟踪器


So I have encountered a problem in the application I am writing using google spreadsheets and app-script. I have put together a step by step guid to reproduce it.

Summary: any script that uses SpreadsheetApp.openByUrl() and SpreadsheetApp.openById() works OK when called from inside the script editor but gives errors when called from inside even the same spreadsheet that has that script inside it.

Error Messages:

Error
    You do not have permission to perform that action. (line ?).
    

Steps to reproduce it:

  1. Make a new Google Spreadsheet
  2. Open its Script Editor ( Tools menu >> Script Editor... )
  3. Choose Blank Project
  4. Paste in this code

    function demo1() {
      return "cat";
    }
    
    function demo2() {
      return [["bob", "fred"], ["x", "y"]];
    }
    
    function demo3() {
      Logger.log("demo3-1")
      ss = SpreadsheetApp.openByUrl("[==URL==]")
      name = ss.getName()
      Logger.log("demo3-2 " + name)
      return name
    }
    
    function demo4() {
      Logger.log("demo4-1")
      ss = SpreadsheetApp.openById("[==id==]")
      name = ss.getName()
      Logger.log("demo4-2 " + name)
      return name
    }
    

  5. Change the "[==URL==]" and "[==id==]" to refer to your own document

  6. Save the script

  7. From the Run menu choose demo1. This will trigger a question about privileges. Grant it the privileges it asks for. ( asks for access to your Google Drive )

  8. Go back to the spreadsheet itself

  9. Set the following cells to have these formulae:

    A1 : =demo1()
    C2 : =demo2()
    F1 : =demo3()
    h1 : =demo4()
    

Note that the first 2 work, so the spreadsheet is seeing the script project and able to run code from it ( so it has permission to run code from that script ) but the last two do not work and complain that it does not have permission to perform that action.

So how do I fix this? Am I doing something wrong or is there an underlying problem ?

解决方案

This error is due to a change Google recently rolled out prohibiting the use of SpreadsheetApp.openByUrl() (or SpreadsheetApp.openById() in custom functions.  You can still use this method from other contexts like a menu item, trigger, etc. Google had to roll out this change for security reasons and they won't be able to revert back to the old behavior. So the only possible fix may very well be to rewrite the function as a 'normal' GAS-function and NOT a custom function.

The issue is listed in the issue tracker.

这篇关于SpreadsheetApp.openByUrl和openById会给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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