Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:37:27+00:00 2026-06-17T10:37:27+00:00

I have a spreadsheet with action items that have dates and priorities attached to

  • 0

I have a spreadsheet with action items that have dates and priorities attached to them. I want to create a function that archives a row if the item is a Priority 2 and a week old or more.

This is my script, but it’s acting out — sometimes it moves the wrong priorities, sometimes it moves the wrong dates, and it never moves all the dates that it should. Worst, it adds tons of empty rows at the bottom of my “Action Items” sheet, and creates empty rows in the “Archive” sheet as well.

Obviously something is wrong, but I really don’t see it, even after a day of intense debugging. Any help would be enlightening and appreciated!

function Archiver() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Action Items'); // get the sheet
  var targetsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Archive'); // get the target sheet
  var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1); // get all the rows with dates
  var fValues = columnF.getValues(); // get the values of dates
  var columnE = sheet.getRange(2, 5, sheet.getLastRow()-1, 1); // get all the rows with priorities
  var eValues = columnE.getValues(); // get the values of priorities
  var day = 24*3600*1000 // calculate ms in a day
  var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
  for (var i = 0; i < fValues.length; i++) { // repeat loop
    var priority = eValues[i][0]; // set priority in loop
    var dataday = parseInt(fValues[i][0].getTime()/day); // convert date column into miliseconds
    Logger.log(dataday+" <= " + today-7 + " - " + priority) // my log isn't picking up day
    if (dataday <= today-7 && priority == "P2") { // if an item is more than 7 days old and Priority 2...
      targetsheet.insertRows(2,1)
      // move the entire source row to the second row of target sheet
    var rangeToMove = sheet.getRange(/*startRow*/ i + 2, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ sheet.getMaxColumns());
    rangeToMove.moveTo(targetsheet.getRange("A2"));
          // add date and time of when approved to target row in column E
    targetsheet.getRange("M2").setValue(Date());
    // delete row from source sheet
    sheet.deleteRows(i + 2,1);
    }
  }
        ss.toast("Move along.", "Archiving Complete.");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:37:29+00:00Added an answer on June 17, 2026 at 10:37 am

    I didn’t debug your code, but my initial thought is that deleting rows while iterating disturbs the order of the data and creates confusion, so I adjusted it to copy the rows (instead of moving) and store the row numbers to be deleted in an array so we can delete them outside the loop. I did a quick test and this seems to work.

    function Archiver() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Action Items'); // get the sheet
      var targetsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Archive'); // get the target sheet
      var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1); // get all the rows with dates
      var fValues = columnF.getValues(); // get the values of dates
      var columnE = sheet.getRange(2, 5, sheet.getLastRow()-1, 1); // get all the rows with priorities
      var eValues = columnE.getValues(); // get the values of priorities
      var day = 24*3600*1000 // calculate ms in a day
      var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
      var rowsToDelete = [];
      for (var i = 0; i < fValues.length; i++) { // repeat loop
        var priority = eValues[i][0]; // set priority in loop
        var dataday = parseInt(fValues[i][0].getTime()/day); // convert date column into miliseconds
        Logger.log(dataday+" <= " + (today-7) + " - " + priority) // my log isn't picking up day
        if (dataday <= today-7 && priority == "P2") { // if an item is more than 7 days old and Priority 2...
          targetsheet.insertRows(2,1)
          // move the entire source row to the second row of target sheet
        var rangeToMove = sheet.getRange(/*startRow*/ i + 2, /*startColumn*/ 1, /*numRows*/ 1, /*numColumns*/ sheet.getMaxColumns());
        rangeToMove.copyTo(targetsheet.getRange("A2"));
              // add date and time of when approved to target row in column E
        targetsheet.getRange("M2").setValue(Date());
        // delete row from source sheet
        rowsToDelete.push(i+2);
        }
      }
      rowsToDelete.reverse();
      for (var j = 0; j < rowsToDelete.length; j++) { sheet.deleteRow(rowsToDelete[j]); }
      ss.toast("Move along.", "Archiving Complete.");
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a spreadsheet that I want to conditionally format according to dates. If
I have a spreadsheet that I am sorting based on an item number column.
I have a spreadsheet that has information in groups. The header row contain company
Like many, I have spreadsheet that draws data from over 40 text files as
I have a spreadsheet of data I am parsing using openpyxl's iter_rows function. However
I have a spreadsheet I'm using to compile text that changes all the time.
I have an Excel spreadsheet that I have exported from some other program. It
I have an Excel spreadsheet that uses a TFS query to pull information on
I have an Excel spreadsheet that looks like: spreadsheet http://img186.imageshack.us/img186/6495/exelf.jpg I'd like to convert
I have one Spreadsheet file in my Google Drive which i want to sync

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.