谷歌脚本;改变按列搜索电子表格的匹配功能,返回匹配行

时间:2016-08-05 21:24:46

标签: google-apps-script google-sheets google-speech-api

我需要改变我的匹配功能,匹配单独工作表上的两列并返回一个 匹配到第三张纸,以便它返回匹配的行

我一直在努力,我的头在旋转,没有成功

由于

function RunMatch() {

  // 0 is the LookIn sht column # to match on 
  // 1 is the LookWith sht column # to match on 
  MatchCols("LookInSheet","LookWithSheet","PostbackSheet",0,1)  

}

function MatchCols(sShtName,tShtName,pbShtName,matchIn, matchWith){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var LookInSht = ss.getSheetByName(sShtName);
  var LookWithSht = ss.getSheetByName(tShtName);
  var PBSheet = ss.getSheetByName(pbShtName);

  var LookIn = LookInSht.getDataRange().getValues();
  var LookWith = LookWithSht.getDataRange().getValues();

  var list = [];

  for (i in LookIn){
    var curName = LookIn[i][matchColIn];
    var exists = true;

    for (j in LookWith){
      var curCheck = LookWith[j][matchColWith];
      if (curCheck == curName){
        exists = false;
        break;
      };
    }; // end for j

    list.push([exists ? "" : curName]);

  } // end for i

  PBSheet.getRange(1, PBSheet.getLastColumn() +1, list.length, 1).setValues(list);

}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,当PostbackSheet LookInmatchColIn列中的值LookIn时,您希望matchColWith包含LookWith的整行1}} exists列。

不要使用list,只需在找到匹配项时将行添加到if (curCheck == curName){ list.push(LookIn[i]); break; }

PBSheet

然后,当您写入PBSheet.getRange(1, PBSheet.getLastColumn() +1, list.length, list[0].length).setValues(list); 时,请使用列表大小以确保范围是正确的大小:

var AddButton = document.createElement("button");
var AddString = "add";
var SectorString = "1_1";
AddButton.setAttribute("onclick",'AddOrDeleteDiv(' + JSON.stringify(AddString) + ',' + JSON.stringify(SectorString) + ')');


function AddOrDeleteDiv(addStr, secStr) {
//code 
}