Monday, May 20, 2024
97
rated 0 times [  104] [ 7]  / answers: 1 / hits: 18711  / 11 Years ago, sun, march 10, 2013, 12:00:00

Is there a way to check againts built-in types in google apps script?
I don't know how to access contructors of built-in types. So I can't use instaceof operator.



For example Profile (https://developers.google.com/apps-script/class_analytics_v3_schema_profile )



function getReportDataForProfile(profile) {
if (profile instanceof Profile) // Profile is undefined...
...
}


Also what is a litle confusing: When I get an instance of Profile (in variable profile)



profile.constructor // is undefined

More From » google-apps-script

 Answers
9

It seems that this won't be a necessarily clean solution, but it will be functional nonetheless.



If it is a Profile object, then profile.getKind() will return analytics#profile. However, if the .getKind() method is not defined for that object, it will throw an error. So it looks like you'll have to do 2 checks.



if (typeof profile.getKind != function) {
if (profile.getKind() == analytics#profile) {
//profile is a Profile!
} else {
//profile is some other kind of object
//use getKind() to find out what it is!
}
} else {
//profile doesn't have a getKind method
//need a different way of determining what it is
}

[#79691] Friday, March 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trayvon

Total Points: 35
Total Questions: 117
Total Answers: 88

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;