0

I am a novice to google app scripts. However I need to copy selected rows (with a checkbox) to a second sheet. I am using this code below. However it copies the entire source sheet. I only need rows that I select to be copied. Can someone please show me how to add a "checkbox" within the script so only the rows I checked are copied. Thanks

Code:

function copyRows() {
  var ss=SpreadsheetApp.getActive();
  var ssh=ss.getSheetByName("data 1");
  var tsh=ss.getSheetByName("Sheet1");
  var srg=ssh.getRange(2,1,ssh.getLastRow()+1,ssh.getLastColumn());//assume 1 header row
  var svs=srg.getValues();
  var trg=tsh.getRange(2,1,tsh.getLastRow()+1,1);//assume 1 header row
  var tvs=trg.getValues().map(function(r){return new Date(r[0]).valueOf();});
  svs.forEach(function(r,i){
//if it is not in the target then append it to the target
    if(tvs.indexOf(new Date(r[0]).valueOf())==-1) {
      tsh.appendRow(r);
    }
  });
}

Note: my header does not contain a date column as mentioned below. However I will ask questions on that later.

2

1 Answer 1

0

Use Array.filter(), like this:

function appendCheckedRows() {
  const checkboxColumn = 3; // column C
  const ss = SpreadsheetApp.getActive();
  const data = ss.getRange('data 1!A2:Z')
    .getValues()
    .filter(row => row[checkboxColumn - 1] === true);
  const targetSheet = ss.getSheetByName('Sheet1');
  data.forEach(row => targetSheet.appendRow(row));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.