Last active
December 4, 2022 23:41
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}) | |
} | |
} |
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