Irfan,
Is there any reason your script is enclosed in php tags? This isn't recommended for adwords scripts, they do not run php, but a modified form of Javascript.
I'm not exactly sure your error, because there is no code on line 46 and the nearest setValue call is line 38, is this where it is crashing? I also see no code where you call setValue after switching to the second sheet on line 84.
var last_check_first = spreadsheet.getRangeByName("last_check").setValue(yesterday);
Make sure your second sheet also has a named range called "last_check". The issue could be that only your first sheet has these named ranges set up on it. To offer any more advice than this, I'd need to see the proper file or error message.
Best of luck,
Neil