Google Apps Script - How to copy a column(s) to another sheet at next available column

后端 未结 3 1007
庸人自扰
庸人自扰 2020-12-21 17:58

I have a requirement where I need to take values from one column (or more) and copy them to another sheet in the next available column (s). I have written a script like this

相关标签:
3条回答
  • 2020-12-21 18:40

    The entire 4th column & 17th row onward can be appended as a column into a different sheet with this approach also.

      var sheetfrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheetfrom');
      var sheetto = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheetto');
    
      sheetfrom.getRange(17, 4, sheetfrom.getLastRow(), 1).copyTo(sheetto.getRange(1, 
      sheetTo.getLastColumn()+1, sheetfrom.getLastRow()-17, 1), {
        contentsOnly: true
      });
    

    This also overcomes the empty cell problem of copying data from some Formula outputs.

    0 讨论(0)
  • 2020-12-21 18:45

    If I understood correctly, here is a code that does what you wanted, ie get the values from one sheet in column4 from row 17 and copy it to the other sheet without overwriting to columns starting at row 1

    function readSalesNum() {
    var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales plan");
    var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SalesRecordsMonthly");
    
    // Copy from 17th row, 4th column, all rows for one column 
    var valuesToCopy = sheetFrom.getRange(17, 4, sheetFrom.getLastRow(), 1).getValues();
    
    //Paste to another sheet from first cell onwards
    sheetTo.getRange(1,sheetTo.getLastColumn()+1,valuesToCopy.length,1).setValues(valuesToCopy);
    }
    

    test sheet here, make a copy to run the script - view only

    This covers only the first part of your question, the second part was a bit confusing (as mentioned in my comment above).

    0 讨论(0)
  • 2020-12-21 18:56

    Try this

    *//keep a copy of Sales numbers for every month
    function readSalesNum() {
      var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales plan");
      var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SalesRecordsMonthly");
    
      // Copy from 17th row, 4th column, all rows for one column 
      var rangeValues = sheetFrom.getRange(17, 4, 1, sheetFrom.getMaxRows()).getValues();
    
      //Paste to another sheet from first cell onwards
    
      sheetTo.appendRow(rangeValues[0]);
    
    }
    

    This will do what you want with the range you suggested. However, looks like you're trying to get two different ranges so you'll have to accommodate that.

    0 讨论(0)
提交回复
热议问题