Home > database >  Google Sheets script - For each used cell in range 1, if cell value exists in range 2, get range 2 m
Google Sheets script - For each used cell in range 1, if cell value exists in range 2, get range 2 m

Time:10-14

I feel I have this script almost working as intended, but I am unsure as to where to place certain components of the procedure within the for loop to achieve the desired result. Tunnel vision is very much in effect right now, it's entirely possible I am overthinking a simple task. Please let me know if I can describe the issue more clearly. Any suggestions or pointers towards an existing resource are helpful.

Setup: Sheet one contains a dynamic vertical list of text values starting in cell I3 going down. Sheet two contains a dynamic vertical list of text values starting in range A2 going down, and has a similar set of text values in the same rows in column B.

Goal:

  • Get the value of each used cell in Sheet1 column I (range one)
  • Get the value of each used cell in Sheet2 column A (range two)
  • For each used cell in range one, check the value of each range one cell to see if the value exists in range two
  • If a match exists, get the row number of the cell in range two that contains the value (there will only ever be a single match if a match exists)
  • Get the value of the cell on that same row in Sheet2 column B
  • Set the above value as the cell value in the row containing the value found in both ranges on Sheet1 Column M

Below is what I have been able to come up with. I am able to get it to function up to the last step. It seems the matching rowNumber variable is not updating with each for loop.

// Get values of range one
var lastRowRangeOne = sheetone.getRange('I3').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var rangeOneValues = sheetone.getRange('I3:I'   lastRowClientColumn).getValues();

// Get values of range two
var sheettwo = spreadsheet.getSheetByName('Sheet2');
var sheettwoLastRow = sheettwo.getLastRow();
var sheettwoList = sheettwo.getRange('A2:A'   sheettwoLastRow).getValues();
var sheettwoListFlat = sheettwoList.map(function(row) {return row[0];});

    for (var i = 0; i< rangeOneValues.length ; i  ){ // for each row in range one
    
      if (sheettwoListFlat[i] == rangeOneValues[i]) { // if the range one value exists in range two

        var rowNumber = sheettwoListFlat.indexOf(rangeOneValues[i])   3; // get the row number of the matching value found in range two
        var sheetOneColumnMValue = sheettwo.getRange('B'   rowNumber).getValue(); // get the cell value of the same row in sheet two column B
        var sheetOneRowNumber = i   3; // get the row number of the range one value
        sheetone.getRange('M'   sheetOneRowNumber).setValue(sheetOneColumnMValue); // set the cell value of sheet one column M row x, where x is sheetOneRowNumber
      }

    }

CodePudding user response:

function compareTwoCols() {
  const c1 = 'COL1';//column names
  const c2 = 'COL3';
  const ss1 = SpreadsheetApp.openById(gobj.globals.datagenid);//data set 1
  const sh1 = ss1.getSheetByName('Sheet1');
  const [hd1, ...vs1] = sh1.getDataRange().getValues();
  let col1 = {};
  hd1.forEach((h, i) => { col1[h] = i });
  const ds1 = vs1.map((r, i) => {
    return r[col1[c1]];
  });
  const ss2 = SpreadsheetApp.openById(gobj.globals.ssid);//data set 2
  const sh2 = ss2.getSheetByName('Sheet0');
  const [hd2, ...vs2] = sh2.getDataRange().getValues();
  let col2 = {};
  hd2.forEach((h, i) => { col2[h] = i });
  const ds2 = vs2.map((r, i) => {
    return r[col2[c2]]
  });
  let matches = { pA: [] };
  let idx = -1;
  ds1.forEach((e, i) => {
    let from = '';
    do {
      idx = ds2.indexOf(e, from);
      if (~idx) {
        if (!matches.hasOwnProperty(e)) {
          matches[e] = [];
          matches[e].push({ val1: e, row1: i   2, col1: col1[c1]   1, row2: idx   2, col2: col2[c2]  1 });
          matches.pA.push(e);
        } else {
          matches[e].push({ val1: e, row1: i   2, col1: col1[c1]   1, row2: idx   2, col2: col2[c2]   1});
        }
        from = idx   1;
      }
    } while (~idx);
  });
  Logger.log(JSON.stringify(matches));
}

Spreadsheet1 Sheet1:

COL1 COL2 COL3 COL4 COL5
3 4 2 3 4
2 6 6 1 4
5 1 7 5 5
9 8 7 9 5
7 9 0 8 1
8 2 8 7 9
5 8 7 9 9
1 2 0 8 6
2 7 4 0 3
8 2 0 2 6

Spreadsheet2 Sheet0:

COL1 COL2 COL3 COL4 COL5
5 1 2 7 6
4 5 7 8 2
6 3 8 1 5
0 7 6 3 6
4 7 6 1 7
5 6 9 2 1
3 0 2 2 8
4 5 0 8 1
1 3 9 2 2
3 6 7 0 3

Matches Object:

{
   "2":[
      {
         "val1":2,
         "row1":3,//ds1 row 3
         "col1":1,
         "row2":2,//ds2 row 4
         "col2":3
      },
      {
         "val1":2, 
         "row1":3,//ds1 row 3
         "col1":1,
         "row2":8,//ds2 row 8
         "col2":3
      },
      {
         "val1":2,
         "row1":10,
         "col1":1,
         "row2":2,
         "col2":3
      },
      {
         "val1":2,
         "row1":10,
         "col1":1,
         "row2":8,
         "col2":3
      }
   ],
   "7":[
      {
         "val1":7,
         "row1":6,
         "col1":1,
         "row2":3,
         "col2":3
      },
      {
         "val1":7,
         "row1":6,
         "col1":1,
         "row2":11,
         "col2":3
      }
   ],
   "8":[
      {
         "val1":8,
         "row1":7,
         "col1":1,
         "row2":4,
         "col2":3
      },
      {
         "val1":8,
         "row1":11,
         "col1":1,
         "row2":4,
         "col2":3
      }
   ],
   "9":[
      {
         "val1":9,
         "row1":5,
         "col1":1,
         "row2":7,
         "col2":3
      },
      {
         "val1":9,
         "row1":5,
         "col1":1,
         "row2":10,
         "col2":3
      }
   ],
   "pA":[//just an array of all of the matches
      2,
      9,
      7,
      8
   ]
}

CodePudding user response:

If you print the value of sheettwoListFlat, it has only 5 elements. Once the iterator of for loop reaches 5 or more it will automatically set if (sheettwoListFlat[i] == rangeOneValues[i]) { to false. Also the statement will only work if the elements of the same index of both arrays are equal.

It would be also more efficient if you search the Sheet1 using the values of Sheet2 since Sheet2 has lesser value.

Here I used enter image description here

Note: Both produce the same output but using Sheet2 values as search key is more faster than the other.

  • Related