I’m trying to find a way to move between cells like A10 and E600 in Google Sheets. I’ve tried using a script with a rectangle drawing but it’s not reliable. Is there a better method that doesn’t need hard-coded values?
Here’s a basic example of what I’m aiming for:
function moveBetweenCells(start, end) {
const sheet = SpreadsheetApp.getActiveSheet();
const cellPairs = {
'B5': 'F300',
'B6': 'F275',
'B7': 'F250',
// more pairs...
};
if (cellPairs[start] === end) {
sheet.getRange(end).activate();
} else {
console.log('No matching pair found');
}
}
function goToF300() {
moveBetweenCells('B5', 'F300');
}
function returnToB5() {
moveBetweenCells('F300', 'B5');
}
This works okay but feels clunky. There must be a smoother way to do this. Any ideas for a more efficient solution?
hey, have u tried using data validation? it’s pretty neat for this kinda thing. u can set up dropdown menus in cells with ur destination ranges. then use a script to grab the selected value and jump to it. something like:
function goToSelected() {
var cell = SpreadsheetApp.getActiveRange();
var destination = cell.getValue();
SpreadsheetApp.getActiveSheet().getRange(destination).activate();
}
This way u don’t need to hardcode anything. just update the dropdown options when needed
Have you considered using Google Sheets’ built-in HYPERLINK function? It’s a game-changer for dynamic cell navigation. You can create clickable links within cells that jump to specific locations in your spreadsheet. Here’s a basic formula:
=HYPERLINK(“#gid=0&range=F300”, “Go to F300”)
Place this in cell B5, and it creates a clickable link. You can then use Apps Script to programmatically click these links:
function clickHyperlink(cell) {
var range = SpreadsheetApp.getActiveSheet().getRange(cell);
var value = range.getValue();
if (value.indexOf(‘=HYPERLINK’) === 0) {
range.activate();
SpreadsheetApp.getUi().alert(‘Click the link in the cell’);
}
}
This approach is more user-friendly and doesn’t require maintaining a separate list of cell pairs.
I’ve faced similar challenges with cell navigation in Google Sheets. In my experience, leveraging named ranges instead of hard-coded cell references makes the implementation clearer and more adaptable. Once you define named ranges for the start and end cells, you can reference them directly in your script, allowing changes in the sheet to be managed without modifying the code. For example, the approach below has worked well:
function moveBetweenCells(startRange, endRange) {
const sheet = SpreadsheetApp.getActiveSpreadsheet();
const startCell = sheet.getRangeByName(startRange);
const endCell = sheet.getRangeByName(endRange);
if (startCell && endCell) {
endCell.activate();
} else {
console.log('Named range not found');
}
}
This solution has proven to be both flexible and easier to maintain over time.