Home > Back-end >  Getting Code.gs function value in JavaScript
Getting Code.gs function value in JavaScript

Time:01-31

I would like to put a function created in Code.gs in JavaScript as a condition. Not sure if it's possible, because I tried different approaches and JavaScript still can't regognize the value.

This is what I have:

Code.gs

function CheckifAdmin() {
    var admins = ['[email protected]'];
    for (var i = 0; i < admins.length; i  ) {
        if (admins[i] == Session.getActiveUser()) {
            return true
        }
    }
}

JavaScript

function ChangeEmail() {
    google.script.run.CheckifAdmin();
    if (CheckifAdmin() == true) {
        var newemail = prompt("Please enter new email");
        document.getElementById('form_email').value = newemail;
    } else {
        alert("You do not have needed permissions.")
    }
}

Maybe you could advice how to handle it? Thank you.

CodePudding user response:

gs:

function CheckifAdmin() {
  const admins = ['[email protected]'];
  return ~admins.indexOf(Session.getActiveUser().getEmail());
}

Javascript:

function ChangeEmail() {
    google.script.run
    .withSuccessHandler((v) => {
      if(v) {
        //code if true
      } else {
        //code if false
      }
    })
    .CheckifAdmin();
}
  • Related