I created a Google App Script named 'MySimpleSharedCode' with the following code:
var MySimpleSharedCode = MySimpleSharedCode || {};
MySimpleSharedCode.sayHello = function() {
return "hello from MySimpleSharedCode";
}
function sayHello2() {
var result = MySimpleSharedCode.sayHello();
console.log(result);
return result;
}
I deployed this script as a Library.
Then created a new Google App Script named 'BorrowerCode'. Added the 'MySimpleSharedCode' library and then following code:
function sayHello3() {
var result = MySimpleSharedCode.sayHello();
console.log(result);
return result;
}
Now when I run sayHello2() in 'MySimpleSharedCode' I see in the logs what I would expect:
hello from MySimpleSharedCode
But when I run sayHello3() in 'BorrowerCode' I get an error
TypeError: MySimpleSharedCode.sayHello is not a function
sayHello3 @ Code.gs:2
What am I doing wrong?
In the screenshot above, looks like the library got added. But somehow I'm not able to access the code in the library?
In your situation, when MySimpleSharedCode
is installed in BorrowerCode
as a library of identification of MySimpleSharedCode
, MySimpleSharedCode
has the objects of MySimpleSharedCode
and sayHello2
.
In your following sample script of BorrowerCode
,
function sayHello3() {
var result = MySimpleSharedCode.sayHello();
console.log(result);
return result;
}
MySimpleSharedCode
has no object of sayHello()
. By this, such an error occurs. If you want to run the function of sayHello
of MySimpleSharedCode
from BorrowerCode
, please modify it as follows.
function sayHello3() {
var result = MySimpleSharedCode.MySimpleSharedCode.sayHello();
console.log(result); // hello from MySimpleSharedCode
return result;
}
For example, if you want to call sayHello
using MySimpleSharedCode.sayHello()
from BorrowerCode
, how about the following modification?
MySimpleSharedCode
var MySimpleSharedCode = MySimpleSharedCode || {};
var sayHello = function () {
return "hello from MySimpleSharedCode";
};
MySimpleSharedCode.sayHello = sayHello;
function sayHello2() {
var result = MySimpleSharedCode.sayHello();
console.log(result);
}
BorrowerCode
function sayHello3() {
var result = MySimpleSharedCode.sayHello();
console.log(result);
}
sayHello2()
is run at MySimpleSharedCode
, hello from MySimpleSharedCode
is shown. And, when sayHello3()
is run at BorrowerCode
, hello from MySimpleSharedCode
is shown.Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments