Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Google Script, call a function by its name, given its name in a string

I have tried the code examples that I found here on the "stackoverfllow" site, related to calling a function from a string. They don't seem to work in a Google App Script. I receive an error to effect that "window" as an undefined object.

Here is the situation that I am trying to solve. I have a spreadsheet that is going to have many different sheets. Some of these sheets will have names that are known at the time I write the code. Other sheets will have names that are not known exactly at the time I write the code (e.g. based on dynamic data).

The current documentation tells me that the name of the "onEdit" event handler is "onEdit". Since thee name is unique, I conclude that there can only be one such routine with this name within a spreadsheet application.

Because of this situation (above) I would like reduce the complexity of my "onEdit" function. I would like to write the "onEdit" routine to do "dynamic" calls to sub-functions which use qualified names, based upon the name of the "sheet" in which the edit event occurred.

Here is a pseudo code example of the "onEdit" routine I want to right.

  function onEdit(src)
  {
     var act_sheet = src.getActiveSheet();
     var sheet_name = act_sheet.getName();
     var rtn_name = "onEdit_sheet_".sheet_name;

     if ( function_exists(rtn_name) )
     {
         window[rtn_name](srv)
     }
  }

I have actually tried similar code. In the google environment the "window" object is flagged as an unknown object.

Is there some other "object" name in the environment that I should be using? Is this possible within the Google Apps Script environment?


In addition, I know that the "function_exists" routine is also a challenge to write. That will be my next question. I know about the "typeof" operation, it returns "string" when I code

      "if ( typeof rtn_name == function" ) .. "

Is there a way to test that a routine exists given a name contained in a string?


I know I can use "static" names and hard code them in the "onEdit" routine. But I would like to write the routine once, and not have to modify it for each new spreadsheet that I write. In stead of writing very complicated "onEdit" routines, I want to concentrate on writing and testing "onEdit" functions for individual sheet's.

I understand Javascript within a browser environment. The Google apps script environment is more of a mystery to me. I find the documentation very very terse, and in need of fuller explanations. Any additional information about what objects exist in the Google apps script environment would be helpful.


Of course my third step might be to write an "onEdit" routine that dispatches the correct sub-function based upon the current "range" associated with an Edit event.


I am NEW to Google Apps Scripts. I am looking for a script solution. It probably can be done in "Java", but that is beyond the scope in which I want to code.


I am also new to the "stackoverflow" environment. Much of it seems a mystery to me. (e.g. How do I specify which tags should be associated with this question? How do I know which tags are available to be assigned to this question? How do I limit my search to certain tags? - I am at the moment interested in "google apps scripts", when I select the "javascript" button in the right column it takes me into a javascript answers which may not apply to the google script environment. Found out how to "assign" tags below. How do I know which tags are available without just guessing?

All and any help is appreciated.

like image 731
just.a.guy Avatar asked Dec 15 '22 23:12

just.a.guy


1 Answers

I know two ways to call the function using its name on a string in Apps Script.

function onEdit(e) {
  var func = 'test';
  this[func]();
  //eval(func+'()'); //msgBox and eval don't play nice together
  Browser.msgBox('finished');
}

function test() {
  Browser.msgBox('test');
}

Of course, when using the this approach, you should not be in another scope, e.g. from a function called using new. But this exact full code that I'm posting works fine.

like image 199
Henrique G. Abreu Avatar answered Dec 28 '22 06:12

Henrique G. Abreu