1
votes

I have a sppreadsheet with many rows, in each row - cell with status in second column for example: a010111101001 and about 30 more statuses. In scripts I have functions named the same like status cell. All I want is one function, that call all other functions based on active status cell. Now I have only function, that get name of the script from active cel, and will be very grateful if you can help me. Here is code that i have:

function nameoffunc() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('123')
  var row = ss.getActiveCell().getRow();
  var status = ss.getRange(row, 2).getValue();
  //value from status - is the name of function
  //I search way to do something like this:
  //run.scrpt(status);
}

Update: In status variable - value "a00110000000000000"

function a00110000000000000(){
  var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('123')
  var result = ss1.getRange("C35");
  result.setValue(22)
}

function nameoffunc() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('123')
  var row = ss.getActiveCell().getRow();
  var status = ss.getRange(row, 2).getValue();
  var a = {
    "u2": function(){return status;},
    "as": function(){return "eft";}
          };
Logger.log(a["u2"]());
}

Update: Thank you @daniel ! The way to run scripn by name from spreadsheet cell is:

function nameoffunc() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('123')
  var row = ss.getActiveCell().getRow();
  var status = ss.getRange(row, 2).getValue();
  var a = {
    "a00110000000000000": function(){
  var ss1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('123')
  var result = ss1.getRange("C35");
  result.setValue(22)
    },
    "as": function(){return "eft";}
  };
Logger.log(a[status]());
}
1

1 Answers

1
votes

What you can do is this:

  • On one cell, create a in-cell drop down list of all your function names
  • Create a onEdit() function to handle value change.
  • In your event handler, check if range is the cell with the drop-down list.
  • Check the value and execute the appropriate function.

Easy to call function without using switch:

To define functions:

  var a = {
    "u2": function(){return "abc";},
    "u1": function(){return "eft";}
  };

To call a function by name (example):

Logger.log(a["u2"]());