gpt4 book ai didi

javascript - 引用错误 : "Sheets" is not defined

转载 作者:数据小太阳 更新时间:2023-10-29 06:04:56 26 4
gpt4 key购买 nike

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

  //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:未定义“工作表”。(第 46 行,文件“代码”)关闭”

最佳答案

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

关于javascript - 引用错误 : "Sheets" is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com