0

I have a sheet with rows I'd like to move to another sheet based on a cell value. I tried following this post's solution (refer below), but I'm having trouble editing the script towards what I want it to do.

I'm doing Check-Ins for an event. I would like to be able to change the value in Column F, populate Column G with the time the status changed, and for the row data to migrate to the Attendee Arrived sheet.

I believe the script already does this, but one has to run it manually. It also takes care of deleting the row data in A (Event) after migrating it to B (Attendee Arrived).

My question is could someone please help me set it up in order for script to run continuously (on edit), and also accomplish all of the above if I missed something?

I don't believe the script will respect the drop down format as it runs so I'm willing to manually type in something. It'd be cool if it could stay that way though - makes it easier for one.

Here's the sheet I'm testing on.

https://docs.google.com/spreadsheets/d/1HrFnV2gFKj1vkw_UpJN4tstHVPK6Y8XhHCIyna9TLJg/edit#gid=1517587380

Here's the solution I tried following. All credit to Jason P and Ritz for this.

Google App Script - Google Spreadsheets Move Row based on cell value efficiently

Thank you D:

 function CheckIn() {

// How Many Columns over to copy
var columsCopyCount = 7; // A=1 B=2 C=3 ....

// What Column to Monitor
var columnsToMonitor = 6; // A=1 B=2 C=3 ....

//TARGET SPREAD SHEETS
var target1 = "Attendee Arrived";

//Target Value
var cellvalue = "Attendee Arrived";


//SOURCE SPREAD SHEET
var ss = SpreadsheetApp.openById('1HrFnV2gFKj1vkw_UpJN4tstHVPK6Y8XhHCIyna9TLJg');
var sourceSpreadSheetSheetID = ss.getSheetByName("Event");
var sourceSpreadSheetSheetID1 = ss.getSheetByName(target1);


var data = sourceSpreadSheetSheetID.getRange(2, 1, sourceSpreadSheetSheetID.getLastRow() - 1, sourceSpreadSheetSheetID.getLastColumn()).getValues();

var attendee = [];


for (var i = 0; i < data.length; i++) {

    var rValue = data[i][6];

    if (rValue == cellvalue) {
        attendee.push(data[i]);
    } else { //Fail Safe
        attendee.push(data[i]);
    }
}

if(attendee.length > 0){

sourceSpreadSheetSheetID1.getRange(sourceSpreadSheetSheetID1.getLastRow() + 1, 
1, attendee.length, attendee[0].length).setValues(attendee);
}


//Will delete the rows of importdata once the data is copided to other 
sheets
    sourceSpreadSheetSheetID.deleteRows(2, 
     sourceSpreadSheetSheetID.getLastRow() - 1);
  }
ChunkyFresh
  • 65
  • 1
  • 9

1 Answers1

2

Try this:

I imagine that you already know that you'll need an installable onEdit Trigger and that you can't test a function of this nature by running it without the event object.

function checkIn(e) {
  var sh=e.range.getSheet();
  if(sh.getName()!="Event") return;
  if(e.range.columnStart==6) {
    if(e.value=="Attendee Arrived"){
      e.range.offset(0,1).setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "M/d/yyyy HH:mm:ss"));
      var row=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues()[0];
      e.source.getSheetByName("Attendee Arrived").appendRow(row);
      sh.deleteRow(e.range.rowStart);
    }
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Yeah, I'm sorry this is Chinese to me. I'm not an expert at javascript. The script above is too hard for me to understand. I'm sure it works. I set up the trigger, but all it did was just erase the contents of the Event sheet. – ChunkyFresh May 13 '19 at 02:22
  • I removed it after testing it. It was executing on edit, but it just made the data disappear. – ChunkyFresh May 13 '19 at 03:39
  • 1
    As I understand it you wish to move the data to a sheet named Attendee Arrived. Correct? – Cooper May 13 '19 at 03:45
  • 1
    That is to say that it's working for me on my account. – Cooper May 13 '19 at 03:46
  • Oh man.. I feel so dumb. There was data around the 300~ row mark, and the script was populating after that (which is what it's supposed to do). I was so confused to see an empty Attendee Arrived sheet at the 1-50 row mark. It's working now. Thank you. You are the best! I left you some comments on the third sheet. I don' want to get in trouble for commenting too much on the post. – ChunkyFresh May 13 '19 at 13:59
  • 1
    Yes I occasionally get corrected by the moderators for commenting too much but some problems just take a lot of interaction and some people don’t have enough reputation to go into chats. – Cooper May 13 '19 at 14:38