简体   繁体   中英

Copy rows that meet a date in Google Sheets

在此处输入图片说明 I want to be able to copy rows from a source sheet to a destination sheet that is the date in a range equals a target date.

I am using the following script and works perfectly when the target is a 'text', but as soon as I try to use a 'date' I get the following error: TypeError: Cannot read property "length" from undefined.

function copyrange() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Source'); //source sheet
   var testrange = sheet.getRange('F:F');
  var testvalue = (testrange.getValues());
  var csh = ss.getSheetByName('Destination'); //destination sheet
  var data = [];
  var j =[];

  //Condition check in L:L; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
  if ( testvalue[i] == 'target') {
  data.push.apply(data,sheet.getRange(i+1,1,1,45).getValues());
  //Copy matched ROW numbers to j
  j.push(i);
 }
 }
//Copy data array to destination sheet

 csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);

}

Thanks Simon

I wasn't sure what your target was so I assumed current date and proceeded anyway. If it's wrong let me know.

function copyrange() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Source');
  var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
  var csh=ss.getSheetByName('Destination');
  var found=false;
  var dt=new Date();
  var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
  var output=[];
  for (var i=0;i<vA.length;i++) {
    var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
    var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
    if (dv==today) {
      output.push(vA[i]);
      var found=true;
    }
  }
  if(found) {
    csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
  }else{
    SpreadsheetApp.getUi().alert('No matches');
  }
}

The following function will get those rows that have todays date and this email email1@test.com.

function copyrange(target) {
  var target=target||'email1@test.com';
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Source');
  var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
  var csh=ss.getSheetByName('Destination');
  var found=false;
  var dt=new Date();
  var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
  var output=[];
  for (var i=0;i<vA.length;i++) {
    var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
    var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
    if (dv==today && vA[i][6]==target) {
      output.push(vA[i]);
      var found=true;
    }
  }
  if(found) {
    csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
  }else{
    SpreadsheetApp.getUi().alert('No matches');
  }
}

And you can change the target email by passing an email to the function.

This function covers dates that have occurred in the last week:

function copyrange(target) {
  var target=target||'email1@test.com';
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Source');
  var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
  var csh=ss.getSheetByName('Destination');
  var found=false;
  var dt=new Date();
  var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
  var sevendaysago=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-7).valueOf();//midnight
  var output=[];
  for (var i=0;i<vA.length;i++) {
    var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
    var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
    if (dv>=sevendaysago && dv<=today && vA[i][6]==target) {
      output.push(vA[i]);
      var found=true;
    }
  }
  if(found) {
    csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
  }else{
    SpreadsheetApp.getUi().alert('No matches');
  }
}

This function only copies B and G of the matching row.

function copyrange(target) {
  var target=target||'email1@test.com';
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Source');
  var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
  var csh=ss.getSheetByName('Destination');
  var found=false;
  var dt=new Date();
  var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
  var sevendaysago=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-7).valueOf();//midnight
  var output=[];
  for (var i=0;i<vA.length;i++) {
    var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
    var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
    if (dv>=sevendaysago && dv<=today && vA[i][6]==target) {
      output.push([vA[i][1],vA[6]]);
      var found=true;
    }
  }
  if(found) {
    csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
  }else{
    SpreadsheetApp.getUi().alert('No matches');
  }
}

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