0

Can't figure out how to make this work. I'm basically making a clock in and clock out time sheet. So when they select the check box, it will run a script to clock them in by copying the time over to another column and deleting the checkbox. And another onEdit script that does the same for clocking out. However, both scripts only perform the clocking out action. Here's my code.

function onEdit(e) {
  // --------- CLOCKING IN ----------
  // Get Range  
  var sheetName = 'Sheet1'
  var cellName = 'D2'
  // Set Value False
  SpreadsheetApp.getActiveSheet().getRange('D2').setValue('FALSE');  
  // Move Time
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('G2').activate();
  spreadsheet.getRange('B2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}  

function onEdit(e) {
  // --------- CLOCKING OUT ----------
  // Get Range  
  var sheetName = 'Sheet1'
  var cellName = 'E2'
  // Set Value False
  SpreadsheetApp.getActiveSheet().getRange('E2').setValue('FALSE');  
  // Move Time
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('H2').activate();
  spreadsheet.getRange('B2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}

And a picture of my Gsheet:

Link to Image

Eagle Eye
  • 3
  • 1
  • 1
    Does this answer your question? [Best Practices for Multiple OnEdit Functions](https://stackoverflow.com/questions/60493793/best-practices-for-multiple-onedit-functions) – Rubén Jul 02 '20 at 03:47
  • 2
    Related https://stackoverflow.com/a/62603739/1595451 – Rubén Jul 02 '20 at 03:50
  • 1
    Does this answer your question? [Merge two onEdit with if functions?](https://stackoverflow.com/questions/62602747/merge-two-onedit-with-if-functions) – TheMaster Jul 02 '20 at 09:56

1 Answers1

-1

Try this:

function onEdit(e) {
  const sh=e.range.getSheet();
  if(sh.getName()=='Sheet1' && e.range.columnStart==4 && e.range.rowStart==2 &&  e.value=="TRUE") {
    e.range.setValue('FALSE');  
    sh.getRange('G2').setValue(sh.getRange('B2').getDisplayValue());
  }
  if(sh.getName()=='Sheet1' && e.range.columnStart==5 && e.range.rowStart==2 &&  e.value=="TRUE") {
    e.range.setValue('FALSE');  
    sh.getRange('H2').setValue(sh.getRange('B2').getDisplayValue());
  }
} 

Please note, you cannot run this sort function with providing it an event object. So no you can't run it from the script editor you will just get the error some thing cannot find method getSheet() of null.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thank you for this, it worked great! So then my other issue that I wanted, is some of my employees have to clock out mid day and then clock back in (due to rain and what not since we do landscaping). How would I offset my clock in and clock out times so that they "stack" in a sense for that day? Then I'll just run an overnight script to clear the clock in and clock out times. I'll attach a screenshot. https://imgur.com/HSx3PYf – Eagle Eye Jul 03 '20 at 11:00