Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Find original line from a google sheet line obtain by filter function

42 views
Skip to first unread message

Serge HAROUTUNIAN

unread,
Mar 31, 2025, 10:20:38 AMMar 31
to Google Apps Script Community
Hello everybody,

I've got a sheet1 with 5 columns of datas.
I've got a sheet2 with some filtered datas from sheet1.

With Google Apps Script, I want to find in sheet1 the first occurence of a choosen line in sheet2.

I've tried to use app script filter function, but I can't because of there's allready filter created in sheet1.

Thanks from responses and leads !!!

Brent Guttmann

unread,
Mar 31, 2025, 9:32:13 PMMar 31
to Google Apps Script Community
Gpt says

function findMatchingRows() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet1 = ss.getSheetByName("Sheet1");
  const sheet2 = ss.getSheetByName("Sheet2");
  
  const data1 = sheet1.getDataRange().getValues(); // Full data from Sheet1
  const data2 = sheet2.getDataRange().getValues(); // Rows to match from Sheet2
  
  const results = []; // Store results: [Sheet2 row, Sheet1 match row]

  for (let i = 0; i < data2.length; i++) {
    const rowToMatch = data2[i];

    const matchIndex = data1.findIndex(row =>
      row.length === rowToMatch.length &&
      row.every((val, idx) => val === rowToMatch[idx])
    );

    if (matchIndex !== -1) {
      results.push([i + 1, matchIndex + 1]); // +1 for row numbers (1-based)
    } else {
      results.push([i + 1, "No Match"]);
    }
  }

  Logger.log("Results: [Sheet2 row, Sheet1 matching row]");
  Logger.log(results);

Serge HAROUTUNIAN

unread,
Apr 2, 2025, 5:26:05 AMApr 2
to Google Apps Script Community
Thanks Brent... and GPT :D
I will try !
Reply all
Reply to author
Forward
0 new messages