What is an equivalent for INDEX in ARRAYFORMULA?

前端 未结 3 1853
Happy的楠姐
Happy的楠姐 2021-02-15 15:22

I have (what I thought was) a simple lookup table holding some exchange rates. There I am looking up values based on row and column indices.

How can I do that in an ARRA

相关标签:
3条回答
  • 2021-02-15 16:06

    You can write a custom script to do this, which can then be used in place of the regular index() function. Just do to Tools --> Script editor then paste in the code, save, then you can use the function like a normal function in Google sheets.

    Code:

    function INDEXMULTI(array, rows, columns) {
      var ret = new Array;
      var i;
      if (rows[0].length != columns[0].length)
        return "Error: Row and column count must be the same";
      for (i=0; i<rows[0].length; i++)
        ret.push(array[(rows[0][i]-1)][(columns[0][i]-1)]);
      return ret;
    }
    

    This function takes the array you want to extract the data from as the first argument and then the rows and columns of the data to be extracted as the second and third arguments. The [0] in the code are just to extract the values from 1-D arrays and the -1 are because javascript arrays are zero based whereas Google sheets is 1 based.

    You can see a demo of it here: https://docs.google.com/spreadsheets/d/1o6uiRr_OKh6lOUY4pOp_5z7hAIzGifFaXUIMOO7SCoc/edit#gid=0

    0 讨论(0)
  • 2021-02-15 16:09

    vlookup can be adapted to mimic index functionality by making the first column of the lookup table the row number (with a suitable offset). Example:

    =arrayformula(vlookup(F1:F, {row(x!C2:C) - row(x!C2) + 1, x!C2:C}, 2))
    

    does what you tried to do with "=ARRAYFORMULA(INDEX(x!C2:C, F1:F))".

    The lookup table {row(x!C2:C) - row(x!C2) + 1, x!C2:C} has first column 1,2,3,... and the second column the range you wish to index. So, for each value from F1:F, vlookup accesses the entry of x!C2:C that index would.

    0 讨论(0)
  • 2021-02-15 16:15

    you can replace index by Vlookup by writing "Anything" in x!C1 and use : =ARRAYFORMULA(Vlookup("Anything", TRANSPOSE(x!C2:C),F1:F))

    0 讨论(0)
提交回复
热议问题