Final apps script code