Convert row and column data to column-only

前端 未结 3 1286
离开以前
离开以前 2021-01-27 13:38

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

相关标签:
3条回答
  • 2021-01-27 13:41

    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]);
      }
    };
    
    0 讨论(0)
  • 2021-01-27 13:58

    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.

    0 讨论(0)
  • 2021-01-27 14:05

    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)," "))
    
    0 讨论(0)
提交回复
热议问题