使用谷歌应用程序脚本进行Vlookup或索引编制

时间:2012-06-28 11:10:56

标签: google-apps-script google-sheets gs-vlookup

我有一个spreadshseet(Sheet1),其中数据是从Col A到D列,在另一张表(Sheet1)中,数据是从Col A到Col W,其中Col F数据有一些与D列匹配。

我在追求的是什么:

我想从Sheet2中的Sheet2(从Col F开始,即G,H,I等)中提取数据,然后在Sheet1中提取数据。 的 Sheet 2中 上校

F G H I J K L 1 A B C D E F. 2 a1 b1 c1 d1 e1 f1 3 a2 b2 c2 d2 e2 f2 等等

Sheet 1中 Col D E F G H.     1 A B C D.     3 a2 b2 c2 d2

数据反映在sheet1中sheet1中的col E,F,G H与col E,F中列D的反映,或者使用vlookup或索引。

我尝试了但是徒劳无功 http://productforums.google.com/forum/#!topic/apps-script/HzeNdIqnIUc

我只想使用谷歌应用程序来获得所需的结果。

请求帮助。

此致

2 个答案:

答案 0 :(得分:0)

很抱歉,您的VLOOKUP存在问题,但这是Google Spreadsheets用户问题,而不是stackoverflow(编程)问题。它可能会帮助您了解这些事情:

  1. 你正在使用VLOOKUP走上正轨。电子表格函数VLOOKUP绝对适用于将sheet2中的数据基于sheet1中的键/列进行查找。它非常适合你说你需要做的事情。
  2. 您可以搜索Google支持网站:http://support.google.com/docs/bin/search.py?query=vlookup
  3. 您可以在Google文档帮助论坛https://productforums.google.com/forum/#!forum/docs
  4. 中提问 祝你好运。

答案 1 :(得分:0)

这是一篇旧文章,但其他人可能仍会偶然发现它。

我写这篇文章是为了以脚本形式解决我的Vlookup需求。

//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//--//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:

var LocNum    = SpreadsheetApp.openById(SheetID).getSheetByName('Sheet1').getRange('J2:J').getValues();

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_(Sheetinfo,"Sheet1!A:G",4,[0],"Database!A1","y","y");
*/



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
    }
  }

}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
// Empty String Check
function isEmpty_(string) 
{
  if(Object.prototype.toString.call(string) == '[object Boolean]') return false;

  if(!string)             return true;         
  if(string == '')        return true;
  if(string === false)    return true; 
  if(string === null)     return true; 
  if(string == undefined) return true;
  string = string+' '; // check for a bunch of whitespace
  if('' == (string.replace(/^\s\s*/, '').replace(/\s\s*$/, ''))) return true;       

  return false;        
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`