Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all selected cells in google script

I am using the following code to get selected cells:

var activeRange = SpreadsheetApp.getActiveRange().getA1Notation();

If I select cells B1, B2, B3 it returns "B1:B3". But if I select B1 and then use the Ctrl key to also select B3 it returns only "B3". I want "B1,B3". I couldn't find a function for this in the documentation.

like image 625
vishesh Avatar asked Dec 18 '14 05:12

vishesh


People also ask

What is === in Google script?

Equality (==): a == b results in true if the value a is equal to value b. Strict equality (===): a === b results in true if the value a is equal to value b and their types are also the same. Inequality (! =): a !=

How do I get a specific cell in an app script?

Get selected cell value in your Google Sheet Script Now we have to add getCurrentCellValue() function. This function will get the value of the currently selected cell and then show it in a message box. In order to make it a little more informative, let us show the cell's A1 notation and then its value.

How do I select all cells in a column in Google Sheets?

Using keyboard shortcuts, To select an entire column press Ctrl + Space . To select an entire row, press Shift + Space .

How do you reference a cell in Google script?

Type = followed by the sheet name, an exclamation mark and the cell being copied. For example, =Sheet1! A1 or ='Sheet number two'! B4 .


2 Answers

  var range = '';
  var sel = SpreadsheetApp.getActive().getSelection().getActiveRangeList().getRanges();
  for(var i = 0; i < sel.length; i++){
    range += sel[i].getA1Notation() + ', ';
  }
  Logger.log(range);

Logs: [18-06-21 22:18:16:866 EEST] B1, B3,

like image 182
ugene Avatar answered Sep 22 '22 13:09

ugene


Apps Script currently does not provide support for handling disjoint ranges within Range objects. What will happen is that the last selected sub-range will be returned by calls of getActiveRange(), etc. It is filed here as issue 4069. You may want to 'star' it.

like image 32
JPV Avatar answered Sep 21 '22 13:09

JPV