Skip to content
Advertisement

Find Google Sheets that matches a value in a cell of a sheet and execute a piece of code

I am sorry for this hideouts looking question but I have to ask. So bare with me.

I am using google app script and trying to achieve following. I have two google spreadsheet books.

1.) Book A has only one sheet.
2.) Book B has 10 sheets.



1.) In Book A’s sheet column A contains range of text values. Ex: ALBWA ,AUSCE, AUSLN , BFLCB
2. In Book B there are multiple sheets named according to the values in Book A’s sheet. Ex : Book B, Sheet 1 Name = ALBWA , Sheet 2 Name = AUSCE etc… 3. Inside all these Sheets in same Cell (“G3”) has a value.

The thing I am trying to achieve is: Using a script get all the text values of Book A’s Sheet and search for the Sheet in Book B. If a Sheet is found then get “G3” value and set it in the column B of Book A in front of the matching text. If Sheet “ALBWA” “G23” has a text called “ABC” then it should be pasted to Book A’s sheet in column B like A1 = ALBWA and B1 should be “ABC”. This needs to be repeated until all the paged are covered.

I know I need to use for loop and I am trying my level best. i managed to get it work but it only gets the last value and return only the last sheet and value. Code is below.

JavaScript

If some kind person can help me out with figuring out this I will be so grateful. Thank you so much in advance.

Advertisement

Answer

The below script accomplishes the following:

  1. Collect the sheet names from the range you specify in book A
  2. Find each collected sheet in book B
  3. Get the value in the range you specify for each sheet in book B
  4. Paste the value from 3 into the paste range you specify in book A

Working Example: Book A Book B

JavaScript

Note that one key difference between this script and yours is that I am collecting all of the needed values from sheets in book B before pasting them into the paste range, as is recommend by Google for minimizing costly calls to the spreadsheet service.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement