您无权调用 openById [英] You do not have permission to call openById

本文介绍了您无权调用 openById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:当我运行脚本时,谷歌告诉我,

Problem: When I run the script, Google tells me,

您没有权限调用 openById

You do not have permission to call openById

我从另一个 Google 电子表格复制了一个脚本,更改了 target_ssKey 变量的单元格引用,并在源和目标电子表格中创建了大小合适的命名范围.

I had copied a script from another one of my Google spreadsheets and changed the target_ssKey variable's cell reference and created properly-sized Named Ranges in both the Source and Target spreadsheets.

Google Apps Script 文档没有说明它可能不起作用的原因:

Google Apps Script documentation says nothing about reasons why it might not be working:

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#openById%28String%29

另一个 Google Apps 脚本文档说它应该对我有用,因为我从自定义菜单中调用它:

Another Google Apps Script documentation says that it should work for me because I invoke it from a custom menu:

https://developers.google.com/apps-script/指南/表格/功能#using_apps_script_services

上面的第二个链接说:

如果您的自定义函数抛出错误消息 您没有调用X服务的权限.,该服务需要用户授权因此不能在自定义函数中使用.

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.

要使用上面列出的服务以外的服务,请创建自定义菜单运行 Apps 脚本函数而不是编写自定义功能.从菜单触发的功能将询问用户必要时进行授权,因此可以使用所有应用程序脚本服务.

To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.

我尝试将函数放入自定义函数"项目,然后放入附加"项目,但仍然收到相同的错误消息.

I tried putting the function into a "Custom Functions" project and then into an "Add-on" project, but still got the same error message.

关于我做错了什么以及如何使它起作用的任何想法?

Any ideas on what I am doing wrong and how to make this work?

这是我的确切代码:

function exportData_SStoSS() {
    //  Get the source data.
    var source_ss = SpreadsheetApp.getActiveSpreadsheet();
    var data = source_ss.getRangeByName("exportData").getValues();

    //  Identify the target.
    var controls_sh = source_ss.getSheetByName("Controls");
    var target_ssKey = controls_sh.getRange('C2').getValue();
    var target_ss = SpreadsheetApp.openById(target_ssKey);

    //  Paste the data to the target.
    target_ss.getRangeByName("importData").setValues(data);
};

推荐答案

我想我会抛出一个类似的问题,这让我想到了这个问题,在那里我收到了错误 You don't have允许通过 openById 调用.

I thought that I would throw in a similar issue that I had which brought me to this question, where I received the error You don't have permission to call by openById.

在我的例子中,我试图从我从这个例子中复制的 translate.gs 调用函数:

In my case I was trying to call functions from translate.gs which I copied from this example:

https://developers.google.com/apps-script/quickstart/docs

注意在translate.gs

/**
 * @OnlyCurrentDoc
 *
 * The above comment directs Apps Script to limit the scope of file
 * access for this add-on. It specifies that this add-on will only
 * attempt to read or modify the files in which the add-on is used,
 * and not all of the user's files. The authorization request message
 * presented to users will reflect this limited scope.
 */

这里的罪魁祸首是 @OnlyCurrentDoc 注释.请参阅此处以供参考:

The culprit here is the @OnlyCurrentDoc comment. See here for reference:

https://developers.google.com/apps-script/guides/服务/授权

删除 @OnlyCurrentDoc 为我解决了这个问题

Removing @OnlyCurrentDoc fixed this issue for me

这篇关于您无权调用 openById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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