5

我正在尝试在 Google 应用程序脚本中创建一个应用程序,但我想根据屏幕的分辨率(移动和桌面)显示不同的布局。但是,我找不到有关如何确定屏幕分辨率的任何信息。

当然,我无法从服务器端确定屏幕分辨率,所以我尝试了是否可以使用客户端处理程序做一些事情,但是ClientHandlers没有任何屏幕分辨率方法。我还尝试使用 向应用程序添加手动客户端脚本app.createHTML(),但是 Google 不允许使用 createHTML 包含 javascript。

那么如何使用 Google Apps 脚本获得浏览器的分辨率呢?

4

2 回答 2

6

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;
}
于 2012-10-08T06:47:41.040 回答
1

目前没有已知的方式来执行此 UI 应用程序。但是,使用 Google Apps Script 中的 HtmlService(不是您提到的 createHTML()),您可以添加 JavaScript 并使用 JavaScript、HTMl 和 CSS 构建整个 UI。

有关文档,请参阅https://developers.google.com/apps-script/class_htmlservice

于 2012-10-07T15:04:14.957 回答