Home > Software design >  How to collect data from multiple cells (quantity of 2) and return an output with 2 comma-separated
How to collect data from multiple cells (quantity of 2) and return an output with 2 comma-separated

Time:05-13

First, I believe this question is very similar to the following one. However, I want to know if there is an alternative way to write it so that I'm only looping through 2 cells at a time?

How do I .getValues and .setValues from multiple cells in a single column?

For example, my input is: Cell E21 = $55 Cell F21 = 1

The desired output is: $55, 1

The current output is: $55

This is the code I'm currently using (below). I am not confident about implementing the above linked solution because I definitely don't want to cycle or loop through any complete rows OR columns! If that solution happens to be the only option, please tell me how to write it for my purpose. In the code below, I was hoping it could be written as, "E21:F21" but that's not working! And when I experimented with .getRangeList, I got an error about not converting to Int. So, maybe this is over-communicating, but, I will note that I don't need any decimal point values. The rounded number of $55 flat is great for the first value. Thanks!

                 [formSheet.getRange("E21").getValue()],  
                 [formSheet.getRange("F21").getValue()],

The answer provided below got me mostly there, but I had to have a friend translate it back to me, and ultimately this was the solution! Thanks all!

[formSheet.getRange("E21").getValue()   ", "   formSheet.getRange("F21").getValue()]

CodePudding user response:

My Code:

function elfunko() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const vs = sh.getRange("A2:B"   sh.getLastRow()).getValues().map(r => [`${r[0]},${r[1]}`])
  Logger.log(JSON.stringify(vs));
  sh.getRange(2,4,vs.length,vs[0].length).setValues(vs)
}

My input and output:

COL1 COL2
4 12 4,12
5 19 5,19
16 4 16,4
4 18 4,18
11 12 11,12
13 15 13,15
19 7 19,7
14 10 14,10
10 6 10,6
9 18 9,18
18 9 18,9
19 10 19,10
19 4 19,4
8 9 8,9
4 4 4,4
14 19 14,19
3 16 3,16
5 18 5,18
16 19 16,19
13 5 13,5
  • Related