javascript - Add timestamp to named column in Google Spreadsheet onEdit script -


i'm trying figure out how retrieve index of column specific name in google spreadsheet script.

i'm working on custom google script automatically adds timestamps spreadsheet when edited. current version adds timestamp last column of active cell's row.

i want create new version of script adds timestamp specially designated column using specific column name. example, want create column in spreadsheet named "last updated," , want script detect index of column , automatically add timestamps instead of last column in row.

this work better me, because place timestamp column wherever wanted , not have worry script overriding important accident.

my current script looks this:

function onedit(event) {    var timezone = "gmt-4";   var timestamp_format = "yyyy-mm-dd hh:mm:ss";   var sheet = event.source.getactivesheet();    // note: actrng = cell being updated   var actrng = event.source.getactiverange();   var index = actrng.getrowindex();   var cindex = actrng.getcolumnindex();    // here want use named column's index instead of active row's last column.   var datecol = sheet.getlastcolumn();   //var datecol = sheet.getcolumnindexbyname('last updated');    var lastcell = sheet.getrange(index,datecol);   var date = utilities.formatdate(new date(), timezone, timestamp_format);    lastcell.setvalue(date); } 

you values in header row, , search required header in values using indexof().

function onedit(event) {    var timezone = "gmt-4";   var timestamp_format = "yyyy-mm-dd hh:mm:ss";   var sheet = event.source.getactivesheet();    // note: actrng = cell being updated   var actrng = event.source.getactiverange();   var index = actrng.getrowindex();    var headers = sheet.getrange(1, 1, 1, sheet.getlastcolumn()).getvalues();   var datecol = headers[0].indexof('last updated');    if (datecol > -1 && index > 1) { // timestamp if 'last updated' header exists, not in header row itself!     var cell = sheet.getrange(index, datecol + 1);     var date = utilities.formatdate(new date(), timezone, timestamp_format);     cell.setvalue(date);   } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -