Home > Enterprise >  Google sheets script to export rows to different worksheet
Google sheets script to export rows to different worksheet

Time:11-12

Here is a script I'm using that works, but is trying to import too much data. I apologize for my ignorance when it comes to this.

    function copyPaste() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getSheetByName("Print Results");
  var rangeSource = source.getDataRange();
  var data = rangeSource.getValues();
  var lr = rangeSource.getLastRow();
  var lc = rangeSource.getLastColumn();
  Logger.log(data);


  var sss = SpreadsheetApp.openById("XXXXXX");
  var target = sss.getSheetByName("Sheet2")
  target.getRange(target.getLastRow() 1,1,lr,lc).setValues(data);
  }

What I need this to do is only select A8:M, and only copy over the rows that have a USER ID in column J. The USER ID is always a number, so a simple rule of ">0" should work for that. USER ID is imported from a different tab using a vlookup formula so the cell is not "empty".

CodePudding user response:

Only copy rows where Column J is not null

function copyPaste() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Print Results");
  const rg = sh.getDataRange();
  const vs = rg.getValues().filter(r => r[9] != '');
  Logger.log(vs);
  const sss = SpreadsheetApp.openById("XXXXXX");
  const target = sss.getSheetByName("Sheet2")
  target.getRange(target.getLastRow() 1,1,vs.length,vs[0].length).setValues(vs);
  }

.filter() method

  • Related