Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type checking on google apps script platform

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
like image 321
vetvicka Avatar asked Dec 21 '22 08:12

vetvicka


1 Answers

After observing the output of Logger.log() it is clear that for most built-in Google Apps objects the output of the toString() method is the Class name:

var sheet = SpreadsheetApp.getActiveSheet()
if (typeof sheet == 'object')
{
    Logger.log(  String(sheet)     ) // 'Sheet'
    Logger.log(  ''+sheet          ) // 'Sheet'
    Logger.log(  sheet.toString()  ) // 'Sheet'
    Logger.log(  sheet             ) // 'Sheet' (the Logger object automatically calls toString() for objects)
}

So any of the above can be used to test the type of the object (except the last one example that obviously works only with the Logger)

like image 172
Steven Pribilinskiy Avatar answered Dec 28 '22 06:12

Steven Pribilinskiy