0

I'm developing a Chrome extension that has access to Google Sheets using a standalone Google Apps Script. I need help to be able to navigate and highlight given variable row from my extension. either using Apps Script or some other way.

I've tried this code in google apps script.

function navigateToRow(parameters) {
  var ss = SpreadsheetApp.openByUrl(parameters.url)
  var sheet = ss.getSheets()[0]
  var range = sheet.getRange('A1:D10'); // will get range from parameters in future
  range.activate();
}

There is another solution that could solve the problem, but it refreshes the page: using url parameters, e.g. https://docs.google.com/spreadsheets/d/<spreadsheet id>/edit#gid=<sheet id>&range=<a1 notation>.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
TechNerdXp
  • 358
  • 2
  • 6
  • 22
  • 1
    Activating a range can be troublesome since apps script runs on an arbitrary instance, not in the client browser. So what does it mean to "activate a range" when there is no UI bound to that script instance? What if the sheet is open in three browsers, only one of which has the extension even installed? G Suite Add-ons are bound to the invoking document and know which UI instance invoked the associated Apps Script code – tehhowch Mar 11 '19 at 13:23
  • @tehhowch what's your view on https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#setactiverangerange ? – Umair Mohammad Mar 11 '19 at 14:02
  • 1
    Url hash `#` will not refresh the page. – TheMaster Mar 11 '19 at 14:26
  • That seems promising. Thanks – TechNerdXp Mar 11 '19 at 14:28

2 Answers2

1

Try something like :

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

Reference : https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#setactiverangerange

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
0

Solved using these lines of code inside the content-script.

hash = window.location.hash.split('&range=')[0]
hash += `&range=${currentRowInput.value}:${currentRowInput.value}#`;
window.location.hash = hash
TechNerdXp
  • 358
  • 2
  • 6
  • 22