2

background

I followed this tutorial which basically makes a button call a google sheets script like this

function jumpToDetail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("details");
  ss.setActiveSheet(sheet).setActiveSelection("A2");
}

that makes you jump to a specific cell. I looked at the google sheets docs but couldn't figure out how to make this function reference the cell it was called from

question

How do I make a function in google sheets reference the function it was called from. For example I would like the final code to look something like this:

function jumpToDetail(clickedCell) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("details");
  // get the value of "id" of the same row the cell was clicked in
  ss.setActiveSheet(sheet).setActiveSelection(function_of(id));
}
abbood
  • 23,101
  • 16
  • 132
  • 246

1 Answers1

3

The active range is obtained with getActiveRange method of SpreadsheetApp. This can be useful when a script is called from a custom function: the active range is then the cell at which the custom function is located. Example:

function currentCell() {
  return SpreadsheetApp.getActiveRange().getA1Notation();
}

Entering =currentcell() anywhere in the sheet will return the A1 notation of that cell.

But clicking a button (an inserted drawing) has no effect on active range, because drawings are not associated with any cell in a sheet. They are in their own layer, floating over the sheet. In a script called with a button, the active range is wherever the selected cell(s) happen to be, and has nothing to do with the location of the button.

In order to communicate some location to a script, the user would have to select a cell first and then call a script (via a button or a menu).

Here is a function that returns the content of the cell in the current row with column labeled "id". (The label is assumed to be in the 1st row of the sheet.)

function getId() {
  var row = SpreadsheetApp.getActiveRange().getRow();
  var sheet = SpreadsheetApp.getActiveSheet();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
  var column = headers[0].indexOf('id') + 1;
  if (column > 0) {
    return sheet.getRange(row, column).getValue();
  }
  else {
    throw new Error('No column labeled id');
  }
}
  • great.. so say i got a column called "id".. now that i have the row selected.. how do i get the value of the id column corresponding to the selected row? – abbood Jun 03 '16 at 11:23
  • I added such a script to the answer. –  Jun 03 '16 at 11:32
  • perfect.. i've awarded you the correct award b/c u definitely exceeded my expectations.. i'm trying to figure this one out now.. how do i now query another spreadhseet's id column for a specific value.. then make the selection go to that specific row? – abbood Jun 03 '16 at 11:50
  • In a comment, I can only outline: I'd use `.getRange(1, n, sheet.getLastRow(), 1).getValues()` to get the values of that column (here n is its index), then flatten it from a double to single array by `.map(function(a) {return a[0]})`, then search with `indexOf` and finally `setActiveRange`. –  Jun 03 '16 at 11:57
  • here is a generic comment: how can i debug a google spreadsheet? i know i can debug it while the window is already open.. but i wanna debug it by using a real spreadsheet? this would make my life so much easier – abbood Jun 03 '16 at 12:00
  • also i made the last question an actual question on SO: http://stackoverflow.com/questions/37613891/how-to-query-a-google-sheet-column-for-a-certain-value.. answer and i'll give you an award there as well! – abbood Jun 03 '16 at 12:03
  • If you mean notifying you of errors, [MailApp.sendEmail](https://developers.google.com/apps-script/reference/mail/mail-app#sendemailrecipient-subject-body) can send a message with a dump of some variables (using `JSON.stringify`, say). –  Jun 03 '16 at 12:04
  • no what i mean is that i keep on outputting stuff on a dialog within the spreadsheet to test what i'm doing (see [gist](https://gist.github.com/abbood/8900cc1fcc445dee1815a6556a4d560b)) i'd much rather be able to do this by setting a breakpoint and viewing stuff on a console like how we do it with chrome web dev tools – abbood Jun 03 '16 at 12:09
  • There's a built in debugger documented here: https://developers.google.com/apps-script/troubleshooting#using_the_debugger_and_breakpoints –  Jun 03 '16 at 12:15