Google script equivalent for Excel script to last column in row offset by one

1.6k views Asked by At

I am coping data from one sheet to another (In google sheets), but I want it to be offset every time I copy so it can create a data chart over time. All I want the script to do is find the last column in a row with data and offset by one so it copies the data from left to right every time the copy button is hit.

I have found a script in excel that does this, but I need the google script equivalent.

Selection.End(xlToRight).Select
    ActiveCell.Offset(0, 1).Select

I have searched all over the internet to find something like this but have been unsuccessful. All I can find is column to row (up to down), and I need row to column (left to right), with an offset by one. Please help!

1

There are 1 answers

2
Александр Ермолин On

I think, the following code is the equivalent:

SpreadsheetApp.getSelection().getCurrentCell()
  .getNextDataCell(SpreadsheetApp.Direction.NEXT)
  .offset(0, 1).activate();

If you want to find the next to right cell after the last non-empty cell in the same row, then try this code:

var sheet = SpreadsheetApp.getActiveSheet();
var row = SpreadsheetApp.getSelection().getCurrentCell().getRow();
var column = sheet.getMaxColumns();
sheet.getRange(row, column).getNextDataCell(SpreadsheetApp.Direction.PREVIOUS).offset(0, 1).activate();