从 HTML 输入返回应用程序脚本中的数据

2024-01-18

我正在尝试使用 HTMLService 从 google 电子表格启动 html 表单,并将数据从选择输入返回到脚本。我正在用这一行收集数据: -

但我不确定如何将其返回到脚本文件:我尝试了以下各种迭代: - 城市= form.Projects_list.text; - 城市= form.Projects_list[0]; - 城市= form.Projects_list.[0][0];

但这些似乎都不是选择的正确句柄。其他变量根据需要从表单中返回。

我怎样才能获取最后一个变量?

HTML 文件

<b>Add Row To Spreadsheet</b><br />

    <form>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#datepicker" ).datepicker();
  } );

  $( function() {
    $( ".widget input[type=submit]" ).button();
    $( "button, input, a" ).click( function( event ) {
      event.preventDefault();
    } );
  } );
  </script>


 <form id = "dateform">
 <p>Date: <input type="text" id="datepicker" name ="datepicker"></p><p></p>
    Reason for Delay: <input id="reason" name="reason" type="text" />

    Last name: <input id="lastName" name="lastName" type="text" />

   <input onclick="formSubmit()" type="button" value="Add Row" />
   <input onclick="google.script.host.close()" type="button" value="Exit" />

  <hr>
  <div id = 'pList'>

    <table>
      <tr>
        <td>Select A City</td><td><select id="Projects_list"name ="Projects_list"></select></td>
      </tr>

    </table>
   </div>


   </form>
   <script type="text/javascript">
        function formSubmit() {
            google.script.run.getValuesFromForm(document.forms[0]);
            google.script.host.close();
        }

  </script> 
  <script type="text/javascript">

    // Client-side JavaScript that uses the list returned by
    // GAS function "getValuesForRngName()" to populate the dropdown.
    // This is standard client-side JavaScript programming that uses
    // DOM manipulation to get page elements and manipulate them.
    function onSuccess(values) {
      var opt,
          dropDown;
        for(i = 0;i < values.length; i +=1){
          dropDown = document.getElementById("Projects_list");
          opt = document.createElement("option");
          dropDown.options.add(opt);
          // Remember that GAS Range method "GetValues()" returns
          // an array of arrays, hence two array indexes "[i][0]" here.
          opt.text = values[i][0];
          opt.value = values[i][0];

       }
    }
    function populate(){
      google.script.run.withSuccessHandler(onSuccess).getValuesForRngName('Projects20');
    }
  </script>
  <script>
    // Using the "load" event to execute the function "populate"
    window.addEventListener('load', populate);

  </script>

  <script>
    // Client-side JavaScript that uses the list returned by
    // GAS function "getValuesForRngName()" to populate the dropdown.
    // This is standard client-side JavaScript programming that uses
    // DOM manipulation to get page elements and manipulate them.
    function onSuccessx(values) {
      var opt,
          dropDown;
        for(i = 0;i < values.length; i +=1){
          dropDown = document.getElementById("Projects_list");
          opt = document.createElement("option");
          dropDown.options.add(opt);
          // Remember that GAS Range method "GetValues()" returns
          // an array of arrays, hence two array indexes "[i][0]" here.
          opt.text = values[i][0];
          opt.value = values[i][0];
       }
       dropDown = dropDown.options.sort()
    }
    function populatex(){
      google.script.run.withSuccessHandler(onSuccess).getValuesForRngName('Projects20');
    }
  </script>
  <script>
    // Using the "load" event to execute the function "populate"
    window.addEventListener('loadx', populate);
  </script>

应用程序脚本文件

function demoHtmlServices() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      html = HtmlService.createHtmlOutputFromFile('changeDateHTML');
  setRngName();
  ss.show(html);

}

//getValuesFromForm
function getValuesFromForm(form){
  var firstName = form.firstName,
      lastName = form.lastName,
      reason = form.reason,
      newDate = form.datepicker,
      city= form.Projects_list.text;
      sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  SpreadsheetApp.getUi().alert('Test');
  sheet.appendRow([lastName, reason, newDate, city]);
  var ui = SpreadsheetApp.getUi() ;
  alert('Test');
  ui.alert('form.Projects_list(0)')

  google.script.host.close();
  }

// Display the GUI
// Execute this function from the script editor ro run the application.
// Note the call to "setRngName()". This ensures that all newly added
// values are included in the dropdown list when the GUI is displayed
function displayGUI() {
  var ss,
      html;
  setRngName();
  ss = SpreadsheetApp.getActiveSpreadsheet();
  html = HtmlService.createHtmlOutputFromFile('index').setSandboxMode(HtmlService.SandboxMode.IFRAME);
  ss.show(html);
}
// Called by Client-side JavaScript in the index.html.
// Uses the range name argument to extract the values.
// The values are then sorted and returned as an array
// of arrays.
function getValuesForRngName(rngName) {
  var rngValues = SpreadsheetApp.getActiveSpreadsheet().getRangeByName(rngName).getValues();
  return rngValues.sort();
}

//Expand the range defined by the name as rows are added
function setRngName() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(),
      sh = ss.getSheetByName('DropdownValues'),
      firstCellAddr = 'A2',
      dataRngRowCount = sh.getDataRange().getLastRow(),
      listRngAddr = (firstCellAddr + ':A' + dataRngRowCount),
      listRng = sh.getRange(listRngAddr);
  ss.setNamedRange('20Projects', listRng);
  //Logger.log(ss.getRangeByName('Cities').getValues());
}

// For debugging
function test_setRngName() {
  setRngName();
}

您可以使用 value 属性。

document.getElementById("Projects_list").value
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 HTML 输入返回应用程序脚本中的数据 的相关文章

随机推荐

  • 错误:toString 失败 Node.js 缓冲区

    有时我对 api 的 get 请求失败并显示此错误消息 顺便说一句 我使用请求模块来触发我的请求 Error toString failed buffer js 378 throw new Error toString failed Err
  • 使用 ES 模块运行 pm2

    我如何将pm2与基于ES模块 类型 模块 的包结合使用 我研究了类似的问题 没有任何有用的帮助 有人说它在 Windows 上不起作用 但我正在使用 Linux 我总是收到错误 Error ERR REQUIRE ESM require o
  • 在 python 中检索 .ttf 字体文件中所有字形的边界框和贝塞尔曲线数据

    我有兴趣提取给定 ttf 文件中所有字形的二次贝塞尔曲线信息 目前 使用 python 中的 ttfquery 库 我能够提取给定字形的轮廓 例如a 按以下方式 from ttfquery import describe from ttfq
  • 如何检查Android GPS是否被禁用[重复]

    这个问题在这里已经有答案了 我有两个文件 MainActivity java 和 HomeFragment java MainActivity 中调用 HomeFragment 中的一个函数 要求用户打开手机上的位置服务 问题是 即使用户已
  • 运行时 AOP 与编译时 AOP

    这两种AOP框架的优缺点是什么 我使用Unity作为我的aop框架 但我猜像postsharp这样的编译时aop框架可能比运行时aop框架有更好的性能 看起来运行时aop框架使用反射来实现注入 我不是 NET 人员 但我了解 Java 生态
  • Yarn add 引发错误 缺少要添加到项目中的包列表

    重新安装 Kubuntu 18 后 我尝试运行我的 vue cli 4 0 5 vuex 3 应用程序 并收到错误 错误缺少要添加到项目中的包列表 serge AtHome mnt work sdb8 wwwroot lar VApps v
  • 在 Xcode 7.2 中使用 PDF 作为图标图像

    我正在尝试在我正在开发的应用程序中使用 PDF 文件作为图标 我遇到的问题是色调颜色不一致 如果我从界面生成器设置按钮图像 则会显示图标图像black在运行时 每次 无论我尝试从界面生成器中设置什么 我尝试通过代码设置按钮图标图像而不是显示
  • Jenkins - 负载统计图奇怪的编码

    我在最新的 Jenkins 上有这个 但图例显示的是奇怪的字符而不是英语 有人知道这可能是什么吗 编码 即使使用其他显示图表的插件也是如此 我在 CentOS Linux 版本 7 4 1708 核心 Jenkins 2 73 1 apac
  • 在球拍中使用 stop-when

    我一直在搞这个程序 它需要一个数字并加 1 我想知道你到底如何使用stop when这里 例如 让它停在 5 点 我想这里需要一个 cond 声明 谢谢 require 2htdp image require 2htdp universe
  • Pandas:添加缺失月份的数据

    我有一个按月划分的客户销售信息数据框 看起来像这样 有多个客户和不同的月份和支出 customer id month year sales 0 12 2012 05 2 58 1 12 2011 07 33 14 2 12 2011 11
  • C#:将基类转换为子类

    我有一个类 NetworkClient 作为基类 using System IO using System Net Sockets using System Threading Tasks namespace Network using S
  • JavaScript,正则表达式,向字符串中包含的所有数字添加前导零

    这个 Perl 脚本是我想在 JavaScript 中实现的 source https stackoverflow com a 2659232 1076407 s 0 9 sprintf 04d 1 ge 明显地sprintf在 JavaS
  • 如何使用nodejs模块http2将http2与ExpressJS集成?

    我正在使用nodejs和express创建一个api 我想将http2与ExpressJS集成 这是我的代码 use strict const http2 require http2 const fs require fs const pa
  • 如何为 jenkins git 插件指定 ssh 密钥

    我正在使用git插件 https wiki jenkins ci org display JENKINS Git Plugin 据称它与 Jenkins 凭证管理功能 集成 凭证 使用 Jenkins 凭证管理功能连接到存储库的凭证 除非允
  • Heroku + Rails + PG:ActiveRecord::StatementInvalid (PG::ConnectionBad: PQconsumeInput() SSL 连接已意外关闭

    我的日志中经常随机收到以下错误 Nov 06 05 31 21 lmrapp app web 2 wbinternacional 0f0965e3 e537 4aed 8f3e 311a222e8fa1 PG ConnectionBad P
  • ngAnimate 1.4.7单元测试不调用动画函数

    我一直在工作本教程 http www sitepoint com angularjs testing tips bootstrap blocks routes events animations 并在谷歌上进行了令人作呕的搜索 但我似乎无法
  • 如何搜索多个pdf文件的内容?

    如何在目录 子目录中搜索 PDF 文件的内容 我正在寻找一些命令行工具 看起来grep无法搜索 PDF 文件 有pdfgrep http pdfgrep org 正如它的名字所暗示的那样 pdfgrep R a pattern to sea
  • Python 截图 2+ 显示器(windows)

    如果连接到多个显示器 如何使用 python 进行屏幕截图 I tried import sys from PyQt4 QtGui import QPixmap QApplication app QApplication sys argv
  • 调整 NSWindow 大小以适合子 NSView

    我有一个空的主 NSWindow 和 5 个 NSView NSView 有不同的按钮和标签等 并且窗口是空的 显示的第一个视图是一个菜单 链接到其他视图并返回 这工作正常并且视图切换得很好 但是 如果 NSWindow 具有一定大小 并且
  • 从 HTML 输入返回应用程序脚本中的数据

    我正在尝试使用 HTMLService 从 google 电子表格启动 html 表单 并将数据从选择输入返回到脚本 我正在用这一行收集数据 但我不确定如何将其返回到脚本文件 我尝试了以下各种迭代 城市 form Projects list