1

我已经承担起学习如何“出类拔萃”的责任。这在我的脑海中更有意义,但是,嘿。

我已经建立了一个“查找工具”,可以在这里查看。

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

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

布局是这样的;主搜索>单烤箱数据库>双烤箱数据库>内置烤箱数据库。我的目标是实现与“洗碗机工具”相同的搜索功能,但是我一直不确定如何从不同来源进行搜索(Vlookup)。

我尝试使用以下公式创建“主数据库”;

={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张表,并通过拉“产品代码”来填充“烤箱”表上的单元格D4。

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

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

编辑 -

对不起大家。不发布我的解决方案是不礼貌的。我已将 Importrange 函数更改为 =Importrange("XYZ",""'Single Ovens'!$A2:$F200") 并重复此操作 3 次,每次之间有 200 个“行”的间隙。不是解决方案,而是一个可行的选择。我的朋友目前正在为我编写一个脚本来实现这一点。由于 importrange 公式简化了问题,Vlookup 公式不再需要那么复杂。

4

2 回答 2

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
      }
    }
    }
于 2015-05-03T19:58:46.747 回答
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:');
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`

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

:)

于 2015-12-29T17:54:07.847 回答