Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active December 4, 2022 23:41
Show Gist options
  • Save mark05e/500b875d7de3009fcebe0f3f44db8ab4 to your computer and use it in GitHub Desktop.
Save mark05e/500b875d7de3009fcebe0f3f44db8ab4 to your computer and use it in GitHub Desktop.
Based on function documented on [](https://stackoverflow.com/questions/48928932/call-library-function-from-html-with-google-script-run) with some enhancements for my usecase.
// Function - callLibraryFunction
// based on function documented on
// https://stackoverflow.com/questions/48928932/call-library-function-from-html-with-google-script-run
// with some enhancements for my usecase.
//
function callLibraryFunction(functionPathAndName, ...args){
let fnPathArray = functionPathAndName.split(".");
let fnPathLength = fnPathArray.length
let libFunc = fnPathArray[fnPathArray.length - 1];
let argLength = args.length
// check - library loaded
libraryPath1 = "typeof " + fnPathArray[0]
libraryPath1Type = eval(libraryPath1)
if (libraryPath1Type !== 'object') {
console.error("Library not found - " + fnPathArray[0])
return false
}
// check - deconstruct array if size 1
// because it could mean that this is a string
if (argLength = 1){
args = args[0]
}
// check - if arg is an actual array, label it is an array
let argType = typeof args
// check if args is an array
if (argType === 'object') {
let validArray = Array.isArray(args)
if (validArray){
argType = 'array'
}
}
// Old school way of debugging
// console.log({functionPathAndName, args, fnPathArray, fnPathLength, libFunc, argLength, argType})
// Retun by triggering function
if (fnPathLength === 3){
return this[fnPathArray[0]][fnPathArray[1]][libFunc].call(this, args);
}
if (fnPathLength === 2){
return this[fnPathArray[0]][libFunc].call(this, args);
}
else {
console.error("Incompatible number of libraries... STAP!")
throw new Error({functionPathAndName, args, fnPathArray, fnPathLength, libFunc, argLength, argType})
}
}
@mark05e
Copy link
Author

mark05e commented Dec 4, 2022

function test_callLibraryFunction(){
  
  let argsObj = {"hello":"mello","tello":"sello"}
  let argsArr = ["hello","mello","sello"]
  let argsStr = '1,2,3,4'
  let result = ""
  
  result = LibraryLayer1.SubLibrary1.ping(argsArr)
  console.log({result})

  result = callLibraryFunction("LibraryLayer1.SubLibrary1.ping",argsObj)
  console.log({result})
  
  result = callLibraryFunction("LibraryLayer1.SubLibrary1.ping",argsArr)
  console.log({result})
  
  result = callLibraryFunction("LibraryLayer1.SubLibrary1.ping",argsStr)
  console.log({result})
  
  result = callLibraryFunction("LibraryLayer1.SubLibrary1.ping",1,2,3,4,5)
  console.log({result})

  result = callLibraryFunction("LibraryLayer1.SubLibrary1.ping")
  console.log({result})
}

@mark05e
Copy link
Author

mark05e commented Dec 4, 2022

  • TODO: Notes about this function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment