在 Google 脚本中从自定义模式添加值的问题

Issues adding value from custom modal in Google Script

我这里有一个函数应该执行以下操作:

  1. 调用下拉 html 模式并将用户选择的内容传递给替换函数,该函数...
  2. 在 Google 演示文稿中找到 "Document Classification: UNDEFINED" 的实例,并将其替换为 "Document Classification: selectValue from the modal"。

然而,当我记录 selectValue 时,它​​所做的只是获取 "Selected value: undefined" 并替换 "Document Classification: undefined"。到底是怎么回事。

function AddValuesFromModal(selectValue) {
  var documentId = SlidesApp.getActivePresentation().getId();
    var htmlDlg = HtmlService.createHtmlOutputFromFile("HTML_myHtml")
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setWidth(200)
    .setHeight(150);

  var modal = SlidesApp.getUi();
  modal.showModalDialog(htmlDlg, "Document Classification");
  console.log("Getting document log...");
  var body2 = SlidesApp.openById(documentId);
  console.log("Got document!");
  console.log("Selected value: " + selectValue);
  console.log("Start setting value....");
  SlidesApp.getActivePresentation().replaceAllText("Document Classification: UNDEFINED", "Document Classification: " + selectValue, true);
  console.log("Value has been set!");
}
<form id="docType">
<select id="selectDocumentType" name="documentClass">
  <option value="PUBLIC">Public</option>
  <option value="INTERNAL">Internal</option>
  <option value="CONFIDENTIAL">Confidential</option>
  <option value="SECRET">Secret</option>
</select>

<hr/>
 <input type="button" onClick="formSubmit();" value="Submit" />
</form>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    </script>
    <script>a
    function formSubmit(){
    Submit();
    closeIt();
    }
   
    
    function Submit() {
    var selectedValue = $('#selectDocumentType').val();
    console.log(selectedValue);
      google.script.run
        .withSuccessHandler(closeIt)
        .AddValuesFromModal(selectedValue);
      };
      
    function closeIt(){
      google.script.host.close();
    };
    </script>

您描述的 2 个步骤需要作为单独的函数实现。

我整理了一个小例子,最初是基于你的例子,但稍微清理了一下。

想要的程序流程是:

  1. 在服务器端,调用 showDialog() 函数显示对话框(通过菜单或其他方式)
  2. 在客户端,通过google.script.run调用updateDocumentClassification()函数并传递用于处理服务器端的表单元素(将是converted to an object

示例如下:

Code.gs

function showDialog() {
  var dialog = HtmlService.createHtmlOutputFromFile('dialog')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setWidth(200)
      .setHeight(150);
  SlidesApp.getUi()
      .showModalDialog(dialog, 'Document Classification');
}

function updateDocumentClassification(formObject) {
  var classification = formObject.documentClass;
  SlidesApp.getActivePresentation().replaceAllText(
      'Document Classification: UNDEFINED', 
      'Document Classification: ' + classification,
      true);
}

function onOpen() {
   SlidesApp.getUi()
       .createMenu('Replace Test')
       .addItem('Replace Text...', 'showModalDialog')
       .addToUi();
}

dialog.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form id="docType" onSubmit="handleFormSubmit(this)">
      <select id="selectDocumentType" name="documentClass">
        <option value="PUBLIC">Public</option>
        <option value="INTERNAL">Internal</option>
        <option value="CONFIDENTIAL">Confidential</option>
        <option value="SECRET">Secret</option>
      </select>    
      <hr/>
      <input type="submit" value="Submit" />
    </form>
  </body>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script>

function handleFormSubmit(formObject) {
  google.script.run
      .withSuccessHandler(closeDialog)
      .updateDocumentClassification(formObject);
  $('#formDocumentType :submit').attr('disabled', true);
}

function closeDialog(){
  console.log('Closing dialog...');
  google.script.host.close();
}

</script>

</html>