简体   繁体   English

如何使用 Google Apps 脚本访问外部电子表格中的数据

[英]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.jsonappscript.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.但是在打开工作表时似乎没有调用触发器。

As you already realized, custom functions and simple trigger cannot perform requests that require authorization正如您已经意识到的那样,自定义函数和简单触发器无法执行需要授权的请求

  • 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

  • This means that you need to create the function myFunction first这意味着您需要先创建 function myFunction
  • Also, you need to run function createSpreadsheetOpenTrigger() once manually - to install the trigger此外,您需要手动运行一次function 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.

相关问题 Google Apps脚本:如何使用电子表格中的数据查询外部数据库? - Google Apps Script: how to use data in a spreadsheet to query a external database? 如何使用Google Apps脚本自动下载Google电子表格数据 - how to automatically download Google spreadsheet data using Google Apps script 谷歌应用程序脚本html选择从电子表格数据加载的选项 - google apps script html select options loaded from spreadsheet data 使用Google Apps脚本作为服务从电子表格中检索数据 - Using Google Apps Script as service to retrieve data from spreadsheet 如何使用谷歌应用程序脚本将数据从电子表格加载到 html? - How can i load data from a spreadsheet to an html using google apps script? Google Apps脚本:如何从一个电子表格中提取今天输入的数据并将其转换为另一电子表格? - Google Apps Script: How do I pull data entered today from one spreadsheet and transpose it to another? 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? Google Apps脚本未从电子表格中检索信息 - Google Apps Script not retrieving information from spreadsheet 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script 在 Google Apps 脚本中从另一个电子表格编辑一个电子表格 - Editing one Spreadsheet from another Spreadsheet in Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM