Javascript focus()在此getUi()中不工作。在Chrome中显示侧栏html

Javascript focus()在此getUi()中不工作。在Chrome中显示侧栏html,javascript,google-chrome,google-apps-script,Javascript,Google Chrome,Google Apps Script,我正在打开一个侧边栏,其中包含一个使用自定义菜单栏函数的GoogleSheets表单 我希望每当侧边栏打开时,都能聚焦(这样光标就在其中,任何键入都会立即添加到其中)。但是使用focus()不起作用。(根据下一行,对其应用样式正在工作。) 请问我怎样才能做到?非常感谢你的帮助 注意:它在Chrome(Win 10)中不起作用,但在Firefox中起作用 function abc() { var html = '<form id="form">' + '&l

我正在打开一个侧边栏,其中包含一个使用自定义菜单栏函数的GoogleSheets表单

我希望每当侧边栏打开时,
都能聚焦(这样光标就在其中,任何键入都会立即添加到其中)。但是使用
focus()
不起作用。(根据下一行,对其应用样式正在工作。)

请问我怎样才能做到?非常感谢你的帮助

注意:它在Chrome(Win 10)中不起作用,但在Firefox中起作用

function abc() {
    var html = 
    '<form id="form">' + 
    '<textarea id="title"> </textarea> '+ 
    '</form> <button>Submit</button> '+
    '<script>'+
    ' document.getElementById("title").focus(); '+ // << NOT WORKING 
    ' document.getElementById("title").style.outline ="solid 2px red"' +  // << WORKING
    ' </script>'+
    '';

  var a = HtmlService.createHtmlOutput(html)
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setTitle('MY-FORM')
  SpreadsheetApp.getUi().showSidebar(a); 
}

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('My Functions')
  .addItem('Start', 'abc')
  .addSeparator()
  .addToUi();
}



函数abc(){
变量html=
'' + 
'  '+ 
“提交”+
''+

'document.getElementById(“title”).focus();'+/尝试添加布尔属性:

“”

尝试添加布尔属性:

“”

您必须等待页面加载

试试这个:

function abc() {
  var html ='<form><input type="text" id="title" name="title" /><br /><input type="button" value="Submit" onclick="google.script.run.processForm(this.parentNode);"/></form>';
  html+='<script>window.onload=function(){document.getElementById("title").focus();document.getElementById("title").style.outline="solid 2px red";}</script>';
  var ui=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(ui); 
}

function processForm(obj) {
  console.log(JSON.stringify(obj));
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Sheet1');
  sh.getRange('A1').setValue(obj.title);
}
函数abc(){
var html='
'; html+=“window.onload=function(){document.getElementById(“title”).focus();document.getElementById(“title”).style.outline=“solid 2px red”;}”; var ui=HtmlService.createHtmlOutput(html); SpreadsheetApp.getUi().showSidebar(ui); } 函数processForm(obj){ log(JSON.stringify(obj)); const ss=SpreadsheetApp.getActive(); const sh=ss.getSheetByName('Sheet1'); sh.getRange('A1').setValue(对象标题); }
您还可以使用文本区域:

function abc() {
  var html ='<form><textarea rows="1" cols="15" id="title" name="title"></textarea><br /><input type="button" value="Submit" onclick="google.script.run.processForm(this.parentNode);"/></form>';
  html+='<script>window.onload=function(){document.getElementById("title").focus();document.getElementById("title").style.outline="solid 2px red";}</script>';
  var ui=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(ui); 
}
函数abc(){
var html='
'; html+=“window.onload=function(){document.getElementById(“title”).focus();document.getElementById(“title”).style.outline=“solid 2px red”;}”; var ui=HtmlService.createHtmlOutput(html); SpreadsheetApp.getUi().showSidebar(ui); }
您必须等待页面加载

试试这个:

function abc() {
  var html ='<form><input type="text" id="title" name="title" /><br /><input type="button" value="Submit" onclick="google.script.run.processForm(this.parentNode);"/></form>';
  html+='<script>window.onload=function(){document.getElementById("title").focus();document.getElementById("title").style.outline="solid 2px red";}</script>';
  var ui=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(ui); 
}

function processForm(obj) {
  console.log(JSON.stringify(obj));
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Sheet1');
  sh.getRange('A1').setValue(obj.title);
}
函数abc(){
var html='
'; html+=“window.onload=function(){document.getElementById(“title”).focus();document.getElementById(“title”).style.outline=“solid 2px red”;}”; var ui=HtmlService.createHtmlOutput(html); SpreadsheetApp.getUi().showSidebar(ui); } 函数processForm(obj){ log(JSON.stringify(obj)); const ss=SpreadsheetApp.getActive(); const sh=ss.getSheetByName('Sheet1'); sh.getRange('A1').setValue(对象标题); }
您还可以使用文本区域:

function abc() {
  var html ='<form><textarea rows="1" cols="15" id="title" name="title"></textarea><br /><input type="button" value="Submit" onclick="google.script.run.processForm(this.parentNode);"/></form>';
  html+='<script>window.onload=function(){document.getElementById("title").focus();document.getElementById("title").style.outline="solid 2px red";}</script>';
  var ui=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showSidebar(ui); 
}
函数abc(){
var html='
'; html+=“window.onload=function(){document.getElementById(“title”).focus();document.getElementById(“title”).style.outline=“solid 2px red”;}”; var ui=HtmlService.createHtmlOutput(html); SpreadsheetApp.getUi().showSidebar(ui); }
在我的例子中,当我打开侧边栏时,你的代码确实集中在文本区域。Cooper提供的解决方案解决了你的问题吗?@Jescanellas不幸的是,Cooper的解决方案在Chrome(Win 10)中仍然不适用于我我编辑了我的问题以反映这一点。谢谢。这很奇怪,你尝试过匿名模式吗?在我的例子中,当我打开侧边栏时,你的代码确实集中在文本区域。库珀提供的解决方案解决了你的问题吗?@Jescanellas不幸的是,库珀的解决方案在Chrome中仍然不适用于我(Win 10)我编辑了我的问题以反映这一点。谢谢。这很奇怪,你尝试过隐姓埋名模式吗?不幸的是,这在Chrome(Win 10)中对我仍然不起作用。我应该注意,即使没有
窗口,代码也可以在Firefox中运行。我现在编辑我的问题以反映这一点。谢谢。不幸的是,这在Chrome(Win 10)中仍然不适用。我应该注意到,即使没有
窗口,代码也可以在Firefox中运行。我现在编辑我的问题以反映这一点。谢谢。我昨晚在chrome V8中运行我昨晚在chrome V8中运行