代码之家  ›  专栏  ›  技术社区  ›  onit

如何使用谷歌表单中的地图获取索引/行号?

  •  0
  • onit  · 技术社区  · 1 年前

    1. 我得到了这样一个数组: [,,2]
    2. 我试过使用 filter() 最后,但它不起作用,如果我已经在使用map遍历数据以获取行,我不确定这会有多高效:
    ar = ["ABC", 25];
    
    vs = [["CBA", 14],
          ["ABC", 25],
          ["DRT", 34]]
    
    function f101() {
      const ss=SpreadsheetApp.getActive();
      const sh=ss.getSheetByName('sheet name');
      const rg=sh.getRange(1,1,sh.getLastRow(),sh.getLastRow());
      const vs=rg.getValues();
      let holder=vs.map((r,i)=>{if(r[0]==ar[0] && r[1] == ar[1]){return i;}});
    }
    

    预期结果: 1 //2nd row

    这是我试图从 this answer

    1 回复  |  直到 1 年前
        1
  •  0
  •   Wicket    1 年前

    Array.prototype.map 使用 Array.prototype.findIndex

    ar = ["ABC", 25];
    
    vs = [["CBA", 14],
          ["ABC", 25],
          ["DRT", 34]]
          
    const h = vs.findIndex((r,i) => r[0] == ar[0] && r[1] == ar[1]);
    console.log(h);
        2
  •  0
  •   Cooper    1 年前

    获取行号

    function f101() {
      const ar = ["ABC", 25];
      const vs = [["CBA", 14],
      ["ABC", 25],
      ["DRT", 34]]
      const ss = SpreadsheetApp.getActive();
      const sh = ss.getSheetByName('sheet name');
      const rg = sh.getRange(1, 1, sh.getLastRow(),sh.getLastColumn());
      const vs = rg.getValues();
      let holder = vs.map((r, i) => { if (r[0] == ar[0] && r[1] == ar[1]) { return i + 1; } });
    }