0

I'm playing with Google Sheets since a few days and I'm up to the point when i'm trying to "see" where the user is on the sheet.

To achieve that, i tried using the getActiveCell() method in a while loop, like that :

function menuItem1() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  row = 0;
  while(1) //WARNING : infinite loop
  {
    new_row = sheet.getActiveCell().getRow();
    if (row != new_row)
    {
      Browser.msgBox("You have selected row " + new_row);
      row = new_row;
    }
  }
}

In short, if the user selects a new cell, a message box will prompt, displaying the row of the new cell.

And for some reason, it works only for the first loop iteration. I launch the script, the message box tells me 'You have selected row xxx', and if select a cell in an other row... it does nothing.

When digging through the documentation, I discovered the getCurrentCell() method, but it does exactly the same.

It looks like while the while loop is running, the script can't "see" the updates on the sheet.

What did I do wrong in my code ? Or is it a known (weird) behavior ?

Thanks for any help =)

  • Because your function is named menuItem1 i’m Assuming you start it from a menu. If you move the cursor to a different cell you have to pick the menu item again. The whole idea of a loop to get the row number doesn’t make sense as the row number is already available from getActiveCell – TheWizEd Mar 20 '19 at 18:09
  • The functions starts from a menu yes. So it means that once i clicked on the menu item, everything i do on the sheet won't be seen by the script ? Feels weird... And the loop is useless here indeed, but i plan to use it later, add some things inside it. – rbechir Mar 20 '19 at 18:28
  • Study this related question & answer: https://stackoverflow.com/questions/30628894/how-do-i-make-a-sidebar-display-values-from-cells/30634581#30634581 – tehhowch Mar 20 '19 at 19:02
  • @rbechir there is no way to just monitor clicks on a spreadsheet – TheWizEd Mar 20 '19 at 20:01

0 Answers0