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

  • SEARCH
  • Home
  • 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 8785591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:24:20+00:00 2026-06-13T21:24:20+00:00

I have a spreadsheet with lots of data in one worksheet. In order to

  • 0

I have a spreadsheet with lots of data in one worksheet. In order to reduce clutter, I have written a function that will look for rows with dates older than the current date, copy them to a worksheet name Backup and delete the entry from the current worksheet.

My fxn is shown below. When I call it, it copies about half of the data. I have to run it several times, before it empties my worksheet. And when it does it does not write the last value into the target worksheet.

I would appreciate any pointers to improve my function.

function movePros() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var TZ = CalendarApp.openByName(ss.getSheetByName("Templates").getRange("E1").getValue()).getTimeZone();
var date = Utilities.formatDate(new Date(), TZ, "MM/dd/yyyy HH:mm:ss");
var dataSheet = ss.getSheetByName("Put your events here");
var targetSheet = ss.getSheetByName("Archive");
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows(), dataSheet.getMaxColumns());
var count = 0;

// Create one JavaScript object per row of data.
objects = getRowsData(dataSheet, dataRange);
var res = objects.length;

for (var i = 0; i < objects.length; ++i) {
 var rowData = objects[i];
 var tmp = Utilities.formatDate(rowData.endDate, TZ, "MM/dd/yyyy HH:mm:ss");

 var last_row = targetSheet.getLastRow();
 targetSheet.insertRowAfter(last_row);
 var target_range = targetSheet.getRange("A"+(last_row+1)+":H"+(last_row+1));

 //Browser.msgBox("The length is " + rowData.eventId.length );

 if ((tmp.valueOf() < date.valueOf() ) && (tmp.valueOf().length > 3)){       
      //Browser.msgBox(rowData.eventId);
      var source_range = dataSheet.getRange("A"+(i+2)+":H"+(i+2));
      source_range.copyTo(target_range);
      count++;
      dataSheet.deleteRow(i+2);
      SpreadsheetApp.flush();
 }     
 }  
 ss.toast(count + " rows copied", "Feedback", 5);
 }
  • 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-13T21:24:21+00:00Added an answer on June 13, 2026 at 9:24 pm
    function movePros() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var TZ = CalendarApp.openByName(ss.getSheetByName("Templates").getRange("E1").getValue()).getTimeZone();
      var date = Utilities.formatDate(new Date(), TZ, "MM/dd/yyyy HH:mm:ss");
      var dataSheet = ss.getSheetByName("Put your events here");
      var targetSheet = ss.getSheetByName("Archive");
      var count = 0;
      var orig_last_row = targetSheet.getLastRow(); //we use this as our reference
    
      //dataSheet.getRange(row, column, numRows, numColumns)
      var header_data = dataSheet.getRange(1, 1, 1, dataSheet.getMaxColumns()).getValues();
      var data = dataSheet.getRange(2, 1, dataSheet.getMaxRows(), dataSheet.getMaxColumns()).getValues();
      var srcArray = new Array();
      var dstArray = new Array();
      var headerArray = new Array();
    
      //get the header into our array, this line contains our column headers
      var buff = header_data[0];
      headerArray.push(buff);
    
      for (i in data){
        var row = data[i];
    
        try {
          var tmp = Utilities.formatDate(row[5], TZ, "MM/dd/yyyy HH:mm:ss");
    
          if(tmp.valueOf() < date.valueOf()){
            srcArray.push(row);
            count++;
          }else{
            dstArray.push(row);
          }
    
        } 
        catch(err){
          //Logger.log("reached the end of the array");
        }
    
      }
    
      dataSheet.clearContents();
    
      if(srcArray.length > 0){
          targetSheet.getRange(orig_last_row+1, 1, srcArray.length, srcArray[0].length).setValues(srcArray);
      }  
    
      if(dstArray.length > 0){
        dataSheet.getRange(1, 1, headerArray.length, headerArray[0].length).setValues(headerArray);
        dataSheet.getRange(2, 1, dstArray.length, dstArray[0].length).setValues(dstArray);
      }
    
    
      ss.toast(count + " rows copied", "Feedback", 5);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a spreadsheet full of data with dates that look like this: Mon
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 an excel spreadsheet that is about 300,000 rows and about 100 columns
I have one spreadsheet of google docs with data like : 0012 0234 0065
I have a spreadsheet that has a list of video filenames in one column.
I have a spreadsheet that is using the WORKDAY() function in Excel (which I
Suppose you have inherited a massive spreadsheet which contains lots and lots of data.
I have one spreadsheet already saved with some data, then in a new spreadsheet
I have a spreadsheet that I am sorting based on an item number column.

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.