google-sheetsgoogle-apps-scriptsourceforge-appscriptgoogle-forms-api

Archive rows to another tab greater than 3 years


Requirement is to archive all the rows older than 3 years and insert those rows into a different tab in the same spread sheet.

I have below code to archive rows to another tab where date is older than 3 years . The below code is deleting all the rows before 3 years in the source tab correctly ,but it is not inserting the deleted rows to the target tab (archived rows).please let me know how to fix this code


const ss = SpreadsheetApp.getActive()
const shSource = ss.getSheetByName('Incident Report Details')
const shTarget = ss.getSheetByName('Archived rows')

function myFunction() {
  const COL_DATE = 1
  const HEADER_HEIGHT = 1
  if(shSource.getLastRow()==HEADER_HEIGHT){
    return
  }
  const data = shSource.getRange(1+HEADER_HEIGHT,1,shSource.getLastRow()-HEADER_HEIGHT, shSource.getLastColumn())
  data.sort(COL_DATE) // prepare for deletion of continous rows
  const minDate = new Date()
  minDate.setMonth(minDate.getMonth()-36)

  
  const rowsToDelete = data.getValues().filter(row=>row[COL_DATE-1]<minDate)
  if(rowsToDelete.length>0){
    console.log(`Delete ${rowsToDelete.length} rows where date is before ${minDate}`)
    shTarget.insertRowsAfter(shTarget.getLastRow(), rowsToDelete.length)
    shTarget.getRange(shTarget.getLastRow()+1,1, rowsToDelete.length, rowsToDelete[0].length).setValues(rowsToDelete)
  } else{
    console.log(`No rows to delete`)
  }

  shSource.deleteRows(HEADER_HEIGHT+1, rowsToDelete.length)
}



Solution

  • Updated answer:

    Your code is working fine for me. Just added the deleteRows statement inside IF.

    const ss = SpreadsheetApp.getActive()
    const shSource = ss.getSheetByName('Incident Report Details')
    const shTarget = ss.getSheetByName('Archived rows')
    
    function myFunction() {
      const COL_DATE = 1
      const HEADER_HEIGHT = 1
      if (shSource.getLastRow() == HEADER_HEIGHT) {
        return
      }
      const data = shSource.getRange(1 + HEADER_HEIGHT, 1, shSource.getLastRow() - HEADER_HEIGHT, shSource.getLastColumn())
      shSource.sort(COL_DATE) // prepare for deletion of continous rows
      const minDate = new Date()
      minDate.setMonth(minDate.getMonth() - 36)
    
    
      const rowsToDelete = data.getValues().filter(row => row[COL_DATE - 1] < minDate)
      console.log('rowsToDelete: ' + rowsToDelete.length);
    
      if (rowsToDelete.length > 0) {
        console.log(`Delete ${rowsToDelete.length} rows where date is before ${minDate}`)
        shTarget.insertRowsAfter(shTarget.getLastRow(), rowsToDelete.length)
        shTarget.getRange(shTarget.getLastRow() + 1, 1, rowsToDelete.length, rowsToDelete[0].length).setValues(rowsToDelete)
        shSource.deleteRows(HEADER_HEIGHT + 1, rowsToDelete.length)
      } else {
        console.log(`No rows to delete`)
      }
    }
    

    Earlier answer (Please ignore):

    Add this line before deleting rows

    SpreadsheetApp.flush();
    

    Spreadsheet operations are sometimes bundled together to improve performance. However, sometimes you may want to make sure that all pending changes are made right away as a scriptYour code is executingworking fine for me.