Google 表格查找工具帮助

Google Sheets Lookup Tool Assistance

我自己承担了学习如何 "excel" 的责任。这在我看来更有意义了,但是嘿。

我建了一个"lookup tool"可以查看here

"Dishwasher search" 正在按预期工作。您可以按噪音级别或装饰面板高度进行搜索,以创建合适型号的列表,然后按产品 code/model 编号将数据拉入单独搜索中。

我决定让它更难一点,并为 Ovens 创建了一个。

布局是这样的;主搜索>单烤箱数据库>双烤箱数据库>内置烤箱数据库。 我的目标是实现与 "Dishwasher tool" 相同的搜索功能,但我一直不确定如何从不同来源搜索 (Vlookup)。

我尝试使用以下公式创建 "Master DB";

={Importrange("1mY13e-75dBYfKgkjV8dFFFEvxC838nGNxPrUdusc0PA", "'Single Ovens'!$A:$F");Importrange("1mY13e-75dBYfKgkjV8dFFFEvxC838nGNxPrUdusc0PA", "'Double Ovens'!$A:$F");Importrange("1mY13e-75dBYfKgkjV8dFFFEvxC838nGNxPrUdusc0PA", "'Built-Under Ovens'!$A:$F")))}

但是,它似乎只从第一个范围而不是其他范围中提取数据,除非我是水平而不是垂直的,但水平不适用于我的 Vlookup 公式(据我所知)。

我尝试过的另一种方法是使用此代码;

=IF(AND($A="Single Oven",$A>0), Vlookup($A,'Single Ovens'!$B:$F,1,False),IF(AND($A="Double Oven",$A>0), Vlookup($A,'Double Ovens'!$B:$F,1,False),If(AND($A="Built-Under Oven",$A>0), Vlookup($A,'Built-Under Ovens'!$B:$F,1,False),IF($A="Single Oven",Vlookup($A,'Single Ovens'!$A:$F,2,False),IF($A="Double Oven", Vlookup($A,'Double Oven'!$A:$F,2,False),If($A="Built-Under Oven", Vlookup($A,'Built-Under Oven'!$A:$F,2,False)))))))

上面的代码被插入并且是"meant" Vlookup all 3 sheets and pull "product Code" through to populate Cell D4 on sheet 'Ovens' .

现在,我在这方面有点新手,但我正在努力理解这一切:)

你们有什么建议或给我指出正确的方向吗?

编辑 -

抱歉各位。不 post 我的解决方案是不礼貌的。我已将我的 Importrange 函数更改为 =Importrange("XYZ","""'Single Ovens'!$A2:$F200") 并重复这 3 次,每次之间的间隔为 200 "rows"。不是解决方案,而是可行的替代方案。我的朋友目前正在为我编写一个脚本来实现这一点。由于 importrange 公式简化了问题,Vlookup 公式不再需要那么复杂。

于是和McSheehy讨论了这个问题,原来问题是这样的。

如何从我选择的一个 sheet、多个 sheet 中获取数据。 并写给许多传播sheets,这些传播sheets中的多个sheets,由我选择。

一旦该数据位于正确的位置,当前的公式应该有效或可以轻松调整。

我想出了一个脚本解决方案, 用户在源 sheet 中创建设置 sheet。 在 A2 向下,Target spreadsheet 键,B2 向下,Source sheet 您希望从当前 sheet 中包含的名称。 C2往下是目标SHEET个名字,如果要写数据到多个sheet.

对一些代码进行了注释,以帮助解释 McSheehy 关于其工作原理的问题。

如果有人有任何改进建议,我相信有一些,特别是 headers 位。 (不需要,但我的 clearContent/clearConents 线路一直在翻转),我洗耳恭听。

谢谢

function getOvenDataV5(){


    var settingsSheetName = "monkey_settings";

    /* DO NOT EDIT BELOW THIS LINE */  


    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var settings = ss.getSheetByName(settingsSheetName);


      // this bit has been edited, note the getValues, not getValue, as we want the whole column now not just a single cell.
    var targetSheetsValues = settings.getRange("C2:C").getValues(); // this gets the target sheet names from the settings sheet
    var targetSheets = []; // And array added to throw target sheet names into, as there is more than one.



      // the reason we use arrays and loops (later on), is because the script has no idea how much data to expect.
      // so we go through whatever it's grabbed, the stuff it thinks is data, but we check it later on.
      // only a simple check. Our check is that it cannot be blank. ""
      // then stuff it in an array, a handy thing to store data, for use later on.



      var sSheets = settings.getRange("B2:B").getValues();
      var sourceSheets = []; 


    // new loop below to get the target sheets names. We'll use this in the write bit later.

        for(var i = 0; i < targetSheetsValues.length;i++){
        if(targetSheetsValues[i][0]!=""){
          targetSheets.push(targetSheetsValues[i]);
        }

      }






      for(var i = 0; i < sSheets.length;i++){
        if(sSheets[i][0]!=""){
          sourceSheets.push(sSheets[i]);
        }

      }


      var dKeys = settings.getRange("A2:A").getValues(); 
      var sKeys = []; 



      for(var i = 0; i < dKeys.length;i++){
        if(dKeys[i][0]!=""){
          sKeys.push(dKeys[i]);
        }

      }

      var data = []; 

      for (var i = 0; i < sourceSheets.length;i++){




        var values = ss.getSheetByName(sourceSheets[i]).getDataRange().getValues(); 



        for (var x = 1;x < values.length; x++){



          if(values[x][0]!= ""){
            data.push(values[x]);
          }


      }




    }






      // Below is an array of your column headers, the script was being annoying when clearing sheet data, so decided to clear the whole damn sheet
      // then write the headers via here instead
      var headers = [["Model No", "Product Code", "Brand", "Model No", "kW", "Amp"]];



    for (var i = 0; i< sKeys.length;i++){
    var tss = SpreadsheetApp.openById(sKeys[i]);
      for(var x = 0; x < targetSheets.length;x++){ // this loop, within the keys loop, goes through the target sheets array
    var target =  tss.getSheetByName(targetSheets[x]); // this loads the target sheet, one by one
    var range = target.getRange(2,1, data.length, data[0].length); // this gets the cells to write to
    target.clearContents(); // clear the sheet before writing data
        target.getRange("A1:F1").setValues(headers); // write the headers to a1:F1 in target sheet

    range.setValues(data); //write the data
      }
    }
    }

我不知道它们是否有任何价值,但这是我在脚本中写的两件事。

下面是一个 vlookup 脚本,用于设置或 return 到带有查询能力提示的内存。

//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//--//Dependent on isEmpty_()
// Script Look-up
/*
Benefit of this script is:
-That google sheets will not continually do lookups on data that is not changing with using this function as it is set with hard values until script is kicked off again.
-Unlike Vlookup you can have it look at for reference data at any Column in the row.  Does not have to be in the first column for it to work like Vlookup.
-You can return the Lookup to Memory for further processing by other functions

Useage:

Lookup_(Sheetinfo,"Sheet1!A:B",0,[1],"Sheet1!I1","n","y");
//or
Lookup_(Sheetinfo,"Sheet1!A:B",0,[1],"return","n","n");
//or
Lookup_(Sheetinfo,"Sheet1!A:B",0,[0,1],"return","n","n");
//or
Lookup_(Sheetinfo,"Sheet1!A:B",1,[0],"return","y","n");
//or
Lookup_("cat","Sheet1!A:G",4,[0],"Database!A1","y","y");

-Loads all Locations numbers from J2:J into a variable 
--looks for Location Numbers in Column 0 of Referance sheet and range eg "Data!A:G"
---Returns results to Column 3 of Target Sheet and range eg "test!A1" or "1,1"

*/



function Lookup_(Search_Key,RefSheetRange,SearchKey_Ref_IndexOffSet,IndexOffSetForReturn,SetSheetRange,ReturnMultiResults,Add_Note)   
{
  var RefSheetRange = RefSheetRange.split("!");
  var Ref_Sheet = RefSheetRange[0];
  var Ref_Range = RefSheetRange[1];

  if(!/return/i.test(SetSheetRange))
  {
  var SetSheetRange = SetSheetRange.split("!");
  var Set_Sheet = SetSheetRange[0];
  var Set_Range = SetSheetRange[1];
  var RowVal = SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(Set_Range).getRow();
  var ColVal = SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(Set_Range).getColumn();
  }

  var twoDimensionalArray = [];
  var data = SpreadsheetApp.getActive().getSheetByName(Ref_Sheet).getRange(Ref_Range).getValues();         //Syncs sheet by name and range into var
  for (var i = 0, Il=Search_Key.length; i<Il; i++)                                                         // i = number of rows to index and search  
  {
    var Sending = [];                                                                                      //Making a Blank Array
    var newArray = [];                                                                                     //Making a Blank Array
    var Found ="";
    for (var nn=0, NNL=data.length; nn<NNL; nn++)                                                                 //nn = will be the number of row that the data is found at
    {
      if(Found==1 && ReturnMultiResults.toUpperCase() == 'N')                                                                                         //if statement for found if found = 1 it will to stop all other logic in nn loop from running
      {
        break;                                                                                             //Breaking nn loop once found
      }
      if (data[nn][SearchKey_Ref_IndexOffSet]==Search_Key[i])                                              //if statement is triggered when the search_key is found.
      {
        var newArray = [];
        for (var cc=0, CCL=IndexOffSetForReturn.length; cc<CCL; cc++)                                         //cc = numbers of columns to referance
        {
          var iosr = IndexOffSetForReturn[cc];                                                             //Loading the value of current cc
          var Sending = data[nn][iosr];                                                                    //Loading data of Level nn offset by value of cc
          if(isEmpty_(Sending))                                                                      //if statement for if one of the returned Column level cells are blank
          {
          var Sending =  "#N/A";                                                                           //Sets #N/A on all column levels that are blank
          }
          if (CCL>1)                                                                                       //if statement for multi-Column returns
          {
            newArray.push(Sending);
            if(CCL-1 == cc)                                                                                //if statement for pulling all columns into larger array
            {
              twoDimensionalArray.push(newArray);
              var Found = 1;                                                                              //Modifying found to 1 if found to stop all other logic in nn loop
              break;                                                                                      //Breaking cc loop once found
            }
          }
          else if (CCL<=1)                                                                                 //if statement for single-Column returns
          {
            twoDimensionalArray.push(Sending);
            var Found = 1;                                                                                 //Modifying found to 1 if found to stop all other logic in nn loop
            break;                                                                                         //Breaking cc loop once found
          }
        }
      }
      if(NNL-1==nn && isEmpty_(Sending))                                                             //following if statement is for if the current item in lookup array is not found.  Nessessary for data structure.
      {
        for(var na=0,NAL=IndexOffSetForReturn.length;na<NAL;na++)                                          //looping for the number of columns to place "#N/A" in to preserve data structure
        {
          if (NAL<=1)                                                                                      //checks to see if it's a single column return
          {
            var Sending = "#N/A";
            twoDimensionalArray.push(Sending);
          }
          else if (NAL>1)                                                                                  //checks to see if it's a Multi column return
          {
            var Sending = "#N/A";
            newArray.push(Sending);
          }
        }
        if (NAL>1)                                                                                         //checks to see if it's a Multi column return
        {
          twoDimensionalArray.push(newArray);  
        }
      }
    }
  }
  if (CCL<=1)                                                                                            //checks to see if it's a single column return for running setValue
  {
    var singleArrayForm = [];
    for (var l = 0,lL=twoDimensionalArray.length; l<lL; l++)                                                          //Builds 2d Looping-Array to allow choosing of columns at a future point
    {
      singleArrayForm.push([twoDimensionalArray[l]]);
    }
    if(!/return/i.test(SetSheetRange))
    {
      SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,singleArrayForm.length,singleArrayForm[0].length).setValues(singleArrayForm);
      SpreadsheetApp.flush();
      if(/y/i.test(Add_Note))
      {
        SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,1,1).setNote("VLookup Script Ran On: " + Utilities.formatDate(new Date(), "PST", "MM-dd-yyyy hh:mm a") + "\nRange: " + Ref_Sheet + "!" + Ref_Range);      
      }
    }
    else
    {
      return singleArrayForm
    }
  }
  if (CCL>1)                                                                                             //checks to see if it's a multi column return for running setValues
  {
    if(!/return/i.test(SetSheetRange))
    {
      SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,twoDimensionalArray.length,twoDimensionalArray[0].length).setValues(twoDimensionalArray);
      SpreadsheetApp.flush();
      if(/y/i.test(Add_Note))
      {
        SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,1,1).setNote("VLookup Script Ran On: " + Utilities.formatDate(new Date(), "PST", "MM-dd-yyyy hh:mm a") + "\nRange: " + Ref_Sheet + "!" + Ref_Range);      
      }
    }
    else
    {
      return twoDimensionalArray
    }
  }

}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`

//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//--//Dependent on isEmpty_()
//Find Last Row on Database
function getFirstEmptyRowUsingArray_(sheetname) 
{
  var data = SpreadsheetApp.getActive().getSheetByName(sheetname).getDataRange().getValues();
  for(var n = data.length ; n>0 ;  n--)
  {
    if(isEmpty_(data[n])==false)
    {
      n++;
      break;
    }
  }
  n++;
  return (n);
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`

以下是一个 ImportRange 脚本,因为我讨厌 =importrange() 如何要求您对每个新公式进行 "request access"。如果该 importrange 是嵌入式的,您必须将其取出并请求访问权限并 re-embed 它。也有很多时候公式会被打破。所以:

//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//Script Based ImportRange

//Example importRange_('0AodPsggrbsh1dDNH............','Main4NS','A:G','Common','C7','y')
//Explanation importRange_('Importing Spreadsheet Key or URL','Importing Spreadsheet Tab Name','Importing Spreadsheet Tab's Range','Destination Spreadsheet Tab Name','Destination Spreadsheet Tab's placement','Will add note to the first cell of import')

function importRange_(Source_Key,Source_Sheet,Source_Range,Set_Sheet,Set_Pos,Add_Note) 
{
  var SourceTypeCheck = Source_Key.indexOf("https://"); 
  if(SourceTypeCheck >= 0)
  {
    var Load = SpreadsheetApp.openByUrl(Source_Key).getSheetByName(Source_Sheet).getRange(Source_Range).getValues();
    var Name = SpreadsheetApp.openByUrl(Source_Key).getName();
  }
  if(SourceTypeCheck == -1)
  {
    var Load = SpreadsheetApp.openById(Source_Key).getSheetByName(Source_Sheet).getRange(Source_Range).getValues();
    var Name = SpreadsheetApp.openById(Source_Key).getName();
  }
  var RowVal = SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(Set_Pos).getRow();
  var ColVal = SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(Set_Pos).getColumn();
  if(Add_Note.toUpperCase() == 'Y')
  {
    SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,1,1).setNote("Import Script Updated On: " + Utilities.formatDate(new Date(), "PST", "MM-dd-yyyy hh:mm a")+"\nSS Name: "+Name+"\nRange: "+Source_Sheet+"!"+Source_Range+"\nSS Key: "+ Source_Key);      
  }
  SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,Load.length,Load[0].length).setValues(Load);
  SpreadsheetApp.flush();
  SpreadsheetApp.getActiveSpreadsheet().toast('At: '+Set_Sheet+'!'+Set_Pos,'Import Completed:');
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`

同样的道理。如果您有改进的想法,请告诉我。如果您找到集成它的方法,酷豆,Post 一些您如何使用它的代码。

:)