Have you used the debugger to check what each line is returning?
I suspect that sheet.getActiveCell() is returning cell A1 no matter which cell is active. It's been a known issue.
See Issue 3496 Try the workarounds suggested at #23 and #41 or Issue 3110 workaround at #2