Match text strings of two columns on different sheet and post back using google script

前端 未结 2 1759
自闭症患者
自闭症患者 2021-01-25 22:49

I am trying to match two columns on two separate worksheets in the same workbook

I\'m am trying to match column A in both sheets

SheetA = FindReplace SheetB = T

相关标签:
2条回答
  • 2021-01-25 23:17

    How about this modification? Please think of this as one of several solutions.

    • It processed the data as array.
    • If the value of column A of sheetB is not existing to the column A of sheetA, if (b != res.length - 1) res.push([""]); is used.
      • The length of created array is necessary to be the same to the length of dataB. I used this.

    Modified script :

    function MatchColumns(){
      // gets spreadsheet A and the range of data
      var sheetA =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("FindReplace");
      var dataA = sheetA.getRange(2, 1, sheetA.getLastRow(), 2).getValues();
    
      // gets spreadsheet B and the range of data
      var sheetB = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
      var dataB = sheetB.getRange(2, 1, sheetB.getLastRow(), 1).getValues();
    
      // Added
      var res = [];
      for (var b in dataB) {
        for (var a in dataA) {
          if (dataA[a][0] == dataB[b][0]) res.push([dataA[a][1]]);
        }
        if (b != res.length - 1) res.push([""]);
      }
      sheetB.getRange(2, 2, res.length, res[0].length).setValues(res);
    }
    

    Result :

    If I misunderstand your question, please tell me. I would like to modify.

    0 讨论(0)
  • 2021-01-25 23:27

    The function did run after changing the > for <

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