Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current active sheet in my script?

I'm using this code to use data in a Google Apps Script:

function getCurrentRow() {
      var currentRow = SpreadsheetApp.getActiveSheet().getActiveSelection().getRowIndex();
      return currentRow;
    }

But when I use other sheet than the first one (number "gid=0"), my function remains getting data from that first sheet and not from my current active sheet. Since I'm using the method .getActiveSheet() how can I fix that?

PS - I'm not calling the code from a cell. I'm using this code: http://opensourcehacker.com/2013/01/21/script-for-generating-google-documents-from-google-spreadsheet-data-source/

like image 931
craftApprentice Avatar asked Nov 08 '13 03:11

craftApprentice


People also ask

How do I get the current time in Google script?

Take advantage of Google Sheets date and time functions: TODAY() - returns the current date to a cell. NOW() - returns the current date and time to a cell. Note.

What does Spreadsheetapp flush (); do?

flush() Applies all pending Spreadsheet changes. Spreadsheet operations are sometimes bundled together to improve performance, such as when doing multiple calls to Range.getValue().

How can I see log in script app?

A basic approach to logging in Apps Script is to use the built-in execution log. To view these logs, at the top of the editor, click Execution log. When you run a function or use the debugger, the logs stream in real time. You can use either the Logger or console logging services in the built-in execution log.


2 Answers

I created from scratch a brand new spreadsheet, within which, I created a few tabs/sheets with some data within each; and then in google apps script in that spreadsheet I installed the following code:

function getCurrentRow() {
  var currentSelection = SpreadsheetApp.getActiveSheet().getActiveSelection()
  var currentValue = currentSelection.getValue();
  var currentRow = currentSelection.getRowIndex();
  Logger.log(currentValue);
  Logger.log(currentRow);

  return currentRow;
}

I ran the script, and it gave me the correct results for the sheet that was open/which cell was selected. So I would say that this code pretty much works as you expect.

In your case, may I suggest that the quickest way to get more info about the error, or to see if the error persists, is for you start from scratch too, with a new spreadsheet, and paste in the code above, and then test to prove that at least that much works for you too. Then, only after this, paste in the greater code (that you have linked to), and see if it still/stops working.

like image 141
David Tew Avatar answered Oct 07 '22 12:10

David Tew


I'm having the same problem while developing in the Script Editor -- the Script Editor instance/window becomes 'disconnected' from the Sheets instance/window and just has the first sheet / A1 etc as the 'actives'.

What worked for me:

Closing the Script Editor window and re-opening from Sheet > Tools > Script editor. Voila, .getActive...()s are working again.

Also:

As implied by some of the other answers, triggering the execution from the Sheets window/instance (probably always) also works. One of the answers calls a function from a cell, which means it's going to be triggered by the Sheet. Another option would be to add a .UI menu and menu-option and trigger it there.

like image 38
Benjamin Warren Avatar answered Oct 07 '22 11:10

Benjamin Warren