google-apps-script - Google 表格 - 从多个工作表选项卡中提取数据的双向查找
问题描述
我一直在处理我想要的工作表并输入选项卡和输出选项卡。输入选项卡基本上是一个表格,输出选项卡将是一个日历(排序)。
在我的例子中:
Sheet1 = 输入选项卡
Sheet2 = 输出选项卡
我希望有一个 vlookup,它将从 Sheet2 (输出)中提取其 search_key 并在 Sheet1 中搜索它。
我一直在搞乱以下(例如绿色单元格 Sheet2):
=IFERROR(VLOOKUP(A2,Sheet1!$A$2:$C$7,MATCH(B1,Sheet1!$A$2:$C$7,0),False))
我也尝试过使用 hlookup 而不是 MATCH 的变体,但运气不佳。
我遇到的问题是我不再知道在哪里放置列索引。在我的示例表中,我使用单向 vlookup(蓝色单元格 Sheet2)使用此索引从 Sheet1(长度列)返回所需的值。是不是不可能在双向查找中做到这一点?
解决方案
尝试在 Apps 脚本中运行此函数:
function myFunction() {
var ss = SpreadsheetApp.getActive();
var sheet1 = ss.getSheetByName('Sheet1');
var sheet2 = ss.getSheetByName('Sheet2');
var firstRow = 2;
var numRows = sheet1.getLastRow() - 1;
var firstCol = 1;
var numCols = sheet1.getLastColumn();
var inputData = sheet1.getRange(firstRow, firstCol, numRows, numCols).getValues();
var numBrands = sheet2.getLastRow();
var outputRange = sheet2.getDataRange();
var outputData = outputRange.getValues();
// Iterating through each row in Sheet1 data:
for(var i = 0; i < numRows; i++) {
// Iterating through each row in Sheet2:
for(var j = 1; j < outputData.length; j++) {
// Iterates through each cell for each row in Sheet2.
for(var k = 1; k < outputData[0].length; k++) {
var inputBrand = inputData[i][0];
var outputBrand = outputData[j][0];
var inputDate = inputData[i][1];
var outputDate = outputData[0][k];
// It checks whether the date and brand corresponding to each cell
// (same row or column) matches the date and brand in the current
// row in Sheet1
if(inputBrand == outputBrand && inputDate.toString() == outputDate.toString()) {
var inputLength = inputData[i][2];
sheet2.getRange(j+1, k+1, 1, 1).setValue(inputLength);
}
}
}
}
}
推荐阅读
- karate - 空手道断言响应匹配断言失败
- yaml - curl 命令导致 bitbucket CI/CD 管道 YAML 中的缩进错误
- python - 如何从 Python 正确访问 JSON 中的属性?
- r - 创建一个 for 循环以使用一系列变量实现 ggplot2。将所有内容视为缺失值
- c# - 浮点戏剧性错误(小数部分完全丢失)
- c# - 找不到元数据文件 .dll 且值不能为空(参数名称:键)在 Xamarin 跨平台应用程序上工作时弹出错误
- javascript - 在 node.js 中返回一个 json
- javascript - 将数字添加到列表项 React-native
- javascript - Java Servlet 在加载 Angular 静态文件时挂起
- javascript - 如何从 JSON 文件填充 javascript 对象