Google Apps Script Works - Script code to Copy Filtered/Selected Data from one google sheet to another

 Hi Everybody,

This is my first ever Blog. I am mostly Interested in programming, and new to Google Apps Script. How ever, I googled a lot to get the following code worked, thanks to all community and google website for giving me the information required

Please post comments if the code found useful to you  

//IN THIS CODE , MASTER SHEET CONTAINS ALL THE APLICATIONS RECEIVED, AND IN WHICH ONE COLUMN SPECIFIES THE LOCALITY/BRANCH TO WHICH APPLICATION RECEIVED 

//TEMP SHEET CONTAINS THE LIST OF ALL VALID LOCALITY/BRANCH 

//THIS SPREAD SHEET CONTAINS SEPERATE SHEETS FOR EACH LOCALITY/BRANCH

//THIS SCRIPT COPIES THE RECORDS FROM MOUSE-SELECTED AREA OF MASTER SHEET TO RESPECTIVE LOCALITY/BRANCH SHEETS

function CopySelectedData()
{
  //Step to avoid accident data copy
  var Result=SpreadsheetApp.getUi().prompt("Please input the Key");
  var addKey=Result.getResponseText();
  if(addKey!=678)
{
  return;
}
//Get Data from Selected area
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var mainSheet=ss.getSheetByName("MasterSheet");
  var AplnRange=mainSheet.getActiveRange(); 
  var AplnList=AplnRange.getValues();


//Reads the list of valid localities/Branches
  var BlockRange =ss.getSheetByName("Temp").getRange("B2:B28")
  var blockList=BlockRange.getValues();/
 var rangeok =false;
for(let k=0;k<blockList.length;k++)
{
  if(blockList[k][0] == AplnList[0][13])//Compare the Locality/Branch in the Received Application with actual list
    {
    rangeok=true;
    break;
    }
}
if(!rangeok)
{
  SpreadsheetApp.getUi().alert("Please Select full Row");
  return;
}

var columncount=AplnRange.getNumColumns();
  if(columncount!=14)
  {
  SpreadsheetApp.getUi().alert("Please Select 14 Columns");
 //throw "Please remove filter and Try";
  return;
  }
  
  //Copy Data to respective sheets
  var lastRow;
  var targetRange;
  for(let i=0;i<AplnList.length;i++)
  {
    //Logger.log(AplnList[i][13]);
    var allSheets =ss.getSheetByName(String(AplnList[i][13]));
    lastRow=allSheets.getLastRow();
    targetRange=allSheets.getRange(lastRow+1,1,1,14);
    targetRange.setValues([AplnList[i]]);
  }

SpreadsheetApp.flush()
}

Comments

  1. If Anybody has doubt in this code, Please comment, I will try my best to clear

    ReplyDelete

Post a Comment