0

I have in a column the invoice info of a client (name, address, email, phone number) each one in one row of the column A, and I need to Copy it to another range in transpose style.

I used the following script trying to edit it as I need but I do not find how

function copyAndDeleteSalidas () {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();

  var source = ss.getRange ("Sheet1!A1:A6");
  var destSheet = ss.getSheetByName("Sheet2!");

  var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
  source.copyTo (destRange, {contentsOnly: true});
  source.clear ();

}
jcguinea
  • 1
  • 3

1 Answers1

0

You'll need a function for transposing 2D arrays, since it appears there isn't a built-in option for this purpose in Apps Script. There are several versions of such a function on Stack Overflow, I used one of them below.

The main difference from your script is that instead of copyTo, I use setValues and getValues. Also, the destination range now must have the size that matches the size of values inserted in it.

function copyTransposeAndDelete () {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var source = ss.getRange("Sheet1!A1:A6");
  var destSheet = ss.getSheetByName("Sheet2");
  var destRange = destSheet.getRange(destSheet.getLastRow()+1, 1, source.getWidth(), source.getHeight());
  destRange.setValues(transpose(source.getValues())); 
  source.clear ();
}

function transpose(array) {
  return array[0].map(function(col, i) { 
    return array.map(function(row) { 
      return row[i]; 
    });
  });
}
Community
  • 1
  • 1