简体   繁体   中英

Setting array values across row in Google Sheets

I have an array pulled from a Google Sheet column using the following code. How can I distribute these values across a row inside of a different sheet? Essentially, I would like the value at decisionPoint[1] to populate cell 'B2', decisionPoint[2] to populate cell 'C2', decisionPoint[3] to populate cell 'D2', and so on. Thanks in advance for any help!

 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // get the spreadsheet object var decisionPointSheet = spreadsheet.getSheets()[2]; // set decision point sheet var lastDecisionPoint = decisionPointSheet.getDataRange().getLastRow(); var decisionPoints = decisionPointSheet.getDataRange().getValues(); for ( var i = 1; i < lastDecisionPoint; i++){ Logger.log(decisionPoints[i][0]); } 

You may try that:

function transpose(){
  var input = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Requests"); // origin sheet
  var out = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("output"); // destination sheet
  var data = input.getDataRange().getValues();
  data.shift(); // remove first row (apparently you are not taking the title)
  var output = []; // better to create your object and then push it at once - it run faster
  for(var i in data){ // I prefer that than the classical var i =1;i<numCol .... 

    if(i<10) output.push(data[i][0]);
  }
  out.getRange(4, 1,1,output.length).setValues([output]); // i you want to specify the row where it need to end
  // out.appendRow(output); // or you can change for that just to appen a new row
}

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