所需权限:https://www.googleapis.com/auth/spreadsheets [英] Required permissions: https://www.googleapis.com/auth/spreadsheets

查看:85
本文介绍了所需权限:https://www.googleapis.com/auth/spreadsheets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在应用程序脚本上创建了一个简单的代码,以使URL中的Google云端硬盘文件名不存在

 功能FileName(URL){var ss = SpreadsheetApp.openByUrl(URL);返回ss.getName();} 

当我跑步时出现错误:

例外:您无权呼叫SpreadsheetApp.openByUrl.所需权限:

解决方法:

您可以通过多种方式使用常规功能:

  1. 实现为自定义菜单按钮,
  2. 通过脚本编辑器执行它们,
  3. 通过

    我建议您在官方文档中查找此脚本中使用的功能,以了解它们各自的功能.

    更新:

    我刚刚打开工作表,发现您正在使用 SpreadsheetApp.openByUrl(URL); ,但是此方法仅适用于电子表格,因此类名为 SpreadsheetApp .如果要打开文件ID,则需要使用 getFileById(id).我在第一列中看到文件Urls,但是没有方法通过URL打开文件.像这样的文件中的ID:

    https://drive.google.com/file/d/1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp/view?usp=sharing

    是:

    1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp

    因此您需要使用

    var文件= DriveApp.getFileById('1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp')

    代码段如下:

      function FileName(){var URL ="1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp"var file = DriveApp.getFileById(URL);var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');sheet.getRange('B1').setValue(file.getName());} 

    请熟悉一下GAS,因为该问题刚刚成为需要更多关注的问题.

    I've created a simple code on appscript to get Google Drive file names out of the URL

    function FileName (URL) {
      var ss = SpreadsheetApp.openByUrl(URL);
      return ss.getName();
    }
    

    When I run that I get an error:

    Exception: You do not have permission to call SpreadsheetApp.openByUrl. Required permissions: https://www.googleapis.com/auth/spreadsheets (linha 6).

    I've already enabled Drive and Sheets API in the Advanced Google Services area, so the "https://www.googleapis.com/auth/spreadsheets" should be ok, but it is not.

    How could I make that work?

    I've created a sample sheet in this link with the problem replication.

    解决方案

    Explanation of the issue:

    You are using a custom function.

    It is very well explained in the official documentation:

    If your custom function throws the error message You do not have permission to call X service., the service requires user authorization and thus cannot be used in a custom function.

    As the message clearly states, you are not allowed to use services that require authorization such as SpreadsheetApp.openByUrl(URL) within a custom function.

    There is also a table that particularly points out that SpreadsheetApp.openByUrl(URL); can not be used within a custom function:

    Workarounds:

    You can use regular functions in multiple ways:

    1. Implemented as custom menu buttons,
    2. Execute them from the script editor,
    3. Execute them via simple triggers,
    4. Create an addon menu etc.

    Regular function version of your script:

    function FileName () {
      var URL = "spreadsheet_url"
      var ss = SpreadsheetApp.openByUrl(URL);
      var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
      sheet.getRange('A1').setValue(ss.getName());
    }
    

    Upon execution (see screenshot), this script will set the value of cell A1 of Sheet1 to the name of the spreadsheet file.

    I would advice you to look for the functions I used in this script in the official documentation to see what each of them does.

    Update:

    I just opened your sheet and saw that you are using SpreadsheetApp.openByUrl(URL); but this method works only for spreadsheets hence the class name SpreadsheetApp. If you want to open a file ID then you need to use getFileById(id). I see in the first column you have file Urls but there is not method to open a file by its URL. The id from a file like that:

    https://drive.google.com/file/d/1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp/view?usp=sharing

    is:

    1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp

    Therefore you need to use

    var files = DriveApp.getFileById('1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp')

    A code snippet will be like that:

    function FileName () {
      var URL = "1d-2L5kGZWbUa_p7iLgBBa59QqZiyIhgp"
      var file = DriveApp.getFileById(URL);
      var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
      sheet.getRange('B1').setValue(file.getName());
    }
    

    Please familiarize yourself with GAS because this question just became a question that needs more focus.

    这篇关于所需权限:https://www.googleapis.com/auth/spreadsheets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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