1

I am using Google Scripts and I just want to run a function based on cell contents. I know this looks like a formula but I want it as a script!

=if(T2="*",SplitNames,fullNames);

So if T2 contains any text then run function SplitNames. Otherwise run function fullNames.

How do I write this please? Thanks.

5
  • Are you trying to write this as a Google Sheet formula or within your google-apps-script? Commented Jul 12, 2019 at 12:30
  • Within the google apps script. The only way I know how to express what I mean is making it look like a formula!! haha Commented Jul 12, 2019 at 12:45
  • Apologies @TheMaster did I miss something. Obviously new to the forum so still finding my way. Any help appreciated. Commented Jul 12, 2019 at 13:03
  • 1
    @TheMaster it would have been if the user was trying to execute this via formula - I didn't realise until afterwards that they may have been trying to portray the logic using the formula as an example. Commented Jul 12, 2019 at 13:50
  • @ross I see............ Commented Jul 12, 2019 at 16:42

2 Answers 2

2

All you need is a simple if statement based on the value of cell "T2" which I have defined below as var cell:

function checkT2() {
  var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var cell = sh.getRange("T2").getValue();

  //check if cell is not empty
  if (cell) {
    SplitNames();
  } else {
    fullNames();
  }
}

References:

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

Comments

0

What you have to is get the content of your cell and then make your test :

yourFunction()
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var T2 = ss.getRangeByName("T2").getValue()//get the content of the cell T2 in the variable T2
  if(T2=="*")
    splitnames();//assuming you have a function splitnames in your script file
  else
    fullnames();

}

5 Comments

Thank you @LALLEMENT.E this script is definitely doing something. Does it make a difference if the contents of T2 is text? I was thinking "*" means ANY text. Seems it only wants to run the SplitNames function regardless of what is between the quotes. Thanks
I think the test T2=="*" is only true when the value in the cell T2 is the character *. If you want to test the type of the content of T2 you should do this instead : if(T2 instanceof String). Now if I understood correctly what you want to do, you should pass your variable T2 in the parameters of your function splitnames(), and fullnames(). So you should write something like this : splitnames(T2); Also can you tell me what is the code of your function splitnames and fullnames ?
Aaaah that makes sense. I want to test for ANY TEXT at al in T2 and then run one function or the other dependent on the result. Thank you, I'll have a play!
Alright but are your functions splitnames() and fullnames() in the same file as your script ?
T2 is not the content of the cell, if you want to access the content you need the method T2.getValue(), and if you want to check if T2 is empty, you do it by if(T2.getValue=="")

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.