-1

Now I'm using a formula to extract the data in company sheet. The sheet and formula I can get the data by the name of A1:A100 and get the data that contain "B1"; but now i also need to get the note of cell that contains B1. Is there any script that can can achieve this? By the way, there are many duplicate names and they all have data including B1; I want to extract all the datas with the same name; The current formula can only extract the data of the top one. Plz help me and I really need this on work. Thank u so much!

Here is the formula:

=BYROW (A1:A100, LAMBDA(each,IFNA( FILTER (INDEX(H2:AQ270, MATCH(each,H2:H270,0)), REGEXMATCH(INDEX(H2:AQ270, MATCH(each,H2:H270,0)),SUBSTITUTE(B1,".","\."))))))
Danel Lau
  • 25
  • 5

2 Answers2

2

I believe your goal is as follows.

  • You want to convert your showing formula to Google Apps Script. When the formula is converted to Google Apps Script, you want to retrieve all values including the duplicated values from columns "J" and "K".
  • Furthermore, when the values are copied, you want to also copy the notes in the cells.
  • You want to put the retrieved values and notes to the cells "D2:D".

About Is there any script that can can achieve this?, I think that in this case, it is required to prepare the script for your situation. So, how about the following sample script?

Sample script:

Please copy and paste the following script to the script editor of Spreadsheet and set your sheet name, and save the script.

function myFunction() {
  const sheetName = "Sheet1"; // Please set your sheet name.

  // Retrieve sheet and data range.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const range = sheet.getRange("A1:K" + sheet.getLastRow());

  // Retrieve notes.
  const notes = range.getNotes();

  // Retrieve values.
  const [[, v], ...values] = range.getValues();

  // Create an object for searching the values of column "A".
  const r = new RegExp(`^${v}`);
  const obj = values.reduce((o, [, , , , , , , h, , ...j], i) => {
    if (h) {
      j.forEach((e, l) => {
        if (e.toString() && r.test(e)) {
          const temp = { v: e, n: notes[i + 1][9 + l] };
          o[h] = o[h] ? [...o[h], temp] : [temp];
        }
      });
    }
    return o;
  }, {});

  // Create new values and notes.
  const newValues = values.flatMap(([a]) => obj[a] ? obj[a].map(e => [e.v]) : []);
  const newNotes = values.flatMap(([a]) => obj[a] ? obj[a].map(e => [e.n]) : []);

  // Put the new values and notes.
  sheet.getRange("D2:D" + sheet.getLastRow()).clearContent().clearNote();
  sheet.getRange(2, 4, newValues.length).setValues(newValues).setNotes(newNotes);
}
  • When this script is run, the values and notes are retrieved from the sheet. And, new values and notes are created by using the value of cell "B1" and values of columns "H", "J", and "K", and those are put to column "D".

Note:

  • I have no your Spreadsheet. So, I prepared a sample script by guessing your situation from your provided sample image. when this sample script was not your expected result, can you provide the sample Spreadsheet including your expected result? By this, I would like to modify the script.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • thank u so much Tanaike! it works so well! There are two question: 1. When there are duplicate values, it will arrange the extracted values into the same column, which will scramble the order. Can these duplicate values be arranged in the same row so that the order will not be wrong 2. What if the values not in "J" and "K" columns but in many columns? Here is a sample sheet, plz help me solve this. Thank u so much! https://docs.google.com/spreadsheets/d/1Sigx9Kmx5Ny41zC_Mq38k1Zxpnw-qNumbEUqqEoeL-o/edit#gid=0 – Danel Lau Mar 19 '23 at 17:51
  • Thank u so much! I've posted a new question and here's the link: https://stackoverflow.com/questions/75786850/is-there-a-script-can-replace-byrow-formula-and-also-get-the-note-of-cell – Danel Lau Mar 20 '23 at 05:37
  • @Danel Lau Thank you for replying. I will check it. And, your this question was resolved. So, please accept the answer. By this, it will be useful for other users who have the same issue. – Tanaike Mar 20 '23 at 05:41
0

Based on the sample, insert this formula in cell G2:

=arrayformula(QUERY({D2:E,row(D2:E)}, "Select Col1, Col2, Col3 where Col1 matches "&"'"&join("|",query({A2:A},"select Col1 where Col1 is not null"))&"' and Col2 contains '"&$B$1&"' label Col1 'Name', Col2 'Value', Col3 'Row#'"))

References
Query with range of values for WHERE clause? - enabling a match from the array in Column A
Get row number with Google spreadsheet query language


SAMPLE

enter image description here

Tedinoz
  • 5,911
  • 3
  • 25
  • 35