我正在尝试基于filter
的存在从__
返回密钥val对
知道了:
function trigger() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ranges = ss.getNamedRanges();
const rangeNames = ranges.map(range => range.getName());
let triggers = {};
triggers = rangeNames.filter(name => {
if (name.indexOf("__")>-1) {const val = ss.getRangeByName(name).getValue();
triggers.name = val
return triggers;
}
});
console.log(triggers);
}
但是它似乎只返回name
。任何帮助将不胜感激
答案 0 :(得分:3)
数组的filter
函数接受应返回false的函数。
也许对您有用的是使用reduce方法。
let triggers = {};
triggers = rangeNames.reduce( (acc, curr)=>{
if (curr.indexOf("__")>-1){
acc[curr] = ss.getRangeByName(curr).getValue();
}
return triggers;
}, triggers );
或者如果您不介意在该函数内的其他位置使用该ranges
您可以直接reduce
“ namedRanges”
function trigger() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const triggers = ss.getNamedRanges().reduce( (acc,curr)=>{
if (curr.getName().indexOf("__")>-1) {
acc[curr.getName()]=curr.getValue();
}
return acc;
}, {});
console.log(triggers);
}