[英]How to access data from an external spreadsheet with Google Apps Script
I have a spreadsheet with some data tables that I want to use in other spreadsheets script.我有一个电子表格,其中包含一些我想在其他电子表格脚本中使用的数据表。 How can I call this spreadsheet by its ID and access the data?\
如何通过 ID 调用此电子表格并访问数据?\
From the documentation , I know that custom functions are not allowed to access other spreadsheets, a solution would be to put this function in a menu and run it, since it'd ask for the user's authorization.从文档中,我知道不允许自定义函数访问其他电子表格,解决方案是将这个 function 放在菜单中并运行它,因为它会要求用户授权。 But this is not an option because I am using the data with the built-in method
onEdit()
.\但这不是一个选项,因为我正在使用带有内置方法
onEdit()
的数据。\
Also, I tried accessing the spreadsheet via onOpen()
since it's not a custom function but still no success.此外,我尝试通过
onOpen()
访问电子表格,因为它不是自定义 function 但仍然没有成功。 Any other solution?还有其他解决方案吗?
My code:我的代码:
function onOpen(){
ss = SpreadsheetApp.getActiveSpreadsheet();
src = SpreadsheetApp.openById("spreadsheetID");
Error message:错误信息:
Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets
And appscript.json
和
appscript.json
{
"timeZone": "Europe/Paris",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets"
]
}
Lastly, I also tried creating an installable trigger with this code:最后,我还尝试使用以下代码创建可安装触发器:
function createSpreadsheetOpenTrigger() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ScriptApp.newTrigger('myFunction')
.forSpreadsheet(ss)
.onOpen()
.create();
}
But the trigger does not seem to be called upon opening the sheet.但是在打开工作表时似乎没有调用触发器。
SpreadsheetApp.openById("spreadsheetID");
is one of those requests是这些请求之一
You are on the right track with the installable triggers - they can trigger the execution of requests that require authorization您使用可安装的触发器走在正确的轨道上 - 它们可以触发需要授权的请求的执行
I think your problem is the correct implementation of installable triggers我认为您的问题是可安装触发器的正确实现
function createSpreadsheetOpenTrigger
creates an installable onOpen
trigger that calls the function myFunction
when the sheet is open function createSpreadsheetOpenTrigger
创建一个可安装的onOpen
触发器,在工作表打开时调用 function myFunction
myFunction
firstmyFunction
function createSpreadsheetOpenTrigger()
once manually - to install the triggerfunction createSpreadsheetOpenTrigger()
- 安装触发器Sample complete code:示例完整代码:
function createSpreadsheetOpenTrigger() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ScriptApp.newTrigger('myFunction')
.forSpreadsheet(ss)
.onOpen()
.create();
}
function myFunction(){
ss = SpreadsheetApp.getActiveSpreadsheet();
src = SpreadsheetApp.openById("spreadsheetID");
Logger.log(src.getActiveSheet().getName());
}
Note:笔记:
Instead of implementing the function createSpreadsheetOpenTrigger()
, you can install the trigger manually by going on Edit->My project's triggers
and bind a trigger of the desired type to the desired function, see also Managing triggers manually而不是实现 function
createSpreadsheetOpenTrigger()
,您可以通过继续Edit->My project's triggers
手动安装触发器并将所需类型的触发器绑定到所需的 function,另请参阅手动管理触发器
First, in the line src = SpreadsheetApp.openById("spreadsheetID");首先,在 src = SpreadsheetApp.openById("spreadsheetID"); did you replace the id with the actual spreadsheet ID?
您是否将 id 替换为实际的电子表格 ID? That's the number at the end of the url.
那是 url 末尾的数字。 I assume you did, but it doesn't show that you did in your example.
我假设你做到了,但它并没有表明你在你的例子中做到了。
Second, just for testing, try to do this action in a regular function (not a trigger) to get things to work.其次,只是为了测试,尝试在常规 function(不是触发器)中执行此操作以使事情正常工作。 Have you done that?
你这样做了吗? Then you can focus on the spreadsheet access without worrying if the trigger is causing a problem.
然后,您可以专注于电子表格访问,而不必担心触发器是否会导致问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.