【问题标题】:ReferenceError: "Sheets" is not definedReferenceError:未定义“表格”
【发布时间】:2017-08-11 02:20:49
【问题描述】:

这是我第一次尝试使用脚本编辑器。我被分配做一个脚本来为谷歌工作表创建数据透视表。

  //creating pivot table through script editor for google sheet
  function addPivotTable() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = "Sheet1";

  // Create a new sheet which will contain our Pivot Table
  var pivotTableSheet = ss.insertSheet();
  var pivotTableSheetId = pivotTableSheet.getSheetId();

 // Add Pivot Table to new sheet
 // Meaning we send an 'updateCells' request to the Sheets API
 // Specifying via 'start' the sheet where we want to place our Pivot Table
 // And in 'rows' the parameters of our Pivot Table 

  var requests = [{
    // Meaning we send an 'updateCells' request to the Sheets API
    "updateCells": {
        // And in 'rows' the parameters of our Pivot Table 
       "rows": {
         "values": [
      {
         // Add Pivot Table to new sheet
        "pivotTable": {
          "source": {
            "sheetId": ss.getSheetByName(sheetName).getSheetId(),
            "startRowIndex": 0,
            "startColumnIndex": 0,
          },
          //create rows from the selected columns
          "rows": [
            {
              "sourceColumnOffset": 14,
              "showTotals": true,
              "sortOrder": "ASCENDING",   
            },
          ],
          //show values from the selected columns
          "values": [
            {
              "summarizeFunction": "COUNTA",
              "sourceColumnOffset": 10
            }
          ],
          //display in horizontal layout
          "valueLayout": "HORIZONTAL"
        }
      }
    ]
  },
  // Specifying via 'start' the sheet where we want to place our Pivot Table
  "start": {
    "sheetId": pivotTableSheetId,
  },
  "fields": "pivotTable"
}
 }];

  Sheets.Spreadsheets.batchUpdate({'requests': [requests]}, ss.getId());
}

请检查我的代码并解释我哪里出错了,因为每次我运行脚本编辑器时都会出错,提示表单未定义弹出窗口。 "ReferenceError: "Sheets" 未定义。(第 46 行,文件 "Code")Dismiss"

【问题讨论】:

  • 剂量表从何而来?它是全局 Obj 然后检查它是否存在并加载。你可以安慰它来确认它

标签: javascript json google-sheets


【解决方案1】:

这是一项来自谷歌的高级服务。您需要在使用之前启用此服务。 https://developers.google.com/apps-script/guides/services/advanced 在脚本编辑器中,选择资源 > 高级 Google 服务...。 在出现的对话框中,单击要使用的服务旁边的开/关开关。 在对话框底部,单击 Google API 控制台的链接。 在控制台中,单击过滤器框并键入 API 的部分名称(例如,“日历”),然后在看到名称后单击该名称。 在下一个屏幕上,单击启用 API。 关闭 API 控制台并返回脚本编辑器。在对话框中单击确定。您启用的高级服务现在可以自动完成。

【讨论】:

  • 我们需要启用什么服务?
  • 您需要开启Google Sheets API服务
【解决方案2】:

只需从此处添加所需的服务,在您的情况下,您需要 Google Sheets API。祝你好运。

Just add the service from here, Good Luck

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 2014-09-26
    • 2012-08-24
    • 1970-01-01
    • 2015-06-29
    • 2016-08-31
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多