【发布时间】: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