我是Google表格编程的新手。我发现了用于创建自定义菜单的代码,并使用该代码隐藏了工作表中的特定列(菜单选择允许用户选择Google工作表中的数据的特定视图,而无需编辑原始数据)。列被隐藏后,我想基于特定列(票证类型)中的值对数据使用过滤器。我不知道如何隐藏列后如何获取运行过滤器的代码。我尝试了许多变体(包括对第二个函数的引用),并在一个函数中查找了函数,但没有成功找到解决方案。我在想可以过滤类似于隐藏列的过滤器,但是它不起作用。有关如何使其工作的建议?
function showReservationView() {
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
showAll();
sheet.hideColumn(sheet.getRange("B1:F1"));
sheet.hideColumn(sheet.getRange("H1"));
sheet.hideColumn(sheet.getRange("K1:U1"));
sheet.hideColumn(sheet.getRange("W1:Z1"));
sheet.hideColumn(sheet.getRange("AB1:AK1"));
sheet.hideColumn(sheet.getRange("AM1"));
sheet.hideColumn(sheet.getRange("AO1:AP1"));
sheet.hideColumn(sheet.getRange("AR1:BC1"));
sheet.hideColumn(sheet.getRange("BE1:BR1"));
}
function setFilter() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var filterSettings = {};
filterSettings.range = {
sheetId: ss.getActiveSheet().getSheetId()
};
// Criteria for showing/hiding rows in a filter
// https://developers.google.com/sheets/api/reference/rest/v4/FilterCriteria
filterSettings.criteria = {};
var columnIndex = 7;
filterSettings['criteria'][columnIndex] = {
'hiddenValues': [
"EARLY BIRD - Top Tier Full Week Residential Pass",
"Top Tier Full Week Residential Pass",
"GROUP DISCOUNT - Top Tier Full Week Residential Pass",
"EARLY BIRD - Middle Tier Residential Pass",
"Middle Tier Residential Pass",
"GROUP DISCOUNT - Middle Tier Residential Pass",
"EARLY BIRD - Courtyard Room Pass",
"Courtyard Room Pass",
"GROUP DISCOUNT - Courtyard Room Pass",
"Non-Residential Week Pass",
"GROUP DISCOUNT - Non-Residential Week Pass",
"Indian Citizen Week Pass",
"Day Pass",
"Donation to the Swami Shukdevanand Trust",
"Teacher/Presenter Ticket",
"Sevak Ticket",
"Infant Ticket",
"Media Day Pass",
"Media Non-Residential Week Pass",
"Media Residential Week Pass",
"Special Guest Residential Pass"
]
};
var request = {
"setBasicFilter": {
"filter": filterSettings
}
};
Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
}
}
答案 0 :(得分:0)
您的代码仅声明变量setFilter
是对function object的引用-而不是calls / invokes the function。在尝试调用函数对象之前,仅此而已-对象引用。即使它是对功能对象的引用。
将setFilter
声明为函数内的函数对象(例如function foo()
)意味着函数foo
之外的其他函数都不能访问该特定函数对象。其他一些函数(或全局函数)可能会说setFilter
是值为1的变量:
var setFilter = 1; // globally scope `setFilter` to be value 1.
function foo() {
Logger.log(typeof setFilter); // number
try { setFilter() }
catch (err) { Logger.log(err) } // TypeError: setFilter is not a function, it is number.
}
function foo2() {
// Define what `setFilter` means within the context (namespace scope) of `foo2`
// This declaration will shadow the global declaration within this function context.
function setFilter() { /** code */ };
Logger.log(typeof setFilter); // function
setFilter(); // no TypeError from calling a non-function
}
要总结链接的功能指南,您需要执行以下操作:
function showReservationView() {
function setFilter(/** add parameters for the function here */) {
// code here
}
// Treat the nearest namespace scope declaration of `showAll` as
// a function object by calling it.
showAll();
// Hide all the column ranges we want to hide.
const sheet = SpreadsheetApp.getActive().getActiveSheet(),
rangesToHide = [
"B1:F1", "H1", "K1:U1", "W1:Z1", "AB1:AK1",
"AM1", "AO1:AP1", "AR1:BC1", "BE1:BR1"
];
sheet.getRangeList(rangesToHide).getRanges().forEach(function (rg) {
sheet.hideColumn(rg);
});
// Treat the nearest namespace scope declaration of `setFilter` as
// a function object by calling it.
setFilter();
}
参考文献: