Stuck using script to copy a row to a logs sheet

29 views
Skip to first unread message

Rob Balneaves

unread,
May 16, 2018, 12:35:52 PM5/16/18
to Google App Engine
Hi,

I'm attempting to copy a row to another sheet on edit when the user clicks yes. So far I've got this script which I'm trying to modify to lose the var name col = 9 so instead of it trying to find the sheet name to copy to from column 9 I'd like it to be copied to the sheet name logs. Spent hours trying to make it work but im stuck I'm sure its very easy to fix. Any ideas? Thanks in advance 

function onEdit(e) {
  // see Sheet event objects docs
  var ss = e.source;
  var s = ss.getActiveSheet();
  var r = e.range;
  
  // to let you modify where the action and move columns are in the form responses sheet
  var actionCol = 9;
  var nameCol = 10;
  
  var target = SpreadsheetApp.openById('18L5-RGHBx41sMtvC1-7AoVCo6y3imvtELQFjkhPXtoY')
  var targetsheet = target.getSheetByName('logs');
  
  // Get the row and column of the active cell.
  var rowIndex = r.getRowIndex();
  var colIndex = r.getColumnIndex();
  
  // Get the number of columns in the active sheet.
  // -1 to drop our action/status column
  var colNumber = s.getLastColumn()-1;
  
  // if our action/status col is changed to yes do stuff
  if (e.value == "Yes" && colIndex == actionCol) {
    // get our target sheet name - in this example we are using the priority column
   var targetSheet = s.getRange(rowIndex, nameCol).getValue();
    // if the sheet exists do more stuff
    if (ss.getSheetByName(targetSheet)) { 
      // set our target sheet and target range
      var targetSheet = ss.getSheetByName(targetSheet);
      var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
      // get our source range/row
      var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
      // new sheets says: 'Cannot cut from form data. Use copy instead.' 
      sourceRange.copyTo(targetRange);
      // ..but we can still delete the row after
      r.setValue("Logged")
      // or you might want to keep but note move e.g. r.setValue("moved");
    }
  }
}




Kenworth (Google Cloud Platform)

unread,
May 16, 2018, 1:37:38 PM5/16/18
to Google App Engine
This thread is reserved for Google Cloud Platform products and services. For Google Apps Script, you can contact the support page here.
Reply all
Reply to author
Forward
0 new messages