简体   繁体   中英

Set Values by only columns in google apps script

I'm new to google apps script and I want to set some values in a sheet where the values are from other sheet. Problem is, the values are saving as row wise but I want to save it as column wise. Here are my codes,

function setKeys() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet1 = ss.getSheetByName("Key");
    var sheet2 = ss.getSheetByName("Bldng4");

    var getWindows = sheet1.getRange(2, 2, sheet1.getLastRow()).getValues();
    sheet2.getRange(sheet2.getLastRow() + 10, 2, 1, getWindows.length).setValues(getWindows);
}

How can I add the values as column wise in sheet2 ? Need this help badly. Thanks.

You'll want to transpose your column vector into a row vector.

Currently your data is in the format [[1],[2],[3]] , you want it in the format [[1,2,3]]

This can be done fairly easily like this

function columnToRowVector(columnVector){
  var rowVector = [];
  rowVector.push([]);
  for (var row = 0; row < columnVector.length; ++row){
    rowVector[0].push(columnVector[row][0])
  }
  return rowVector;
}

I have written three small methods for the getValues() objects here which allow you to create column vectors, range vectors and to the transposition I just mentioned in a more general fashion, feel free to use that if you have to do more transpositions.

I solved it like this:

var verticalArray = new Array(number of rows you need);
for (var i = 0; i < verticalArray.length - 1; i++) {
  verticalArray [i] = new Array(1);
  verticalArray [i][0] = horizontalArray[i];
}

Now you may use the verticalArray with setValues() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM