Google Script: Conditionally copy rows from one sheet to another in the same spreadsheet

心不动则不痛 提交于 2019-11-30 09:56:11

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 :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!