Skip to main content
4 of 6
edited tags
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174

Cleaning up Google Spreadsheet archiving script

I've just created a script in Google Spreadsheet in order to archive some things. I have data from four countries. If something is marked done, then that line could be archived into that country's archive.

My code is pretty ugly. Is there a clearer way to do this? It would also be great if someone could give me a little help with something else. I want to do the archiving daily and add to the countries archive. But after a month, I want to start another sheet for that month's daily backups. How can I do that?

function CroatianArchive() 
{  
  var source_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('original');
  var target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('croatian_backup');
  var lastRow = source_sheet.getLastRow();
  var source_range = source_sheet.getDataRange();
  var target_range = target_sheet.getDataRange();  
  var rowsToBeDeleted = [];
  
  var i = 2;
  while (i <= lastRow) {
  if (source_sheet.getRange("A"+i).getValue() == "CRO" && source_sheet.getRange("M"+i).getValue() == "DONE" ) {
    var office = source_sheet.getRange("A"+i).getValue();
    var title = source_sheet.getRange("B"+i).getValue();
    var imdbId = source_sheet.getRange("C"+i).getValue();
    var channel = source_sheet.getRange("D"+i).getValue();
    var type = source_sheet.getRange("E"+i).getValue();
    var added = source_sheet.getRange("F"+i).getValue();
    var deadline = source_sheet.getRange("G"+i).getValue();
    var airing = source_sheet.getRange("H"+i).getValue();
    var link = source_sheet.getRange("I"+i).getValue();
    var picture = source_sheet.getRange("J"+i).getValue();
    var comment = source_sheet.getRange("K"+i).getValue();
    var portId = source_sheet.getRange("L"+i).getValue();
    var status = source_sheet.getRange("M"+i).getValue();
    var data = [office,title,imdbId,channel,type,added,deadline,airing,link,picture,comment,portId,status];
    target_sheet.appendRow(data);    
    rowsToBeDeleted.push(i);
    i++;
    } else {
      i++;
      }
    }  
    rowsToBeDeleted.reverse();
    for (var j = 0; j < rowsToBeDeleted.length; j++) 
    {
        source_sheet.deleteRow(rowsToBeDeleted[j]);
    }   
    SpreadsheetApp.getActiveSpreadsheet().toast('Archivation finished.', 'Status');
  }
function SerbianArchive() 
{  
  var source_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('original');
  var target_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('serbian_backup');
  var lastRow = source_sheet.getLastRow();
  var source_range = source_sheet.getDataRange();
  var target_range = target_sheet.getDataRange();  
  var rowsToBeDeleted = [];
  
  var i = 2;
  while (i <= lastRow) {
  if (source_sheet.getRange("A"+i).getValue() == "SER" && source_sheet.getRange("M"+i).getValue() == "DONE" ) {
    var office = source_sheet.getRange("A"+i).getValue();
    var title = source_sheet.getRange("B"+i).getValue();
    var imdbId = source_sheet.getRange("C"+i).getValue();
    var channel = source_sheet.getRange("D"+i).getValue();
    var type = source_sheet.getRange("E"+i).getValue();
    var added = source_sheet.getRange("F"+i).getValue();
    var deadline = source_sheet.getRange("G"+i).getValue();
    var airing = source_sheet.getRange("H"+i).getValue();
    var link = source_sheet.getRange("I"+i).getValue();
    var picture = source_sheet.getRange("J"+i).getValue();
    var comment = source_sheet.getRange("K"+i).getValue();
    var portId = source_sheet.getRange("L"+i).getValue();
    var status = source_sheet.getRange("M"+i).getValue();
    var data = [office,title,imdbId,channel,type,added,deadline,airing,link,picture,comment,portId,status];
    target_sheet.appendRow(data);    
    rowsToBeDeleted.push(i);
    i++;
    } else {
      i++;
      }
    }  
    rowsToBeDeleted.reverse();
    for (var j = 0; j < rowsToBeDeleted.length; j++) 
    {
        source_sheet.deleteRow(rowsToBeDeleted[j]);
    }   
    SpreadsheetApp.getActiveSpreadsheet().toast('Archivation finished.', 'Status');
  }