5
votes

I want to know if we can select a row in a Spreadsheet using Google script like this enter image description here

Anyway first I've tried with the columns by using this fonction but I get an error of the data (is not found): If you have an idea :)

  function testGetFullColumn()
{
  getFullColumn('A', 1) ;
}

// This function gets the full column Range like doing 'A1:A9999' in excel
// @param {String} column The column name to get ('A', 'G', etc)
// @param {Number} startIndex The row number to start from (1, 5, 15)
// @return {Range} The "Range" object containing the full column: https://developers.google.com/apps-script/class_range
function getFullColumn(column, startIndex){
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var lastRow = sheet.getLastRow();
  return sheet.getRange(column+startIndex+':'+column+lastRow);
}

Edit :I know about getRange but I would be cool^^^^^^ to see in visually the selection like in the photo I've attached because I tried with

var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; return sheet.getDataRange();

and it doesn't select all the sheet visually I mean I want to see an interactive selection :) like here

Edit 2: I've tried to use setActiveRange after getRange as you suggested thanks Guilherme M but it doesn't select visually all the document :( Your code works it's really cool^ I was afraid that it's impossible to select I will try to do the same thanks ^^

 function testGet()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var range =  sheet.getDataRange();
  SpreadsheetApp.setActiveRange(range)
}

Edit 3: To select all the document I've tried this code:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var lastRow = sheet.getLastRow();
 var lastColumn = sheet.getLastColumn();

  var range = sheet.getRange(1,1,lastRow+1,lastColumn+1);
 sheet.setActiveRange(range);

 var selection = sheet.getSelection();
 // Current cell: A1
 var currentCell = selection.getCurrentCell();
 // Active Range: tout le document
 var activeRange = selection.getActiveRange();
1
You should try getRange(row, column, numRows, numColumns)user8214858
Yes I know about getRange but I would be cool^^^^^^ to see in visually the selection like in the photo I've attached because I tried with var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; return sheet.getDataRange(); and it doesn't select all the sheet visually I mean I want to see an interactive selection :)ana maria
Oh, well then you use setActiveRange(range) after getRange().user8214858

1 Answers

9
votes

Yes, you can select a row or any range in the spreadsheet using Apps Script. You do so with method setActiveRange(). An example follows below:

 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var range = sheet.getRange('A1:D4');
 sheet.setActiveRange(range);

 var selection = sheet.getSelection();
 // Current cell: A1
 var currentCell = selection.getCurrentCell();
 // Active Range: A1:D4
 var activeRange = selection.getActiveRange();

Notice The method getDataRange() selects range "in which data is present.", so it won't select blank cells or the whole document. For such task, you need method getRange(row, column, numRows, numColumns) combined with getMaxRows() and getMaxColumns().

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
sheet.setActiveRange(range);

var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();