How to directly call static methods in classes in libraries in Google AppsScript

64 Views Asked by At

Preface:

I have already checked out Can I use Class objects in Google Apps Script Libraries?, but that calls only one method.

What I want is to access the static methods directly, without instancing an object. AND I'd like to have autocomplete available in the GAppsScript editor.

Situation:

I have got a library called "XLib" inside a Google Sheets file's AppsScript.

It contains one script file: TestClasses.gs

class XObj {
  static isValid(pObject) {
    return pObject != null && pObject != undefined;
  }
}
class XStr {
  static containsStr(pHaystack, pNeedle) {
    if (!XObj.isValid(pHaystack)) return false;
    if (!XObj.isValid(pNeedle)) return true;
    let pos = pHaystack.indexOf(pNeedle);
    Logger.log("XStr.containsStr(" + pHaystack + ", " + pNeedle + ") => " + pos);
    return pos >= 0;
  }
}

function testClassesLocally() {
  Logger.log("0:\t" + "Hallo Welt!".indexOf("Hallo") > 0);
  Logger.log("1:\t" + XStr.containsStr("Hallo Welt!", "Hallo"));
  Logger.log("2:\t" + XStr.containsStr("Hallo Welt!", "Welt"));
  Logger.log("3:\t" + XStr.containsStr("Hallo Welt!", "Bier"));
}

function getXStr() {
  return XStr;
}

let XRef = XObj;
let XInst = new XObj();

If I run the method testClassesLocally(), all works fine.

Now I got into the Project Settings, and copy its Script ID, which is a 57 chars long String.

Problem:

Now I have a second Sheet with its own AppsScript container, where I want to use the Scripts from that library.

I have a file called "Code.gs" (default name).

I have added the Library by pressing the "+" sign next to "Libraries", and entered the Script ID from above. I have defined the library's name "YLib" to avoid any collisions. (I also tried with leaving it "XLib", but same problems...)

Inside Code.gs is the following code:

function testClassesRemotely() {
  // runs fine
  Logger.log(" - - - TEST 1 - - - ");
  YLib.testClassesLocally();

  Logger.log("");
  Logger.log(" - - - TEST 2 - - - ");
  let x = YLib.getXStr(); // OK
  Logger.log("0:\t" + "Hallo Welt!".indexOf("Hallo") >= 0); // OK
  Logger.log("1:\t" + x.containsStr("Hallo Welt!", "Hallo")); // OK
  Logger.log("2:\t" + YLib.getXStr().containsStr("Hallo Welt!", "Welt")); // OK
  //Logger.log("3:\t" + YLib.XStr.containsStr("Hallo Welt!", "Bier")); // FAIL: TypeError: Cannot read properties of undefined (reading 'containsStr')
  //Logger.log("4:\t" + YLib.XRef.containsStr("Hallo Welt!", "Bier")); // FAIL: TypeError: Cannot read properties of undefined (reading 'containsStr')
  //Logger.log("5:\t" + YLib.XInst.containsStr("Hallo Welt!", "Bier")); // FAIL: TypeError: Cannot read properties of undefined (reading 'containsStr')
}

After lots of fizzling, I got all code here to work, except the lines with log messages 3-5.

How to access methods inside XStr (like XStr.containsStr(...) ) directly? And preferably with autocomplete available in the GAppsScript editor?

And also important: how to access those imported function names as formulas in the cells of the sheet? (=containsStr(A1, "Peter"))

I feel like this article https://ramblings.mcpher.com/apps-script/apps-script-v8/multiple-script-files/ comes closest, but especially the last code snippet ("Libraries") is the most interesting one, but I don't understand how to use that for my purpose.

0

There are 0 best solutions below