6
votes

I read Google Script: Conditionally copy rows from one spreadsheet to another and could not make it work for me.

I need a script that will allow me to do the quoted text below. Please feel free to edit my spreadsheet to make a complete working script. I will keep the spreadsheet publicly available for anyone to copy the script for their own use. I really know nothing about script writing so I need it all spelled out. Sorry, I'm such a noob with all this, but this will help me learn.

https://docs.google.com/spreadsheet/ccc?key=0AoJdwy8V1ldHdFA5M1M1Wlp1NHZhcmxJOUZKVEU4X3c&usp=sharing

If Column B on sheet "All_Mileage" says "John" then I want that row to be copy and pasted into Sheet "John" starting on row 3 and following.

If Column B on sheet "All_Mileage" says "Adam" then I want that row to be copy and pasted into Sheet "Adam" starting on row 3 and following.

If Column B on sheet "All_Mileage" says "Mike" then I want that row to be copy and pasted into Sheet "Mike" starting on row 3 and following.

I saw other scripts on here, but I couldn't get them to work. Like I said I'm greener than a sapling when it come to code.

Thanks a ton!

1
Welcome, I'm not sure you'll have much success asking for a written script. It's customary to show what you have tried and ask a question regarding a specific issue you're having.ChiefTwoPencils

1 Answers

12
votes

C.Lang is right, this is not a place to get readymade scripts ... but since this question is so common and has been aswered so often it took me a few minutes to write and test... so there it is :

var ss=SpreadsheetApp.getActiveSpreadsheet();// some global variables
var master = ss.getSheetByName('All_Mileage');
var colWidth = master.getMaxColumns();


    function copyRowsOnCondition() {
      var data = master.getDataRange().getValues();
      for(n=2;n<data.length;++n){
        if(data[n][1].length<16){ // if not pre-filled with your text
        Logger.log(data[n][1])
         var dest = ss.getSheetByName(data[n][1].toString().replace(/ /g,''));//remove any spaces that could be included in the name so the name = sheetName for sure
         var destRange = dest.getRange(dest.getLastRow()+1,1);// the place to write
         master.getRange(n+1,1,1,colWidth).copyTo(destRange);the copy itself value & format
         }
      }// loop
    }

EDIT : since I used the name value in MasterSheet to find destination sheet I thought it might be usefull to handle the case where the destination sheet doen't exist by creating it using the same rule, ie. name = sheetName...

The other issue was that there was no way to know which rows had been already copied... so I made a version that handles all that, copying only the rows that are manually selected (even in only a single column) and change the background color to tell that these rows have been processed. I also added a menu for a minimal comfort ;-)

(how to keep busy on a cold sunday afternoon ;-)

var ss=SpreadsheetApp.getActiveSpreadsheet();
var master = ss.getSheetByName('All_Mileage');
var colWidth = master.getLastColumn();// last used col in masterSheet
var sheets = ss.getSheets();// number of sheets

function onOpen() {
  var menuEntries = [ {name: "Copy selected Rows to sheets", functionName: "copyRowsOnConditionV2"},

                     ];
  ss.addMenu("Copy functions",menuEntries);// custom menu
}
function copyRowsOnConditionV2() {
  var sheetNames = [];// array of existing sheet names
  var sheets = ss.getSheets();// number of sheets
  for(s=0;s<sheets.length;++s){sheetNames.push(sheets[s].getName())};
  ss.getActiveSelection().setBackground('#ffffbb'); 
  var selectedfirstRow = ss.getActiveSelection().getRowIndex();
  var selectedHeigth = ss.getActiveSelection().getHeight()
  var selectedFullRange = master.getRange(selectedfirstRow,1,selectedHeigth,colWidth);
  var data = selectedFullRange.getValues();
  for(n=0;n<data.length;++n){
    if(data[n][1].length<16){
     if(sheetNames.toString().match(data[n][1].toString().replace(/ /g,''))!=data[n][1].toString().replace(/ /g,'')){// if no sheet exist with this name
     var newSheet = ss.insertSheet(data[n][1].toString().replace(/ /g,''),ss.getSheets().length);// then create it
     master.getRange(1,1,2,colWidth).copyTo(newSheet.getRange(1,1));// and copy the headers on 2 first rows, then continue as usual
     newSheet.getRange(1,1).setValue('Gas Mileage Log - '+data[n][1].toString().replace(/ /g,''));// set name in header
     SpreadsheetApp.flush();
     var sheets = ss.getSheets();// number of sheets
     var sheetNames = [];// update array of existing sheet names
     for(s=1;s<sheets.length;++s){sheetNames.push(sheets[s].getName())};
     Logger.log(sheetNames)
     };
     var dest = ss.getSheetByName(data[n][1].toString().replace(/ /g,''));//find the destination sheet
     Logger.log(data[n][1].toString().replace(/ /g,''))
     var destRange = dest.getRange(dest.getLastRow()+1,1);// define range
     master.getRange(selectedfirstRow+n,1,1,colWidth).copyTo(destRange);// and make copy below last row
     }
  }
}

Illustration below :

enter image description here