Tiddo
Tiddo

Reputation: 6564

Google apps script: how to determine screen resolution?

I am trying to create an app in Google apps script, but I want to display different layouts depending on the resolution of the screen (mobile & desktop). However, I can't find anything about how to determine the screen resolution.

Of course I can't determine the screen resolution from the server side, so I tried if I could do something with clientside handlers, but the ClientHandlers don't have any method for screen resolution. I also tried to add a manual clientside script to the app using app.createHTML(), however Google doesn't allow to include javascript using createHTML.

So how can I get the resolution of the browser using Google apps script?

Upvotes: 5

Views: 5131

Answers (2)

megabyte1024
megabyte1024

Reputation: 8660

The JavaScript screen.witdth property does not work in GAS environment. The Google Caja sanitizer, which is under the hood of GAS, throws the

Uncaught script error: 'screen is not defined.' in source: 'tryit.asp?filename=tryjsref%5fscreen%5fwidth' at line: 7 tryit.asp?filename=tryjsref%5fscreen%5fwidth:7: screen is not defined.

exception if to load the following HTML page to the Caja Playground. Probably it is an issue of Caja.

<!DOCTYPE html>
<html>
<body>

<script>

document.write("Test");
document.write("Total Width: " + screen.width);

</script>

</body>
</html>

A possible workaround is not to rely upon a screen resolution but to use a user-agent string. This string contains the user browser "id". The UiApp.getUserAgent method returns it. For instance, Mozilla Firefox on a desktop provides this string like - Mozilla/5.0 (Windows NT 6.0; rv:15.0) Gecko/20100101 Firefox/15.0.1,gzip(gfe). On a mobile phone this string contains ...(Android; Mobile; rv:15.0).... A check procedure searches in the returned string the words Mobile, Android, etc. If one of them found it is a mobile platform and the main code draws a mobile GUI using relative controls sizes. The following code shows how to use it

function doGet(e) {
  var app = UiApp.createApplication();
  app.add(app.createLabel(UiApp.getUserAgent()));
  return app;
}

Upvotes: 6

Srik
Srik

Reputation: 7965

There is currently no known way of doing this UI App. However, using HtmlService in Google Apps Script (not the createHTML() you mentioned), you can add JavaScript and build your entire UI in JavaScript, HTMl and CSS.

See https://developers.google.com/apps-script/class_htmlservice for documentation

Upvotes: 1

Related Questions