To add a menu, and run code I've created the following code and explanation:
Add a Function the runs when the spreadsheet opens:
Choose TOOLS, SCRIPT EDITOR and the code editor will open. Paste in this onOpen()
function.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Menu')
.addItem('Display User Dialog', 'displayUserDialog')
.addToUi();
};
That code will run when you open the spreadsheet. You will see a new Menu Item.
That code is set up to trigger some more code when you choose 'Display User Dialog' from the Custom Menu. You need to add more code. Add this function:
function displayUserDialog() {
//Logger.log('displayUserDialog ran: ');
var html = HtmlService.createTemplateFromFile('Dialog Copy Choices')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.NATIVE)
.setWidth(500)
.setHeight(300);
SpreadsheetApp.getUi()
.showModalDialog(html, 'My custom dialog');
};
The above two functions go into a gs
script file. I think a new script file is usually automatically created name: Code.gs
Create a new HTML file:
From the FILE menu, choose NEW - HTML
HTML File named "Dialog Copy Choices"
<div id="outer" style="padding:1;"/>
<div>
<br>Sheet To Copy To:<br>
<select id="idSheetNames">
<?!= getSheetNames(); ?>
</select>
<br>
<br>
<div>Copy To:</div>
<input id="idToCol" type='text' placeholder='Column To Copy to:'>
<br>
<input id="idToRow" type='text' placeholder='Row To Copy to:'>
<br>
<br>
<div>Copy From:</div>
<input id="idFromStart" type='text' placeholder='A1'>
<br>
<input id="idFromEnd" type='text' placeholder='D20:'>
<br>
<br>
<input type="button" value="Copy Cells" id="idCopyBtn" onclick="myFunction()">
</div>
<script>
function myFunction() {
var sheetToGetData = document.getElementById("idSheetNames").value;
var copyToRow = document.getElementById("idToRow").value;
var copyToCol = document.getElementById("idToCol").value;
var copyFromStart = document.getElementById("idFromStart").value;
var copyFromEnd = document.getElementById("idFromEnd").value;
//console.log("values of variables: " + sheetToGetData + " : " + copyToCol + " : " + copyToRow + " : " + copyFromStart + " : " + copyFromEnd);
google.script.run.gsCopyData(sheetToGetData, copyToCol, copyToRow, copyFromStart, copyFromEnd);
google.script.host.close();
};
</script>
Refresh the spreadsheet after adding all that code, and a new custom menu should appear. Use the custom menu to display the dialog.
The custom dialog displays all the sheets in your spreadsheet in a drop down field. The drop down allows you to choose what sheet to copy the data to:
gs Code to Get Sheet Names
function getSheetNames() {
var allSheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var howManySheets = allSheets.length;
var sheetNames = [];
var htmlSelectOptions = "";
var thisName = "";
for (var i = 0;i < howManySheets; i++) {
thisName = allSheets[i].getName();
htmlSelectOptions += '<option value="' + thisName + '">' + thisName + '</option>';
};
return htmlSelectOptions;
};
gs Code to Copy Data
function gsCopyData(sheetToGetData, copyToCol, copyToRow, copyFromStart, copyFromEnd) {
//Logger.log(sheetToGetData + " : " + copyToCol + " : " + copyToRow + " : " + copyFromStart + " : " + copyFromEnd);
//Logger.log("gsCopyData ran");
var alphaBet = 'abcdefghijklmnopqrstuvwxyz';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rowStartCopy = Number(copyFromStart.slice(1));
var colStartCopy = copyFromStart.slice(0,1);
colStartCopy = 1 + alphaBet.indexOf(colStartCopy.toLowerCase());
var rowEndCopy = Number(copyFromEnd.slice(1));
var colEndCopy = copyFromEnd.slice(0,1);
colEndCopy = 1 + alphaBet.indexOf(colEndCopy.toLowerCase());
var numRows = rowEndCopy - rowStartCopy + 1;
var numColumns = colEndCopy - colStartCopy + 1;
var rangeToCopy = sheet.getRange(rowStartCopy, colStartCopy, numRows, numColumns);
copyToCol = alphaBet.indexOf(copyToCol.toLowerCase()) + 1;
var destinationSheet = ss.getSheetByName(sheetToGetData);
//Logger.log(destinationSheet + copyToCol + numColumns + copyToRow + numRows);
rangeToCopy.copyValuesToRange(destinationSheet, copyToCol, numColumns, copyToRow, numRows);
};