Google表格查找工具帮助

时间:2015-04-30 18:36:33

标签: google-sheets gs-vlookup

我自己学会了如何超越" excel"。这让我的头脑更有意义,但是嘿。

我已经构建了一个"查找工具"可以查看here

"洗碗机搜索"正在按预期工作。您可以按噪声级别或装饰面板高度进行搜索,以创建合适模型的列表,然后按产品代码/型号no将数据拉入单个搜索中。

我决定让它更难一点并为烤箱创造一个。

布局是这样的;主要搜索>单一烤箱数据库>双层烤箱数据库>内置烤箱数据库。 我的目标是实现与"洗碗机工具相同的搜索工具"但是我一直不确定如何从不同来源搜索(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$19="Single Oven",$A$4>0), Vlookup($A$4,'Single Ovens'!$B:$F,1,False),IF(AND($A$10="Double Oven",$A$4>0), Vlookup($A$4,'Double Ovens'!$B:$F,1,False),If(AND($A$10="Built-Under Oven",$A$4>0), Vlookup($A$4,'Built-Under Ovens'!$B:$F,1,False),IF($A$10="Single Oven",Vlookup($A$7,'Single Ovens'!$A:$F,2,False),IF($A$10="Double Oven", Vlookup($A$7,'Double Oven'!$A:$F,2,False),If($A$10="Built-Under Oven", Vlookup($A$7,'Built-Under Oven'!$A:$F,2,False)))))))

上面的代码已被插入,并且是"意思是"到Vlookup全部3张并拉出"产品代码"通过填写表格' Ovens'上的Cell D4。

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

你们可以提出任何建议或指出我正确的方向吗?

编辑 -

对不起伙计们。不发布我的解决方案是不礼貌的。我已将Importrange函数更改为= Importrange(" XYZ",""' Single Ovens'!$ A2:$ F200")并重复此3时间差距为200"行"每一个之间。不是解决方案,而是可行的替代方案。我的朋友目前正在编写一个脚本来实现这一目标。由于importrange公式简化了问题,Vlookup公式不再需要那么复杂。

2 个答案:

答案 0 :(得分:2)

所以在与McSheehy讨论问题之后,问题实际上是这个。

如何从一张电子表格中获取数据,我选择多张。 并写入许多电子表格,我选择的电子表格中的多个工作表。

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

我提出了一个脚本解决方案, 用户在源表中创建设置表。 在A2向下,目标电子表格键,B2向下,您希望包含在当前表格中的源表格名称。 C2向下是目标SHEET名称,如果您想将数据写入多个工作表。

代码的注释有助于解释McSheehy关于其工作原理的问题。

如果有人有任何改进建议,我确定有一些,特别是标题位。 (它不需要,但我的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
      }
    }
    }

答案 1 :(得分:0)

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

以下是一个vlookup脚本,用于设置或返回内存,提示查询ish abilites。

//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//--//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()要求您对每个新公式“请求访问”。如果嵌入了该importrange,则必须将其取出并请求访问并重新嵌入它。公式很多时候也会破坏。这样:

//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//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:');
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`

同样的事情。如果您有改进的想法,请告诉我。如果你找到一种方法来集成它,那么就可以发布一些你如何使用它的代码。

:)