Skip to content

Instantly share code, notes, and snippets.

@mogsdad
Created April 18, 2013 20:09
Show Gist options
  • Save mogsdad/5415837 to your computer and use it in GitHub Desktop.
Save mogsdad/5415837 to your computer and use it in GitHub Desktop.
From StackOverflow answer http://stackoverflow.com/a/16090621/1677912.

moveRange

Google Apps-Script spreadsheet function.

Move all values from source range to destination range. Upon completion, source range will be cleared. Source values will be moved into a destination range starting at the "top left" of the destination range, using the dimensions of the source range. This is a blind copy, with no overwrite test.

Parameters:

<tbody>
    <tr>
            <td class="name"><code>source</code></td>
        <td class="type">
                    Range   
        </td>
        <td class="description last">Range Object to take values from.</td>
    </tr>
    <tr>
            <td class="name"><code>destination</code></td>
        <td class="type">
                    Range   
        </td>
        <td class="description last">Range Object to receive values.</td>
    </tr>
</tbody>
Name Type Description

Returns: null

/**
* Move all values from source range to destination range. Upon
* completion, source range will be cleared. Source values will
* be moved into a destination range starting at the "top left"
* of the destination range, using the dimensions of the source
* range. This is a blind copy, with no overwrite test.
*
* @param {Range} source Range Object to take values from.
* @param {Range} destination Range Object to receive values.
*
* @returns {null}
*/
function moveRange(source,destination) {
var sourceSheet = source.getSheet();
var destSheet = destination.getSheet();
var sourceData = source.getValues();
var dest = destSheet.getRange(
destination.getRow(),
destination.getColumn(),
sourceData.length,
sourceData[0].length);
dest.setValues(sourceData);
source.clear();
}
// Test function
function test_moveRange() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var destSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
var source = sourceSheet.getRange("A7:C10");
var destination = destSheet.getRange("C4:H2");
moveRange(source,destination);
}
@azmeco
Copy link

azmeco commented May 9, 2017

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment