首页 > 解决方案 > 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(长度列)返回所需的值。是不是不可能在双向查找中做到这一点?

这是示例的链接: https ://docs.google.com/spreadsheets/d/1_nqH-XOxNhQAUVJzesNBZeMci7AV9RowSQUnptAruPc/edit?usp=sharing

标签: google-apps-scriptgoogle-sheetsvlookupgs-vlookup

解决方案


尝试在 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);
        }      
      }  
    }
  }
}

推荐阅读