如何使用Google表格脚本编辑器制作表单 [英] How to make a form using Google Sheets script editor

查看:200
本文介绍了如何使用Google表格脚本编辑器制作表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个包含3列的Google工作表(A列:名称,B列:性别,C列:电子邮件)

I need to create a Google sheet with 3 columns (column A: Name, column B: Gender and column C: E-mail)

然后,我需要向工作表中添加脚本以制作表格(出于某些原因,我无法使用google表单),其中包含我们刚刚在工作表中添加的三个相关问题

Then I need to add a script to the sheet to make a form (For some reasons I can not use google forms) with the three related questions we just added in the sheet

我可以执行form.html代码,但是我不太熟悉JavaScript,一旦提交就将表单连接到工作表上

I can do the form.html code but I am not so much familiar with JavaScript to connect the form to the sheet once submitted

我认为是这样的:

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

function update spreadsheet {
  var sheet = "get active spreadsheet"
  ...

我无法完成上述代码,有人可以帮我吗?

I am not able to complete the above code, can anybody can help me with this?

推荐答案

您可以将Apps脚本部署为Web App [1].您需要创建一个html文件[2],在其中放置所需的表单.这里[3]很好地解释了如何使用html中的JavaScript执行Apps脚本功能.最后,在Apps脚本函数中,可以使用SpreadsheetApp类插入所需的值[4].

You can deploy Apps Script as a Web App [1]. You'll need to create a html file [2] in which you'll put the form you want. Here [3] is well explain how to execute Apps Script functions with JavaScript in the html. Finally, in an Apps Script function you can use the SpreadsheetApp class to insert the values you want [4].

这将是在文档中找到的示例代码:

This would be an example code found on the documentation:

Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      // Prevent forms from submitting.
      function preventFormSubmit() {
        var forms = document.querySelectorAll('form');
        for (var i = 0; i < forms.length; i++) {
          forms[i].addEventListener('submit', function(event) {
            event.preventDefault();
          });
        }
      }
      window.addEventListener('load', preventFormSubmit);

      function handleFormSubmit(formObject) {
        google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
      }
      function updateUrl(url) {
        var div = document.getElementById('output');
        div.innerHTML = '<a href="' + url + '">Got it!</a>';
      }
    </script>
  </head>
  <body>
    <form id="myForm" onsubmit="handleFormSubmit(this)">
      <input name="myFile" type="file" />
      <input type="submit" value="Submit" />
    </form>
    <div id="output"></div>
 </body>
</html>

code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function processForm(formObject) {
  var formBlob = formObject.myFile;
  var driveFile = DriveApp.createFile(formBlob);
  return driveFile.getUrl();
}

[1] https://developers.google.com/apps-script/指南/网络

[2] https://developers.google.com/apps-script /guides/html/

[3] https://developers.google.com/apps-脚本/指南/html/通讯

[4] https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

这篇关于如何使用Google表格脚本编辑器制作表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆