How to loop through each column in a given row and then have the loop go automatically to the next column

Viewed 42

I need to extract a data from this other sheet where the header will repeat itself by the number of items under it. So far I can only do this with the first column from the data source and I can’t seem to get the right code where the loop will go through the next column once it detects the first empty cell of the previous column.

Here’s where I am getting stuck

function filter(){

var sheet = SpreadsheetApp.getActiveSpreadsheet()
var data = sheet.getSheetByName("data")
var filter = sheet.getSheetByName("filter")

var dataRange = data.getDataRange().getValues();
var dataLr = data.getLastRow();
var dataLc = data.getLastColumn();

var row = 1
var col = 1

 for(var i=1; i < dataRange.length; i++){
  let targetAppName = filter.getRange(i+1,1)
  let targetQname = filter.getRange(i+1,2)

        targetQname.setValue(dataRange[i])
        targetAppName.setValue(dataRange[0][0])

}
}

I’m trying to get these results in another sheet tab:

|Names | Subjects | |:—————|:———————-:|

|Michael Lowry| Trigonometry|

|Michael Lowry| Biology|

And so on, until it reads all columns and rows of the data range

1 Answers

Loop throught each column of each row

function loopthroughteachcolumnofarow() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  const vs = sh.getDataRange().getValues();
  vs.forEach((r,i) => {
    r.forEach((c,j) => {
      //c is the value for vs[i][j];
    })
  })
}
Related