Here's a step-by-step guide to creating a script with a button in Google Spreadsheets that copies a row and pastes it into the last blank row in another sheet:
Prerequisites:
- You have a Google Spreadsheet with at least two sheets (e.g., "Sheet1" and "Sheet2").
- You know which row you want to copy (this example will use a variable for the row number, but you can hard-code it if needed).
- You have basic familiarity with the Google Apps Script editor.
Steps:
1. Open Google Apps Script Editor
- Open your Google Spreadsheet.
- Navigate to
Extensions > Apps Script.
2. Create the Script
In the script editor, delete any code in the editor, and paste the following script. This script assumes you want to copy the entire row from "Sheet1" to "Sheet2" based on a variable sourceRow. The button will trigger the copyRow function.
function copyRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheetName = "Sheet1"; // Change if your sheet name is different
var targetSheetName = "Sheet2"; // Change if your sheet name is different
// Dynamically get the row number based on user selection
var sourceSheet = ss.getSheetByName(sourceSheetName);
var targetSheet = ss.getSheetByName(targetSheetName);
if (!sourceSheet || !targetSheet) {
throw new Error("One or both sheets were not found. Please check sheet names.");
}
var selection = ss.getActiveRange();
if (selection.getNumRows() !== 1) {
throw new Error("Please select only one row.");
}
var sourceRow = selection.getRow();
// Get the last row with data in the target sheet
var lastRow = targetSheet.getLastRow() + 1; // +1 to get the first blank row after data
// Check if source row has data
var rangeToCopy = sourceSheet.getRange(sourceRow, 1, 1, sourceSheet.getLastColumn());
if (rangeToCopy.isBlank()) {
throw new Error("The selected row is blank. Please select a row with data.");
}
// Copy the row from the source sheet to the target sheet
rangeToCopy.copyTo(targetSheet.getRange(lastRow, 1));
}
// Optional: Create a custom menu to test your script (not necessary if using a button)
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or `addMenuItem` if you prefer
ui.createMenu('Script Menu')
.addItem('Copy Row', 'copyRow')
.addToUi();
}
3. Draw a Button in Your Spreadsheet
- Go back to your Google Spreadsheet.
- Navigate to
Insert > Drawing.
- Draw a button shape (you can use the rectangle tool and add text on top of it).
- Click
Save and close. Your drawing will be placed on the sheet.
4. Assign the Script to the Button
- Click on the newly created button to select it.
- Click on the
⋮ (three vertical dots) at the top right corner of the button.
- Select
Assign script.
- Enter
copyRow (the name of the function you want to run when the button is clicked).
- Click
OK.
5. Testing
- Select the row you want to copy in "Sheet1".
- Click the button. The selected row from "Sheet1" should now be copied to the first blank row after any existing data in "Sheet2".
Notes:
- Security Prompt: The first time you click the button or run the script from the editor, you'll be prompted to authorize the script. Follow the prompts to complete the authorization.
- Row Number: The script dynamically detects the selected row. Make sure to select the row you wish to copy before clicking the button.
- Column Count: The script automatically detects the last column with data in the source row. If you only want to copy up to a certain column, adjust the
getLastColumn() part in the getRange method.
- Error Handling: This script includes checks for potential errors, such as invalid sheet names, empty rows, or incorrect selections. If an issue occurs, an error message will be displayed.