Home > Software engineering >  How to copy over a specific range for every sheet after a specific one, instead of just one value
How to copy over a specific range for every sheet after a specific one, instead of just one value

Time:05-11

I come here with a rather specific inquiry I couldn´t quite figure out on my own, since I am probably running into somewhat of a language barrier for this.

Essentially, I have this function:

function moneyowened() {
  var out = new Array()
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for (var i=2 ; i<sheets.length ; i  ) out.push([sheets[i].getRange("a44").getValue()])
  return out 
}

Which works for it´s intended purpose perfectly, as it makes an array out of every value of every sheet after the second. Now, for another project, I thought to copy this over since it required a similar task.

The difference being: the target that needs to be copied is an actual range of values. (specifically a row)

The difficulty lies within this factor as I can not figure out how to make it actually apply to a range of values. It either returns blank fields, or just the first value.

I have naturally tried to apply "getValues" and different ways of declaring the range, however, I only ever get either a blank row or just the first value of the row I need to copy.

One way I could resolve this is naturally to simply create a function to get each value independently, but that would be extremely tedious, and so, I wanted to ask if there is an solution I am blind to.

Thanks most kindly for any aid rendered.

Lists of Variations tried:


for (var i=2 ; i<sheets.length ; i  ) out.push([sheets[i].getRange("a44:z44").getValues()])

This results in a Blank row

CodePudding user response:

Get Rows

function getRow44() {
  var out = [];
  SpreadsheetApp.getActive.getSheets().forEach((sh, i) => {
    if (i > 1) out.push(sh.getRange(44,1,1,sh.getLastColumn()).getValues().flat());
  });
  console.log(JSON.stringify(out));
  return out;
}

CodePudding user response:

Here's a pretty way to do it:

function getRow44() {

    return SpreadsheetApp.getActiveSpreadsheet()
                         .getSheets()
                         .slice(2)
                         .flatMap(i => i.getSheetValues(44, 1, 1, i.getLastColumn()))

}
  • Related