jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 如何确定Google表格窗口大小大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在从Google Sheet包含的脚本启动的模态对话框中,是否有某种方法可以确定浏览器窗口或主机显示的大小?我希望能够将对话框的宽度设置为用户显示的百分比,因此支持各种计算机.

正常的jQuery技术如$(window).width()和$(document).width()返回对话框的宽度,这不是我想要的(并且对于这种环境是唯一的).

试图引用对话框外的任何div容器都会返回null(我已经尝试过“#docs-editor-container”,“modal-dialog-bg”和其他几个.)

这是一个简单的脚本,用我到目前为止的结果重新创建这个对话框.

Code.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>

解决方法

您可以尝试使用CSS样式在Width = 100%和Height = 0%上获得屏幕开发的大小.然后使用document.getElementById(“container”).offsetHeight

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

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

code.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>

在此示例中,您可以删除注释(/ * * /)并尝试获取Heigth大小.

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

大佬总结

以上是大佬教程为你收集整理的jquery – 如何确定Google表格窗口大小全部内容,希望文章能够帮你解决jquery – 如何确定Google表格窗口大小所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: