3
votes

I'm looking for some help on looping through a set of data in a spreadsheet by row, then write the value in the first cell to a new sheet followed by each cell value across the rows.

Here is a link to a sample set of data. Sample Data

Sheet1 on the above spreadsheet shows the raw data. I want to read in that data and write it to sheet two (2) so that it looks like what is on sheet 2 of the above link.

Here is the current code I have but it writes all data into a single column.

function myfunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var sheet2 = 
      SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++)  {
    for (var j = 0; j < data[i].length; j++) {
      var iava_value = (data[i][0]);
      var lastRow = sheet2.getLastRow();           
      sheet2.getRange(lastRow +1, +1).setValue(data[i][j]);
    }
  }
}
2
Try for (var i = 0; i < data.length; i++) {Juniar
Replace [0] with [j] inside the loop. To be var iava_value = (data[i][j]); Then set sheet2 like: sheet2.getRange(lastRow +1,+1).setValue(data[i][0]);Juniar
Thank you for the suggestions.. Didn't get quite what I wanted.jjones312

2 Answers

3
votes

Here is a working version of what I was trying to do.

function transpose() {
  var sheet = 
     SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var sheet2 = 
     SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
  var data = sheet.getDataRange().getValues();  
  var myRA = new Array(2); 
  for (var i = 0; i < data.length; i++)  {
    //GET IAVA No
  var iava_no = data[i][0];    

    for (var j = 1; j < data[i].length; j++) {
      var plugin_id = (data[i][j]);      

    if(plugin_id === ""  || plugin_id =="n"  || plugin_id == "a"){        
     break;        
    }
    else
    {
    Logger.log(plugin_id);
    var lastrow = sheet2.getLastRow();      
    sheet2.getRange(lastrow + 1, 1).setValue(data[i][0]);        
    sheet2.getRange(lastrow + 1, 2).setValue(data[i][j]);
   }
 }
 }
 }
0
votes

This code will write the data across in first available row

function myfunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');  
  var lastRow = sheet2.getLastRow();
  var colCounter = 1
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++)  {
    for (var j = 0; j < data[i].length; j++) {
      var iava_value = (data[i][0]);       
      sheet2.getRange(lastRow +1, colCounter).setValue(data[i][j]);
      colCounter++;
 }
 };
 }

However, note the above code is the not the most efficient way to process/write the data. A lot of overhead = time to writing values individually to the sheets.

This is a much better approach

function myfunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');  
  var lastRow = sheet2.getLastRow();
  var data = sheet.getDataRange().getValues();
  var reArrangeData = []
  for (var i = 0; i < data.length; i++)  {
    reArrangeData = reArrangeData.concat(data[i])
    // concat the arrays end over end to get 1D array
 }
 sheet2.getRange(lastRow +1, 1, 1,   reArrangeData.length).setValues([reArrangeData]);
 };
 }

Hope that helps