google-sheetsgoogle-apps-script

How to access the cell next to the selected cell in Google sheet using script?


I just started exploring google sheets and JavaScript and I wrote a function which prompts the user to type a string which is entered into the selected cell. I'm accessing the current cell using SpreadsheetApp.getActiveSheet().getActiveCell() and setting the value using .setValue().

Now I made another prompt and I want to put this value in the cell next to the selected cell (same row but column+1). How do I access the current cells row and column positions and then increment the col with 1?

In pseudo code I'm thinking something like this:

ActiveCell[row][column+1] = string input from prompt2

Solution

  • Try using offset to specify offset:

    let cell = SpreadsheetApp.getActiveSheet().getActiveCell();
    
    // update cell to the right
    cell.offset(0, 1).setValue("upd");