James
James

Reputation: 3

Call function from cell value, then run function

I'm looking to call a function from a cell value, then run that function. In the script below, A45 in the active sheet contains the function I want to run. The text in A45 is "RunScript1()", however this will change depending on the sheet being used.

function GetScript(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var cell1 = sheet.getRange('A45').getValue();
cell1   //// help here please ////
}

Thanks in advance.

Upvotes: 0

Views: 929

Answers (1)

TheMaster
TheMaster

Reputation: 50426

Change the text to function name without brackets: RunScript. Then you can use

this[cell1]();

this refers to the global namespace and contains the function's names. Therefore the function can be accessed and executed.

Although limited to the function name, Note that this exposes your account to arbitrary code to be run from the sheet. If you shared the sheet, any one with evil intentions may be able to trick you into executing arbitrary code, like deleting your gmail or sending email as you. Therefore, this method should preferably be avoided. Use custom menus or a pre written onEdit script instead.

Upvotes: 2

Related Questions