我试图使用回答这个问题的脚本 google spreadsheet script - Enable filter with a range by script
目的是在电子表格上创建基本过滤器。
这是脚本
function applyFilter() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var dataSheet = ss.getActiveSheet();
var lastRow = dataSheet.getLastRow();
var lastColumn = dataSheet.getLastColumn();
var sheetId = dataSheet.getSheetId();
var filterSettings = {
"range": {
"sheetId": sheetId,
"startRowIndex": 0,
"endRowIndex": lastRow,
"startColumnIndex": 0,
"endColumnIndex": lastColumn
}
};
var requests = [{
"setBasicFilter": {
"filter": filterSettings
}
}];
Logger.log(requests );
Logger.log(" ssid: " + ssId);
try{
Sheets.Spreadsheets.batchUpdate({'requests': requests}, ssId);
}
catch(e){Logger.log(e.message)}
}
这是我上次尝试运行它的日志,最后一行是我得到的错误
[17-09-17 10:32:57:700 BST] [{setBasicFilter={filter={range={endColumnIndex=26, endRowIndex=3756, sheetId=519417536, startColumnIndex=0.0, startRowIndex=0.0}}}}]
[17-09-17 10:32:57:701 BST] ssid: 1-05tNvAddY6gwzK9GYC4aMG347XARy_yUXZRcXxgHIk
[17-09-17 10:32:57:702 BST] Cannot call method "batchUpdate" of undefined.
ssid是正确的,sheetId是正确的。我已在资源中启用Google表格API - 我已在Google API控制台中启用它
如果有人能告诉我自己做错了什么,我将不胜感激
答案 0 :(得分:0)
根据您的问题,您发现您已在API控制台上启用了Sheets API。但错误显示Cannot call method "batchUpdate" of undefined.
。所以我认为Sheets API可能尚未在高级Google服务中启用。请按以下方式确认。
如果这对你没用,我很抱歉。
在此示例中,UrlFetchApp.fetch()
使用Sheets API v4代替高级Google服务。
function applyFilter() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var dataSheet = ss.getActiveSheet();
var lastRow = dataSheet.getLastRow();
var lastColumn = dataSheet.getLastColumn();
var sheetId = dataSheet.getSheetId();
var filterSettings = {
"range": {
"sheetId": sheetId,
"startRowIndex": 0,
"endRowIndex": lastRow,
"startColumnIndex": 0,
"endColumnIndex": lastColumn
}
};
var requests = [{
"setBasicFilter": {
"filter": filterSettings
}
}];
Logger.log(requests );
Logger.log(" ssid: " + ssId);
var url = "https://sheets.googleapis.com/v4/spreadsheets/" + ssId + ":batchUpdate"
var params = {
method:"post",
contentType: "application/json",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
payload: JSON.stringify({"requests": requests}),
muteHttpExceptions: true,
};
var res = UrlFetchApp.fetch(url, params).getContentText();
Logger.log(res)
}