【问题标题】:How to determine Google Sheets window size如何确定 Google 表格窗口大小
【发布时间】:2014-08-13 18:31:34
【问题描述】:

在从包含 Google Sheet 的脚本启动的模态对话框中,是否有某种方法可以确定浏览器窗口或主机显示的大小?我希望能够将对话框的宽度设置为用户显示的百分比,因此支持多种机器。

$( window ).width()$( document ).width() 这样的普通jQuery 技术返回对话框 的宽度,这不是我想要的(并且是这个环境所独有的)。

尝试引用对话框外的任何 div 容器会返回 null(我尝试过 "#docs-editor-container""modal-dialog-bg" 和其他一些方法。)

这里有一个简单的脚本,用我目前得到的结果重新创建这个对话框。

代码.gs

function openDialog() {
  var htmlOutput = HtmlService.createHtmlOutputFromFile('Dialog')
     .setWidth(500)
     .setHeight(400);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'How wide is the screen?');
  return;
}

Dialog.html

<div id="outer" style="padding:1;"/>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
  $(function() {
    reportSizeOf( 'window',$(window) );                   // 500 - expected
    reportSizeOf( 'document',$(document) );                 // 500 - same as window
    reportSizeOf( '#docs-editor-container',$('#docs-editor-container'));  // null
    reportSizeOf( 'modal-dialog-bg',$('modal-dialog-bg'));         // null
  });

  function reportSizeOf( elname, element ) {
    $('#outer').append('<br>Width of "' + elname + '": ' + element.width());
  }
</script>

【问题讨论】:

  • 我过去没有运气。我不认为 Caja 传播任何有关实际浏览器窗口的知识。
  • 不确定您的控制逻辑,但是如何在对话框启动之前获取值,然后将值传递给 openDialog() 函数?
  • @Fuhrmanator - 如果我能。直到加载后才确定大小,否则用户会在任何事情发生之前坐在空白屏幕上(并单击几次)。渲染其中一个 div 可能需要 30 秒以上。即使那样 - 如果我希望最大宽度保持在用户的显示范围内,我无法知道那是什么。
  • 在我的插件的 Sidebar.html(这不是服务器端)的文档加载功能中快速尝试 alert("Window width " + $(window).width() + " Document " + $(document).width() ); 让我得到了Window width 300 Document 850,但我现在意识到你'不是在谈论附加组件。
  • @Fuhrmanator:啊,但我说的是附加组件。它有一个侧边栏和一个模态对话框。我已经将该警报放在我的侧边栏的负载中,我得到了Window width 300 Document 300。如果你真的有这个工作,请将其发布为答案。

标签: jquery google-apps-script google-sheets google-docs


【解决方案1】:

如果附加 iframe 的来源相同,我们可以跳出 2 个 iframe:

parent.parent.document.body.clientHeight

按照this post 中的说明访问它。

既然不是这样,Docs 可能需要一个增强请求,以通过postmessage 或其他一些跨域机制将最大尺寸传递到附加 iframe。将maxHeight 和宽度添加到现有的Browser 类可能更直接,但认为这是为添加浏览器类型的小部件而设计的。

设置大于所需的宽度确实会退回到文档窗口的最大宽度,但它会导致关闭按钮在用户的视野之外,迫使他们按下 esc。如果您想要 90% 最大高度/宽度的文档视图,目前似乎是不可能的。

【讨论】:

    【解决方案2】:

    您可以尝试使用宽度=100% 和高度=0% 的 CSS 样式来获取屏幕大小的 DIV。然后使用 document.getElementById("container").offsetHeight

    问题是您只能在 SandBox EMULATE 模式 (HtmlService.SandboxMode.EMULATED) 上使用它。

    这里是一个例子(https://script.google.com/d/10kzZ1DiTsere_BdjLEnIJcZaOHVCfcGF56DED6fMLY2PfY9g--h1Efrr/edit?usp=sharing):

    代码.gs

    function doGet() {
      return HtmlService.createHtmlOutputFromFile('index')
          .setSandboxMode(HtmlService.SandboxMode.EMULATED);
    }
    

    index.html

    <style>
      #container{
        width: 100%;
    /*    height: 100%; */
        background-color: #ccccff;
      }
    </style>
    <div id="container">
    </div>
    <script>
      alert(document.getElementById("container").offsetWidth);
    /*  alert(document.getElementById("container").offsetHeight); */
    </script>
    

    在本例中,您可以删除 cmets (/* */) 并尝试获取 Heigth 大小。

    要西班牙语和完整版本,请查看https://sites.google.com/site/appsscriptenespanol/obtener-tamano-maximo-de-pantalla-en-apps-script-usando-htmlservice

    【讨论】:

    • 这个问题是关于电子表格中的模式对话框...您的答案是关于 webapps...这是两件不同的事情。你的回答根本无关紧要。
    • Serge 是对的 - 您的示例在模态对话框中不起作用。
    猜你喜欢
    • 2012-07-31
    • 2013-04-16
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2018-07-15
    相关资源
    最近更新 更多