scripting - How to move an entire row from one sheet to another based on the date in a certain column AND a selection in another column -
so blowing mind right , no 1 can seem me out it. have moved entire row "archive" sheet, not criteria mentioned in title , have been messing 2 or 3 weeks. want able archive entire row 30 days old , has value of "complete". i've been trying incorporate onopen script suggestions appreciated if there way.
here onedit script working off of , i've used before (minus criteria want set in place). have edited onopen before , didn't have errors wouldn't work.
the checklist sheet i'd pulling row from, "archive" sheet destination moving of row , column 15 i'd find value "complete". can give access test sheet if necessary.
function onedit(event) { // assumes source data in sheet named needed // target sheet of move named acquired // test column yes/no col 4 or d var ss = spreadsheetapp.getactivespreadsheet(); var s = event.source.getactivesheet(); var r = event.source.getactiverange(); if(s.getname() == "checklist" && r.getcolumn() == 15 && r.getvalue() == "complete") { var row = r.getrow(); var numcolumns = s.getlastcolumn(); var targetsheet = ss.getsheetbyname("archive"); var target = targetsheet.getrange(targetsheet.getlastrow() + 1, 1); s.getrange(row, 1, 1, numcolumns).moveto(target); s.deleterow(row); } }
i have created example spreadsheet here apps script complete task; make copy play it. there portion of spreadsheet on sheet3 calculates index how many rows need archived needed script, sorry i'm new maybe else can edit script include it.
the formula using index records need archived is:
=counta(iferror(filter(sheet1!a:a,sheet1!a:a<today()-30,sheet1!b:b="complete")))
this script:
- on opening spreadsheet runs following steps:
- sorts first 2 columns in sheet1 bring completed rows top.
- uses index on sheet3 move dates older 30 days sheet2.
- erased dates older 30 days off of sheet1
- sorts both columns on sheet1 in order date.
note: option trigger script changing function name, going icon in menu, adding new trigger, , setting function > spreadsheet > on open
function onopen() { var s1 = spreadsheetapp.getactivespreadsheet().getsheetbyname('sheet1'); var s2 = spreadsheetapp.getactivespreadsheet().getsheetbyname('sheet2'); var s3 = spreadsheetapp.getactivespreadsheet().getsheetbyname('sheet3'); var sortrange = s1.getrange(1, 1, s1.getmaxrows(), 2); sortrange.sort([{column: 2, ascending: true}, //first column sort {column: 1, ascending: true}]);//second column sort var lastdateindex = s3.getsheetvalues(3, 2, 1, 1); //number of rows move var range = s1.getrange(1, 1, lastdateindex, 2) startrow = range.getrowindex(), numrows = range.getnumrows(), numcols = range.getnumcolumns() var values = range.getvalues(), lastrow = s2.getlastrow(); s2.getrange(lastrow+1,1,lastdateindex,2).setvalues(values); s1.deleterows(startrow,numrows); var sortrange = s1.getrange(1, 1, s1.getmaxrows(), 2); sortrange.sort([{column: 1, ascending: true}]); }