moving rows to a different sheet if timestamp column is more than 90 days ago

49 views
Skip to first unread message

teresa aiki

unread,
Dec 5, 2020, 8:56:15 PM12/5/20
to google-apps-sc...@googlegroups.com

I'm trying to move some rows from one spreadsheet to another based on the column A 'Timestamp'. So if the Timestamp value is more than 90 days ago from today, then the row should be deleted from spreadsheet 'archive' and appended to spreadsheet 'archive archive'. I'm struggling with the coding. Here's what I have so far.

function archiveRow(){

var from = SpreadsheetApp.openById('###').getSheetByName('archive');

var to = SpreadsheetApp.openById('###').getSheetByName('archive archive');

var values = from.getDataRange().getValues();

var lastRow = from.getLastRow();

var olderthan = new Date().setDate(new Date().getDate() - 9);

for(var row=lastRow;row>1;row--)

{

var timestamp = new Date('A'+row);

if(timestamp.valueOf() < olderthan.valueOf())

{

to.appendRow(values[row]);

from.deleteRow(values[row]);

}

}

}

Laurie Nason

unread,
Dec 6, 2020, 12:14:57 AM12/6/20
to google-apps-sc...@googlegroups.com
Hi Teresa,
It looks like your "for" loop might need re-writing as you are not actually retrieving the values in the contained 'if' statement - you probably want something like this:

//check that your start row is not 0 based - you may need to use 1 if you are starting on row 2
for (var row=2;row<values.length;row++){
    // check for the value in column A - I think it's a zero based array - so it will be 0 rather than 1
    if(values[row][0]<olderthan){
        //add to your archive archive sheet
        to.appendRow(values[row]);
        //remove from your archive sheet again I think you may need to check whether the delete row needs an actual row (think it does) rather then the 0 based array
        from.deleteRow(row+1);
    }
}

On Sun, Dec 6, 2020 at 4:56 AM teresa aiki <aikit...@gmail.com> wrote:

I'm trying to move some rows from one spreadsheet to another based on the column A 'Timestamp'. So if the Timestamp value is more than 90 days ago from today, then the row should be deleted from spreadsheet 'archive' and appended to spreadsheet 'archive archive'. I'm struggling with the coding. Here's what I have so far.

function archiveRows(){




var from = SpreadsheetApp.openById('###').getSheetByName('archive');

var to = SpreadsheetApp.openById('###').getSheetByName('archive archive');

var values = from.getDataRange().getValues();

var lastRow = from.getLastRow();

var olderthan = new Date(new Date().setDate(new Date().getDate() - 90));

for(var row=2;row<=lastRow;row++)

{

if('A'+row < olderthan)


{

to
.appendRow(values[row]);

from.deleteRow(values[row])

}

}

}


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/37fdeb68-8b6c-4c61-9a95-a015b735db73o%40googlegroups.com.


--

Laurie Nason 

The KAUST School – Operational Analyst

Information Technology Support
Deep in the Bowels of the School
Landline: +966-12-808-6853

aikit...@gmail.com

unread,
Dec 6, 2020, 12:33:38 PM12/6/20
to Google Apps Script Community
I tried you suggestions but the function skips some rows that it should not. I have since modified my function to hopefully fix the comparison issues, but I am still unable to get it working properly.

Jean-Luc Vanhulst

unread,
Dec 6, 2020, 12:55:42 PM12/6/20
to google-apps-sc...@googlegroups.com
Laurie,

deleteRow() needs a row number - so you would have to do deleteRow( row) . not deleteRow(value[row])

Also, be aware that once you do that, the next row is now a different number (everything shifts) So you might consider instead deleting the rows in the values array first and adding a blank row at the end of array and then doing one range.setValues() at the end?


cbmserv...@gmail.com

unread,
Dec 6, 2020, 1:40:25 PM12/6/20
to google-apps-sc...@googlegroups.com

Teresa,

 

Jean-Luc is correct, Try this:

 

function archiveRows(){

  var from = SpreadsheetApp.openById('###').getSheetByName('archive');

  var to = SpreadsheetApp.openById('###').getSheetByName('archive archive');

  var values = from.getDataRange().getValues();

  var lastRow = from.getLastRow();

  var olderthan = new Date(new Date().setDate(new Date().getDate() - 90));

  var offset = 1;

  for(var row=2;row<=lastRow;row++)

  {

    if(values[row][0]<olderthan)

    {

     to.appendRow(values[row]);

      from.deleteRow(row+offset);

      offset--;

aikit...@gmail.com

unread,
Dec 6, 2020, 4:29:22 PM12/6/20
to Google Apps Script Community
That seems to work! I just needed to start from row 1 instead of row 2: `for(var row=1;row<=lastRow;row++)' and add an `else return;` statement to avoid the "TypeError: Cannot read property '0' of undefined " . Just for my edification, that error has something to do with the offset? What exactly does `offset--;` do? I take it, that's why the script has to run the rows from top down and fails if you try bottom up (from lastRow)?

CBMServices Web

unread,
Dec 6, 2020, 6:09:09 PM12/6/20
to google-apps-sc...@googlegroups.com
The offset variable is used to provide difference in row number from spreadsheet row and the row in the array of data that you pulled using getValues().

Everytime you delete a row in the sheet, the offset is decrementd to keep the alignment for the rest of the rows to be checked. This makes sure you do not delete the wrong rows.


Laurie Nason

unread,
Dec 7, 2020, 6:55:35 AM12/7/20
to google-apps-sc...@googlegroups.com
Oh - yes.. I forgot about the fact that you delete rows everything moves..
I remember back in the VBA days of deleting rows in databases, one trick was to start at the end of the list and remove them that way - then nothing shifted up.
Laurie

Reply all
Reply to author
Forward
0 new messages