Mog*_*dad 10 jquery google-docs google-sheets google-apps-script
在从Google Sheet包含的脚本启动的模态对话框中,是否有某种方法可以确定浏览器窗口或主机显示的大小?我希望能够将对话框的宽度设置为用户显示的百分比,因此支持各种计算机.
正常的jQuery技术喜欢$( window ).width()并$( document ).width()返回对话框的宽度,这不是我想要的(并且对于这种环境是唯一的).
试图引用对话框外的任何div容器返回null(我已尝试过"#docs-editor-container","modal-dialog-bg"还有其他几个.)

这是一个简单的脚本,用我到目前为止的结果重新创建这个对话框.
function openDialog() {
var htmlOutput = HtmlService.createHtmlOutputFromFile('Dialog')
.setWidth(500)
.setHeight(400);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'How wide is the screen?');
return;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
小智 0
您可以尝试将屏幕的大小开发为宽度=100%、高度=0%的CSS样式的DIV。然后使用 document.getElementById("container").offsetHeight
问题是您只能在 SandBox EMULATE 模式 (HtmlService.SandboxMode.EMULATED) 上使用它。
代码.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('index')
.setSandboxMode(HtmlService.SandboxMode.EMULATED);
}
Run Code Online (Sandbox Code Playgroud)
索引.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>
Run Code Online (Sandbox Code Playgroud)
在此示例中,您可以删除注释 (/* */) 并尝试获取高度大小。
要获得完整的西班牙语版本,请检查https://sites.google.com/site/appsscriptenespanol/obtener-tamano-maximo-de-pantalla-en-apps-script-usando-htmlservice