从模式对话框页面获取返回值

时间:2019-05-19 19:21:25

标签: google-apps-script google-sheets

我看了一些有关如何使用GAS打开模式对话框的教程和页面。 我的目的很简单。我想打开一个带有字段的对话框。用户输入一个值,然后在脚本的后续步骤中获取它。

在电子表格中,我有

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('datePaiementDlg');
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showModalDialog(html, 'Dialog title');

}
function returnDatePaiementDlg( values)
{
  Logger.log(values);
  Browser.msgBox(values);
}

HTML页面是:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>

<form>
   <input type="date" name="datePaiement"/>

   <input type="submit" value="Envoyer" class="action" onclick="form_data()" />
   <input type="button" value="Fermer"  class="action" onclick="closeIt()" />
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
<script>
function form_data(){
  var values = $("input[name=datePaiement]).val();
  google.script.run
  .withSuccessHandler(closeIt)
  .returnDatePaiementDlg(values);
};
function closeIt(){
google.script.host.close();
};
</script>
</body>
</html>

我从Google Spreadsheet中的菜单项运行openDialog()函数。 当我单击提交按钮时,将打开一个新的空白页。如果我返回到电子表格页面,则该对话框仍处于打开状态(应将其关闭,否?)

然后,另一个问题:我想使用用户在showModalDialog()之后输入的值。但是我不知道如何,因为“提交”按钮调用了另一个函数(returnDatePaiementDlg())。如何建立“链接”?

我忘记了什么...什么?

谢谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

function askAQuestion() {
  var ss=SpreadsheetApp.getActive();
  var html='<select id="sel1" onchange="sendChoice()">'
  html+='<option value="no Choice">Make a Choice</option>';
  html+='<option value="Sheet1">Sheet1</option>';
  html+='<option value="Sheet2">Sheet2</option>';
  html+='<option value="Sheet3">Sheet3</option>';
  html+='</select>';
  html+='<script>function sendChoice() { var choice=document.getElementById(\'sel1\').value;google.script.run.withSuccessHandler(function(){google.script.host.close();}).displayChoice(choice);}console.log(\'My Code\');</script>';
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Make a Choice');
}

function displayChoice(choice) {
  SpreadsheetApp.getUi().alert(choice);
  return;
}
相关问题