Monday, August 12, 2013

Google Spreadsheet Script to cut paste data from one range to another

Question:

( by Aidoo )


Hello,
how to copy a row to another row at the same sheet?
Sample:
1. Input Data to B5:E5
2. <NEED THE SCRIPT>
3. Script must copy data from B5:E5 to B13:E13
4. Script must delete data from B5:E5
5. New Input Data to B5:E5
6. Start the Script again...
7. Script must copy data from B5:E5 to B14:E14!
8. Script must delete data from B5:E5

Example Sheet:

Thanks for help!

Solution:

Have a look at the following animated screenshot:


Have a look at the following code:

///////////////////////////////////////

// This script has been developed by Kishan.
// For more visit iGoogleDrive.blogspot.com

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Move 'B5:E5'", functionName: "copyAndDelete"} ];
  ss.addMenu("Move Values",menuEntries);// custom menu
};

function copyAndDelete() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Test");
  var copyRange = sheet.getRange("B5:E5");
  var l = sheet.getDataRange().getValues().length;
  var pasteRange = sheet.getRange( "B"+(l+1)+":E"+(l+1) );
  pasteRange.setValues(copyRange.getValues());
  copyRange.clear();

};

///////////////////////////////////////

put the above code in your script editor, and then you can use it directly in your Google Spreadsheet.


Click on the Menu "Move Values" (which is created by this script) and then click on "Move B5:E5". Then script will move the values to below the last row of the sheet "Test".


And If you are not much familiar with scripts then check out the following link:
http://igoogledrive.blogspot.com/2012/08/how-to-write-script-in-google.html 

I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.


I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,

3 comments: