Copy/Move Dialog (Same Spreadsheet)

A Dialog for moving or copying ranges from one sheet to another

It utilizes a radio button group to select sheets and because it's modeless dialog you can also select source and destination ranges by clicking and dragging and then select either copy or move and the operation will complete and the dialog will go away automatically. Here's the code.

Code Notes:

You may paste the code into your Google Script Text Editor. I'll start each code section with the name of the file that you should use and then the code. Just create the gs or html files and name them appropriately and copy and paste the code. Select one of them to run and Google will determine all of the needed scopes and providing you with an authorizations dialog so that you can authorize the script to run on your acccount.


This title of each code section will be something like index.html so you will provide the title as "index" and make sure your creating an html file and the editor will supply the ".html". Note if you type in "index.html" then your filename will be "index.html.html"

This code doesn't require any special libraries or API's.

Code.gs


Be sure to checkout the references at the bottom of the page.

function onOpen(){

SpreadsheetApp.getUi().createMenu('My Menu')

.addItem("Show Dialog", 'showMyDialog')

.addToUi()

}


function setActiveSheet(sObj) {

var ss=SpreadsheetApp.getActive();

ss.setActiveSheet(ss.getSheetByName(sObj.sheetname));

return sObj;

}


function getSelectedRange() {

var ss=SpreadsheetApp.getActive();

var sh=ss.getActiveSheet();

var rg=sh.getActiveRange();

var a1=rg.getA1Notation();

var sht=sh.getName();

return {range:Utilities.formatString('%s!%s', sht,a1)};

}


function getRadioButtons() {

var ss=SpreadsheetApp.getActive();

var sh=ss.getActiveSheet();

var shts=ss.getSheets();

var html='';

for(var i=0;i<shts.length;i++) {

html+=Utilities.formatString('<br /><input type="radio" name="rgroup" id="%s" value="%s" onChange="getSelected();" /> %s',"id-"+ shts[i].getName(),shts[i].getName(),shts[i].getName());

}

return {html:html,id:'id-' + sh.getName()};

}



function showMyDialog(){

var ui=HtmlService.createTemplateFromFile('index').evaluate().setWidth(400);

SpreadsheetApp.getUi().showModelessDialog(ui, 'Copy/Paste');

}


function doGet(e){

return HtmlService.createTemplateFromFile('index').evaluate();

}


function include(filename){

return HtmlService.createHtmlOutputFromFile(filename).getContent();

}


function closeDialog() {

var userInterface=HtmlService.createHtmlOutputFromFile('close');

SpreadsheetApp.getUi().showModelessDialog(userInterface,'Closing');

}


function move(mObj) {

var ss=SpreadsheetApp.getActive();

var srcrg=ss.getRange(mObj.src);

var desrg=ss.getRange(mObj.des);

srcrg.copyTo(desrg);

srcrg.clear();

}


function copy(mObj) {

var ss=SpreadsheetApp.getActive();

var srcrg=ss.getRange(mObj.src);

var desrg=ss.getRange(mObj.des);

srcrg.copyTo(desrg);

}



index.html

<!DOCTYPE html>

<html>

<head>

<base target="_top">

<?!= include('resources') ?>

<?!= include('css') ?>

</head>

<body>

<?!= include('hl') ?>

<?!= include('script') ?>

</body>

</html>

<html><head>

resource.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


css.html

<style>

body {background-color:#ffffff;}

input[type="button"],input[type="text"]{margin:0 0 2px 2px;}

</style>


script.html

<script>

$(function(){

google.script.run

.withSuccessHandler(function(hObj) {

document.getElementById('radiodiv').innerHTML=hObj.html;

var rbtn=document.getElementById(hObj.id);

rbtn.checked=true;

})

.getRadioButtons();

});

function move() {

var src=$('#srctxt').val();

var des=$('#destxt').val();

var rObj={src:src,des:des};

google.script.run

.withSuccessHandler(function(hObj){

google.script.run.closeDialog();

})

.move(rObj);

}

function copy() {

var src=$('#srctxt').val();

var des=$('#destxt').val();

var rObj={src:src,des:des};

google.script.run

.withSuccessHandler(function(hObj){

google.script.run.closeDialog();

})

.copy(rObj);

}

function cancel() {

google.script.run.closeDialog();

}

function selectSRC() {

google.script.run

.withSuccessHandler(function(rObj){

document.getElementById('srctxt').value=rObj.range;

})

.getSelectedRange();

}

function selectDES() {

google.script.run

.withSuccessHandler(function(rObj){

document.getElementById('destxt').value=rObj.range;

})

.getSelectedRange();

}

function getSelected(){

var sObj={sheetname:document.querySelector('input[name="rgroup"]:checked').value};

google.script.run

.withSuccessHandler(function(rObj){

//document.getElementById('msgdiv').innerHTML='Selected Sheet: ' + rObj.sheetname;

})

.setActiveSheet(sObj);

}

console.log('My Code');

</script>


hl.html

<div id="radiodiv"></div>

<br /><input type="button" value="Select Source Range" onClick="selectSRC();" /><input type="text" id="srctxt" />

<br /><input type="button" value="Select Destination Range" onClick="selectDES();" /><input type="text" id="destxt" />

<br /><input type="button" value="Copy" onClick="copy();" />

<br /><input type="button" value="Move" onClick="move();" />

<br /><input type="button" value="Cancel" onClick="cancel();" />

<div id="msgdiv"></div>


close.html

<!DOCTYPE html>

<html>

<head>

<base target="_top">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script>

$(function(){

google.script.host.close();

});

</script>

</head>

<body>

</body>

</html>


Just copy and paste the code into the recommended file names and you should be good to go.

If you need any help with additional questions please feel free to come ask me questions at StackOverFlow.com