Home > other >  Looping Through Both Rows and Columns
Looping Through Both Rows and Columns

Time:09-27

I am self taught in Google Script and not very good at it... LOL One thing I have always struggled with is loops. :) Going to try and be as detailed as possible while still keeping this simple... What I need to do is:

  • Start at Row 113
  • Capture the values in the following columns:
    • B, D, F, S, AD, AM, AV, BB, BH, BK, BN, BQ, BT, BW, BZ, CC, CH, CM, CR, CW, and DB
    • Side note, in case it matters: the columns in-between all of the above are not blank
  • Starting at B225, paste each of those values horizontally
    • So, B113 is being pasted into B225, D113 is being pasted into C225, F113 into D225, so on and so forth until DB113 is pasted into V225
  • Repeat the above for Row 114 picking up where Row 113 left off - so, B114 is pasted into W225, D114 into X225, so on and so forth
  • Repeat again, for all rows 115 to 132 continuing to paste horizontally along Row 225

Below is what I was able to come up with after a lot of Google Research and a bunch of failed swings and misses. I am getting stuck on how to get the loop to move horizontally across the columns; turns out that A 1 does not equal B... LOL

function testLoopTwo() {
  var teamSheet = SpreadsheetApp.getActive();
  for (var rowCounter = 113; rowCounter < 133; rowCounter = rowCounter   1) {
    teamSheet.getRange("B225").activate();
    teamSheet.getCurrentCell().setValue(teamSheet.getRange('B'   rowCounter).getValues());
    //This is were I am stuck... how do I move on to C225, D225, etc?
    teamSheet.getCurrentCell().setValue(teamSheet.getRange('D'   rowCounter).getValues());
    //now the active cell should move another column right, to column D
    teamSheet.getCurrentCell().setValue(teamSheet.getRange('F'   rowCounter).getValues());
    //active cell shifts right again, so now it would be column F
    teamSheet.getCurrentCell().setValue(teamSheet.getRange('S'   rowCounter).getValues());
    //So on and so forth...
  }
};

Below was my original thought on how to approach this, but it doesn't work... The idea was a loop to run from Rows 113 to 132 then embedded within that loop another loop to capture each value from the looped role and paste it into Row 225, starting at Column B, one by one. I feel like this approach made more sense than my latest approach above, but I couldn't get either of them to work and not sure what it is that I am doing wrong... :(

function testLoop() {
  var teamSheet = SpreadsheetApp.getActive();
  for (var colCounter = 1; colCounter < 421; colCounter = colCounter   1){  
    for (var rowCounter = 113; rowCounter < 133; rowCounter = rowCounter   1) {
      teamSheet.getRange(225,colCounter).setValue(teamSheet.getRange(rowCounter,1).getValue())
      teamSheet.getRange(225,colCounter 1).setValue(teamSheet.getRange(rowCounter,3).getValue())
      teamSheet.getRange(225,colCounter 2).setValue(teamSheet.getRange(rowCounter,5).getValue())
      teamSheet.getRange(225,colCounter 3).setValue(teamSheet.getRange(rowCounter,18).getValue())
      teamSheet.getRange(225,colCounter 4).setValue(teamSheet.getRange(rowCounter,29).getValue())
      teamSheet.getRange(225,colCounter 5).setValue(teamSheet.getRange(rowCounter,38).getValue())
      teamSheet.getRange(225,colCounter 6).setValue(teamSheet.getRange(rowCounter,47).getValue())
      teamSheet.getRange(225,colCounter 7).setValue(teamSheet.getRange(rowCounter,53).getValue())
      teamSheet.getRange(225,colCounter 8).setValue(teamSheet.getRange(rowCounter,59).getValue())
      teamSheet.getRange(225,colCounter 9).setValue(teamSheet.getRange(rowCounter,62).getValue())
      teamSheet.getRange(225,colCounter 10).setValue(teamSheet.getRange(rowCounter,65).getValue())
      teamSheet.getRange(225,colCounter 11).setValue(teamSheet.getRange(rowCounter,68).getValue())
      teamSheet.getRange(225,colCounter 12).setValue(teamSheet.getRange(rowCounter,71).getValue())
      teamSheet.getRange(225,colCounter 13).setValue(teamSheet.getRange(rowCounter,74).getValue())
      teamSheet.getRange(225,colCounter 14).setValue(teamSheet.getRange(rowCounter,77).getValue())
      teamSheet.getRange(225,colCounter 15).setValue(teamSheet.getRange(rowCounter,80).getValue())
      teamSheet.getRange(225,colCounter 16).setValue(teamSheet.getRange(rowCounter,85).getValue())
      teamSheet.getRange(225,colCounter 17).setValue(teamSheet.getRange(rowCounter,90).getValue())
      teamSheet.getRange(225,colCounter 18).setValue(teamSheet.getRange(rowCounter,95).getValue())
      teamSheet.getRange(225,colCounter 19).setValue(teamSheet.getRange(rowCounter,100).getValue())
      teamSheet.getRange(225,colCounter 20).setValue(teamSheet.getRange(rowCounter,105).getValue())
    }
  }
};

I really appreciate your help on this!! :D

CodePudding user response:

Try this:

function copyvaluestoasingleline() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("teamSheet");
  const vs = sh.getRange("A113:DB132").getValues();
  let n = 2;//column counter starts at two and increments by arr[0].length
  vs.forEach(([,b,,d,,f,,,,,,,,,,,,,s,,,,,,,,,,,ad,,,,,,,,,am,,,,,,,,,av,,,,,,bb,,,,,,bh,,,bk,,,bn,,,bq,,,bt,,,bw,,,bz,,,cc,,,,,ch,,,,,cm,,,,,cr,,,,,,,,,,db],i) => {
    let arr = [[b,d,f,s,ad,am,av,bb,bh,bk,bn,bq,bw,bz,cc,ch,cm,cr,db]];//The assignment in the first parameter of the forEach method is something I learned from Tanaike.
    sh.getRange(225,n,arr.length,arr[0].length).setValues(arr);
    n = arr[0].length;//add number of columns to column counter
  });
  Logger.log('First Line of Input: %s',JSON.stringify(vs[0]))
  Logger.log('Output: %s', JSON.stringify(sh.getRange("225:225").getValues()[0]))
}

3:27:14 PM  Notice  Execution started
3:27:15 PM  Info    First Line of Input: ["A113","B113","C113","D113","E113","F113","G113","H113","I113","J113","K113","L113","M113","N113","O113","P113","Q113","R113","S113","T113","U113","V113","W113","X113","Y113","Z113","AA113","AB113","AC113","AD113","AE113","AF113","AG113","AH113","AI113","AJ113","AK113","AL113","AM113","AN113","AO113","AP113","AQ113","AR113","AS113","AT113","AU113","AV113","AW113","AX113","AY113","AZ113","BA113","BB113","BC113","BD113","BE113","BF113","BG113","BH113","BI113","BJ113","BK113","BL113","BM113","BN113","BO113","BP113","BQ113","BR113","BS113","BT113","BU113","BV113","BW113","BX113","BY113","BZ113","CA113","CB113","CC113","CD113","CE113","CF113","CG113","CH113","CI113","CJ113","CK113","CL113","CM113","CN113","CO113","CP113","CQ113","CR113","CS113","CT113","CU113","CV113","CW113","CX113","CY113","CZ113","DA113","DB113"]
3:27:15 PM  Info    Output: ["","B113","D113","F113","S113","AD113","AM113","AV113","BB113","BH113","BK113","BN113","BQ113","BW113","BZ113","CC113","CH113","CM113","CR113","DB113","B114","D114","F114","S114","AD114","AM114","AV114","BB114","BH114","BK114","BN114","BQ114","BW114","BZ114","CC114","CH114","CM114","CR114","DB114","B115","D115","F115","S115","AD115","AM115","AV115","BB115","BH115","BK115","BN115","BQ115","BW115","BZ115","CC115","CH115","CM115","CR115","DB115","B116","D116","F116","S116","AD116","AM116","AV116","BB116","BH116","BK116","BN116","BQ116","BW116","BZ116","CC116","CH116","CM116","CR116","DB116","B117","D117","F117","S117","AD117","AM117","AV117","BB117","BH117","BK117","BN117","BQ117","BW117","BZ117","CC117","CH117","CM117","CR117","DB117","B118","D118","F118","S118","AD118","AM118","AV118","BB118","BH118","BK118","BN118","BQ118","BW118","BZ118","CC118","CH118","CM118","CR118","DB118","B119","D119","F119","S119","AD119","AM119","AV119","BB119","BH119","BK119","BN119","BQ119","BW119","BZ119","CC119","CH119","CM119","CR119","DB119","B120","D120","F120","S120","AD120","AM120","AV120","BB120","BH120","BK120","BN120","BQ120","BW120","BZ120","CC120","CH120","CM120","CR120","DB120","B121","D121","F121","S121","AD121","AM121","AV121","BB121","BH121","BK121","BN121","BQ121","BW121","BZ121","CC121","CH121","CM121","CR121","DB121","B122","D122","F122","S122","AD122","AM122","AV122","BB122","BH122","BK122","BN122","BQ122","BW122","BZ122","CC122","CH122","CM122","CR122","DB122","B123","D123","F123","S123","AD123","AM123","AV123","BB123","BH123","BK123","BN123","BQ123","BW123","BZ123","CC123","CH123","CM123","CR123","DB123","B124","D124","F124","S124","AD124","AM124","AV124","BB124","BH124","BK124","BN124","BQ124","BW124","BZ124","CC124","CH124","CM124","CR124","DB124","B125","D125","F125","S125","AD125","AM125","AV125","BB125","BH125","BK125","BN125","BQ125","BW125","BZ125","CC125","CH125","CM125","CR125","DB125","B126","D126","F126","S126","AD126","AM126","AV126","BB126","BH126","BK126","BN126","BQ126","BW126","BZ126","CC126","CH126","CM126","CR126","DB126","B127","D127","F127","S127","AD127","AM127","AV127","BB127","BH127","BK127","BN127","BQ127","BW127","BZ127","CC127","CH127","CM127","CR127","DB127","B128","D128","F128","S128","AD128","AM128","AV128","BB128","BH128","BK128","BN128","BQ128","BW128","BZ128","CC128","CH128","CM128","CR128","DB128","B129","D129","F129","S129","AD129","AM129","AV129","BB129","BH129","BK129","BN129","BQ129","BW129","BZ129","CC129","CH129","CM129","CR129","DB129","B130","D130","F130","S130","AD130","AM130","AV130","BB130","BH130","BK130","BN130","BQ130","BW130","BZ130","CC130","CH130","CM130","CR130","DB130","B131","D131","F131","S131","AD131","AM131","AV131","BB131","BH131","BK131","BN131","BQ131","BW131","BZ131","CC131","CH131","CM131","CR131","DB131","B132","D132","F132","S132","AD132","AM132","AV132","BB132","BH132","BK132","BN132","BQ132","BW132","BZ132","CC132","CH132","CM132","CR132","DB132"]
3:27:16 PM  Notice  Execution completed

My data sheet is generated by putting the A1Notation of each cell so that you can tell what is being copied and where it came from.

array destructuring

It's a bit faster using Array.map and flattening it:

function copyThesevalues() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("teamSheet");
  const vs = sh.getRange("A113:DB132").getValues();
  let o = [vs.map(([,b,,d,,f,,,,,,,,,,,,,s,,,,,,,,,,,ad,,,,,,,,,am,,,,,,,,,av,,,,,,bb,,,,,,bh,,,bk,,,bn,,,bq,,,bt,,,bw,,,bz,,,cc,,,,,ch,,,,,cm,,,,,cr,,,,,,,,,,db],i) => [b,d,f,s,ad,am,av,bb,bh,bk,bn,bq,bw,bz,cc,ch,cm,cr,db]).flat()];
  sh.getRange(225,2,o.length,o[0].length).setValues(o);
  Logger.log('First Line of Input: %s',JSON.stringify(vs[0]))
  Logger.log('Output: %s', JSON.stringify(sh.getRange("225:225").getValues()[0]))
}
  • Related