Search code examples
google-apps-scriptgoogle-sheetshighlight

Active Cell's Row & Column Highlighting in Google Sheets


Highlight Active Row & Column

I am looking for a way to highlight the entire row and column in a google sheet depending on the active cell.

What is the best possible way to accomplish this task.


Solution

  • From your showing sample demonstration, I remembered this post (Author: me). In this case, I think that your goal can be achieved using onSelectionChange. The sample script is as follows.

    Sample script:

    Please copy and paste the following script to the script editor of Google Spreadsheet. And, save the script. And, just in case, please reopen the Spreadsheet.

    function onSelectionChange(e) {
      const range = e.range;
      const sheet = range.getSheet();
      const maxRows = sheet.getMaxRows();
      const maxColumns = sheet.getMaxColumns();
      sheet.getRange(1, 1, maxRows, maxColumns).setBackground(null);
      sheet.getRange(range.getRow(), 1, 1, maxColumns).setBackground("#e6b8af");
      sheet.getRange(1, range.getColumn(), maxRows, 1).setBackground("#c9daf8");
    }
    

    Testing:

    When the above script is used, the following situation is obtained.

    enter image description here

    Reference: