To achieve your goal of inserting a new row at line 22 in the current sheet and copying the values from cells L14 and M14 into the newly inserted row's L and M columns, you can use Google Apps Script. Below are step-by-step instructions along with the necessary script to accomplish this task.
Step 1: Open Google Apps Script Editor
-
Open your Google Sheet where you want to implement this functionality.
-
Click on the menu bar: Extensions > Apps Script.
[image: Open Apps Script] open
Step 2: Write the Script
Once you're in the Apps Script editor, follow these steps:
- Delete any existing code in the editor to ensure a clean script.
- Copy and paste the following script:
function insertRowAndCopy() {
// Get the active spreadsheet and sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Define the row number where you want to insert the new row
var insertRow = 22;
// Insert a new row before row 22
sheet.insertRowBefore(insertRow);
// Define the source cells (L14 and M14)
var sourceRange = sheet.getRange("L14:M14");
// Define the destination range in the newly inserted row (L22 and M22)
var destinationRange = sheet.getRange(insertRow, 12, 1, 2); // Column 12 = L, 13 = M
// Copy the content and formatting from source to destination
sourceRange.copyTo(destinationRange, {contentsOnly: false});
// Optionally, you can set specific values instead of copying everything:
/*
var sourceValues = sourceRange.getValues();
sheet.getRange(insertRow, 12).setValue(sourceValues[0][0]); // L14 to L22
sheet.getRange(insertRow, 13).setValue(sourceValues[0][1]); // M14 to M22
*/
// Optional: Provide a confirmation message in the spreadsheet
SpreadsheetApp.getUi().alert('New row inserted at line ' + insertRow + ' and L14:M14 copied to the new row.');
}
Script Explanation:
-
Access the Active Sheet: The script starts by accessing the currently active sheet in your Google Sheets document.
-
Insert New Row: It inserts a new row before row 22. This means the new row becomes the new row 22, and the existing row 22 shifts down to row 23.
-
Copy Cells L14 and M14: The script copies both the content and formatting from cells L14 and M14 to the newly inserted row's L22 and M22.
-
Optional Confirmation: After the operation, a confirmation dialog box appears to inform you that the row has been inserted and the cells have been copied.
Step 3: Save the Script
- Click the Save icon (💾) or press Ctrl + S (Windows) / Cmd + S (Mac).
- Name your project, for example,
InsertRowAndCopy.
Step 4: Add a Custom Menu to Run the Script
To make it easy to run this script from your Google Sheet, you can add a custom menu.
- Add the following function below the
insertRowAndCopy function in your script:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Scripts')
.addItem('Insert Row at 22 and Copy L14:M14', 'insertRowAndCopy')
.addToUi();
}
- Save the script again.
Explanation:
- The
onOpen function runs automatically every time you open the spreadsheet.
- It adds a new menu item called "Custom Scripts" with an option to "Insert Row at 22 and Copy L14:M14".
- Selecting this menu item will execute the
insertRowAndCopy function.
Step 5: Authorize the Script
-
Run the onOpen Function: Click the dropdown menu (it might say onOpen by default) and select onOpen, then click the Run button (▶️).
-
Authorize the Script: You'll be prompted to authorize the script to access your Google Sheets. Follow the authorization steps:
- Click Review Permissions.
- Choose your Google account.
- Click Allow to grant the necessary permissions.
[image: Authorization] open
Step 6: Use the Custom Menu
-
Reload your Google Sheet to initialize the custom menu.
-
After reloading, you'll see a new menu item called "Custom Scripts" in the menu bar.
[image: Custom Menu] open
-
Click on "Custom Scripts" > "Insert Row at 22 and Copy L14:M14".
[image: Run Script] open
-
A confirmation dialog should appear confirming the operation.
Optional: Assigning the Script to a Button
If you prefer using a button instead of the menu, follow these steps:
-
Insert a Drawing or Image:
-
Go to Insert > Drawing and create a button-like shape, then click Save and Close.
-
Alternatively, you can insert an image to use as a button.
-
Assign Script to the Drawing/Image:
- Click on the inserted drawing or image.
- Click the three vertical dots that appear and select "Assign script".
- Enter
insertRowAndCopy as the script to assign, then click OK.
[image: Assign Script] open
-
Use the Button:
- Now, clicking the button will execute the
insertRowAndCopy function.
Notes
-
Customization: If you want to copy only the values without formatting, you can modify the copyTo method options or use the commented-out section in the script to set values manually.
-
Error Handling: For production use, consider adding error handling to manage scenarios like when row 22 is already occupied or if cells L14 and M14 are empty.
-
Permissions: Ensure that you have the necessary permissions to run scripts in the Google Sheet, especially if it's shared with others.
Complete Script for Reference
function insertRowAndCopy() {
// Get the active spreadsheet and sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Define the row number where you want to insert the new row
var insertRow = 22;
// Insert a new row before row 22
sheet.insertRowBefore(insertRow);
// Define the source cells (L14 and M14)
var sourceRange = sheet.getRange("L14:M14");
// Define the destination range in the newly inserted row (L22 and M22)
var destinationRange = sheet.getRange(insertRow, 12, 1, 2); // Column 12 = L, 13 = M
// Copy the content and formatting from source to destination
sourceRange.copyTo(destinationRange, {contentsOnly: false});
// Optional: Provide a confirmation message in the spreadsheet
SpreadsheetApp.getUi().alert('New row inserted at line ' + insertRow + ' and L14:M14 copied to the new row.');
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Scripts')
.addItem('Insert Row at 22 and Copy L14:M14', 'insertRowAndCopy')
.addToUi();
}
Feel free to customize the script further based on your specific needs. If you encounter any issues or have additional questions, don't hesitate to ask!