Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9707

How to include VLOOKUP with a loop for a list of data on Google Scripts?

$
0
0

I have a list of data on Sheet1 columns A-N, and a list of updates on Sheet2, columns A-N. I have a code that imports data to Sheet2, and I would like to add to my code a similar function to VLOOKUP. In essence, I want the code to check the values in Column C on Sheet1 within my list of updated data in Column B on Sheet2. If it finds a match, I want the code to copy/paste the updates in the corresponding columns for that found value in that row in Sheet 1. If it doesn't find a match, I want it to copy/paste the whole row from Sheet2 to the bottom of the list in Sheet1.

In my example file, I have two yellow cells on Sheet 2 highlighted to show what I want the code to find. Essentially, the script should look up the value in Column C on Sheet 1, search for the same value in Column B on Sheet2, and if it finds it, to pull in the updated cells and copy/paste to the corresponding cell on Sheet1. I also have a new set of data in row 5 of Sheet 2, and the code should copy/paste this whole row to Sheet 1.

I have the start of a code, but I know it needs much more:

function TEST() {  var sh = SpreadsheetApp.getActiveSheet();  var ss = SpreadsheetApp.getActiveSpreadsheet();  var last=ss.getLastRow();  var data=sh.getRange(1,1,last,2).getValues();  for(nn=0;nn<data.length;++nn){    if (data[nn][1]==valB){break} ;    //Copy/pastes updates if a value is found    //Copy/pastes whole row if value isn't found  }}

Example Sheet


Viewing all articles
Browse latest Browse all 9707

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>