I\'m completely new to Google Script so bear with me. I\'m attempting to write a script that will take data from several rows and columns and then rewrite it into a single colum
This seems to work. Manually iterates through the array and sets it with a for loop.
function copyRow() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var numRows = sheet.getLastRow();
var rowIdx = sheet.getActiveRange().getRowIndex();
var colIdx = sheet.getActiveRange().getColumnIndex();
var theValues = sheet.getRange(rowIdx,colIdx,sheet.getLastRow(),3).getValues();
theValues = theValues.toString().split(",");//convert 2D array to 1D
var outerArray = [];
for(var i=0; i<theValues.length; i++){
outerArray.push(theValues[i]);
};
Logger.log(outerArray);
// sheet.getRange(1,1,outerArray.length,30).setValues(outerArray);
for(var i = 0; i < outerArray.length; i++){
sheet.getRange(30 + i, 1).setValue(outerArray[i]);
}
};
Try this code. The getValues()
method gets a 2 dimensional array. Every inner array is a new row. Every element of each inner array is a cell in a new column. The 2D array can be collapsed into a regular array, with no outer array. But to write the values, a 2D array is needed, so the code below creates a new 2D array. Every inner array only has one value.
function copyRow() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var numRows = sheet.getLastRow();
var rowIdx = sheet.getActiveRange().getRowIndex();
Logger.log('rowIdx: ' + rowIdx);
//getRange(start row, start column, number of rows, number of columns)
var theValues = sheet.getRange(rowIdx,1,sheet.getLastRow()).getValues();
Logger.log('theValues.length: ' + theValues.length);
theValues = theValues.toString().split(",");//convert 2D array to 1D
Logger.log('theValues.length: ' + theValues.length);
var outerArray = [];
for(var i=0; i<theValues.length; i++){
Logger.log('theValues[i]: ' + theValues[i]);
outerArray.push(theValues[i]);
};
Logger.log('outerArray.length: ' + outerArray.length);
sheet.getRange(1,1,outerArray.length,outerArray[0].length).setValues(outerArray);
};
Try it and see if it works, and let me know.
You don't need script. Just put this formula in a cell:
=transpose(split(join(" ",A1:A3)," "))
You can join multiple rows and columns. I tested this on data like you provided in column A & B rows 1 to 3. Works fine.
=transpose(split(join(" ",A1:A3,B1:B3)," "))