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]);
}
}
}
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
Mobile: +966-54-564-3698
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/99c1370c-1ab5-47b6-9ea4-8bd412a44240n%40googlegroups.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--;
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAKTLZ53HVxcDjpKUd2ZSSWgk7dVYtZb%3DHz7A_N8TnHsZB2KH7g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/606a2f9e-f799-4198-ac57-6b7804b45b09n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAFX%2Bz3W7d25PkgA7gF1FbTUA8B3MAUcsTuQrX12TijF7PfTXLw%40mail.gmail.com.