简体   繁体   English

Sheets 函数在独立的 Google Script 中工作,但在脚本绑定到 Google Sheet 时不起作用

[英]Sheets function working in standalone Google Script but NOT when script is bound to a Google Sheet

I have this piece of code that is working perfectly when I use it in a standalone Google Script running as a web-app :我有这段代码,当我在作为网络应用程序运行的独立 Google 脚本中使用它时,它运行良好:

function getSubjectsRollNosFromGradeSheet() {
  var spreadsheetId = '171hWWH8bV6kM1JJ2dvL80q3BoWC6eEc0U7l7wCTaXHE';
  var rangeSubject = 'GradesT1!G1:AA1';

  var values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeSubject).values;

However when I write this same code in a Script that is embedded inside another Google sheet (not the one being accessed for data) attached to a Google Form I get an error :但是,当我在附加到 Google 表单的另一个 Google 工作表(不是正在访问数据的工作表)中嵌入的脚本中编写相同的代码时,出现错误:

function updateForm(){

  // call your form and connect to the drop-down item
  var form = FormApp.openById("1VV_LeFJODZcIzfcYOh8vHEkhl033Lx4ZsXaY4Pm1ZUE");
  var subList1 = form.getItemById("497738674").asListItem();

  //pull data from another sheet
  var spreadSheetID = "1EJSx62rZuLSKIgel7LH8hE7-68lupz4buutmVQBqi2I";
  var rangeSubs = 'Sheet1!A1:A';

  var subValues = Sheets.Spreadsheets.Values.get(spreadSheetID, rangeSubs).values;

I get this error我收到这个错误

ReferenceError: "Sheets" is not defined.参考错误:未定义“表格”。 (line 16, file "Code") (第 16 行,文件“代码”)

Why is the Sheets object available in the first case but not in the second case and how should I change my code to overcome this error.为什么 Sheets 对象在第一种情况下可用,但在第二种情况下不可用,我应该如何更改代码以克服此错误。

Sheets is one of methods of Advanced Google Services. Sheets是高级 Google 服务的一种方法。 So in order to use Sheets of Advanced Google Services, please enable Sheets API for Advanced Google Services and API console as follows.因此,为了使用Sheets of Advanced Google Services,请按如下方式启用 Sheets API for Advanced Google Services 和 API 控制台。

Please do this at the bound script which included the script of your question.请在包含问题脚本的绑定脚本中执行此操作。

Enable Sheets API v4 at Advanced Google Services在高级 Google 服务中启用 Sheets API v4

  • On script editor在脚本编辑器上
    • Resources -> Advanced Google Services资源 -> 高级 Google 服务
    • Turn on Google Sheets API v4启用 Google Sheets API v4
    • Enable Sheets API v4 at API console在 API 控制台启用 Sheets API v4

Enable Sheets API v4 at API console在 API 控制台启用 Sheets API v4

  • On script editor在脚本编辑器上
    • Resources -> Cloud Platform project资源 -> 云平台项目
    • View API console查看 API 控制台
    • At Getting started, click Enable APIs and get credentials like keys.在入门中,单击启用 API 并获取密钥等凭据。
    • At left side, click Library.在左侧,单击库。
    • At Search for APIs & services, input "sheets".在搜索 API 和服务中,输入“sheets”。 And click Google Sheets API.然后单击 Google Sheets API。
    • Click Enable button.单击启用按钮。

References :参考资料:

If I misunderstand your question, I'm sorry.如果我误解了你的问题,我很抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM