arrays - Google Sheet Query - 动态构建参考数组
问题描述
我在一个文件中有多个选项卡,并希望将相同的范围合并到一个主选项卡。
我做了一个查询
=QUERY({source1!AR5:AU;source1!AR5:AU}, "select Col1,Col2,Col3,Col4 where Col1 is not null order by Col1", 0)
但最后我会有越来越多的标签(大约 30 个),我不想每次都手动更改我的查询。我能怎么做?
我在某处看到我可以使用宏来创建一个函数,你知道代码吗?我只是想要一些简单的东西,我在另一个标签中添加了我所有的标签名称
解决方案
尝试:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var allSheets = [];
// Append more names if you want to exclude more
// Since 'query' is master sheet, excluded it as well
// Remove 'query' below if you want to include it in the formula
var excludedTabs = ['other', 'random', 'query'];
sheets.forEach(function (sheet){
var sheetName = sheet.getSheetName();
if (!excludedTabs.includes(sheetName)){
allSheets.push(sheetName);
}
});
// Set formula in A1 cell in "query" sheet
// Modify A1 to change the cell
var cell = ss.getSheetByName('query').getRange("A1");
cell.setFormula("=QUERY({" + allSheets.join('!AR5:AU;') + "!AR5:AU}, \"select Col1,Col2,Col3,Col4 where Col1 is not null order by Col1\", 0)");
}
这会将公式设置为A1
工作表query
,随时通过更改A1
和更改设置公式的位置query
。
您还可以添加要排除的工作表。将其附加到excludedTabs
阵列上。
样本输出:
添加了样品表以检查新的表盒。
添加了预期的公式。(不包括query
,random
和other
床单)
推荐阅读
- c++ - 在 Linux/x86-84 上传递大对象的 C++ 调用约定
- ios - 颤振运行->错误:没有名称为“keyboardDismissBehavior”的命名参数
- python - Python - Beautiful Soup: Webscraping PubMed - 提取 PMID(文章 ID)、添加到列表和防止重复抓取
- reactjs - 测试去抖函数 React - React-testing-library
- python - RQ-作业无缘无故失败
- php - MySQLi 在表格中显示结果
- laravel - 如何在 Laravel 中使用 Laravel Excel 设置导出 csv 的编码
- html - 如何使所有div的高度相等?
- html - 为什么推荐主图使用 16:9 的比例?
- javascript - 如何使用胖箭头 es6 语法重写此嵌套调用以设置?