move writing to the spreadsheet to an external script. Fixed a potential bug with key generation