A Tomka
A Tomka

Reputation: 1

How to copy row using a checkbox from one tab and add it to the bottom of the tab and two additional tabs?

There are several StackOverflow questions that address moving a row of data to an outside sheet and within a sheet. What I'd like to do is unite these within the same sheet but I've had issues with the script working onEdit and writing to the same tab and two additional tabs within the same sheet using a loop.

If the checkbox in the "test1" tab is selected, duplicate the row at the bottom of tabs "test1", "test2", and "completed"

https://docs.google.com/spreadsheets/d/1_wHyVcR-rCBP6doJEQFqjwCdD6gzXsk_eU8p8MusmvY/edit?usp=sharing

function onEdit(event) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //var sec = SpreadsheetApp.getActiveSheet();
  //  var s = ss.getSheetByName("test1");
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  // Duplicates an order as a new draft on the last row of the draft tab
  if (s.getName() == "test1" && r.getColumn() == 1 && r.getValue() == true) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();

    //var targetSheet = ss.getSheetByName("draft");

    var sheets = ['test1', 'test2', 'Completed']
    for (var i = 0; i < sheets.length; i++) {
      var sheetName = sheets[i]
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
      if (sheet != null) {
        targetsheet(sheet)

      }
    }


    var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
    sheets.forEach(function(sheet) {
      targetsheet(sheet)
    })

    var target = sheets.getRange(sheets.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).copyTo(target);
    s.copyRow(row);
  }
}

Upvotes: 0

Views: 77

Answers (1)

Cooper
Cooper

Reputation: 64100

Try this:

function onMyEdit(e) {
  e.source.toast('Entry')
  const sh=e.range.getSheet();
  const sA=['Sheet2','Sheet3','Sheet4'];//tabs to copy to
  if(sh.getName()=='Sheet1' && e.range.columnStart==9 && e.value=="TRUE") {
    e.source.toast('Conditional');
    const row=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues()[0];
    sA.forEach(function(name){
      let sh=e.source.getSheetByName(name);
      sh.appendRow(row);
    });
  }
}

Upvotes: 0

Related Questions