P
P
PiShim2021-03-09 12:16:37
Google Sheets
PiShim, 2021-03-09 12:16:37

How to make a script to select the entire line GOOGLE SHEETS?

At first I wrote a script for color formatting a row in a table, but I use a script (see below). All other formatting flies. Therefore, the idea came up to create a script (by clicking on any cell in the table, the key combination Shift + Space is automatically used ). But I could not write such an event in any way.

function onSelectionChange() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let ws = ss.getActiveSheet();
  let activeCell = ws.getActiveCell().getRow();
  ws.getRange(2, 1, 1000, ws.getLastColumn()).clearFormat();
  ws.getRange(activeCell, 1, 1, ws.getLastColumn()).setBackground("yellow")
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2021-03-09
@ProgrammerForever

To avoid formatting, do not use

ws.getRange(2, 1, 1000, ws.getLastColumn()).clearFormat();

To bind a script to a keyboard shortcut - write a macro and assign it. There are combinations like Ctrl + Alt + Shift + N
Or you can tweak appsscript.json
//...
  "sheets": {
    "macros": [{
      "menuName": "macros1",
      "functionName": "myFunction",
      "defaultShortcut": "Ctrl+Shift+Alt+1"
    }]
  }
  //...

It is also possible that the code simply does not execute (an error), try this:
function onSelectionChange() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let as = ss.getActiveSheet();
  let ac = as.getActiveCell();
  let row = ac.getRow();

  as.getRange(row+":"+row).setBackground("yellow");
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question