0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.