您如何使用Google App脚本将表单提交的内容发布到Google云端硬盘电子表格 [英] How do you post form submissions to a Google Drive Spreadsheet using Google App Scripts

查看:72
本文介绍了您如何使用Google App脚本将表单提交的内容发布到Google云端硬盘电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站上创建一个表单,当用户提交表单时,该表单会将其数据发布到我的Google云端硬盘中的特定电子表格.如何使用Google App脚本执行此操作?

I want to create a form on my site that when a user submits, it posts their data to a particular Spreadsheet in my Google Drive. How can I do this with Google App Scripts?

样本表格

<form method='post' action='https://script.google.com/macros/s/xxxx.....'>
  Favorite Color <input type='text' value='' name='color' />
  Favorite Ice Cream Flavor <input type='text' value='' name='flavor' />
  <input type='button' value='submit' />
</form>

以便我点击提交时在Google云端硬盘电子表格中创建一条记录

so that when I hit submit it creates a record in a Google Drive Spreadsheet

| color | flavor |
   red    vanilla

这与GAS兼容吗?还是更适合Google Drive SDK(通过Javascript)执行的任务?

Is this doable with GAS, or is sort of task more suited for the Google Drive SDK (via Javascript)?

更新

使用了如何将表单元素添加到UI App Service 中的示例完成脚本

Used the example from How to add form elements to UI App Service to complete the script

这是我目前一起使用的当前脚本...并且可以正常工作!!!

var SPREADSHEET_ID = '0Aqa6DbU_0sv7dGNrMEEybjNrdm00MlpwTTNx...';

function doPost(e) {
  var app = UiApp.getActiveApplication();

  SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName('RequestInvites').appendRow([e.parameter.emailAddress, 'hash123']);

  app.add(app.createLabel("Form submitted. Your email address is: '" + e.parameter.emailAddress));

  return app;
}

function createForm(e){
   var app  = UiApp.createApplication();
   var form = app.createFormPanel();
   var flow = app.createFlowPanel();

   flow.add(app.createLabel('Enter your email address to get an invitation').setId('inviteLabel'));
   flow.add(app.createTextBox().setId('emailAddress').setName('emailAddress'));
   flow.add(app.createSubmitButton("Request Invite"));
   form.add(flow);

   app.add(form);
   return app;
}

推荐答案

您可以使用GAS做到这一点.在您的脚本中,单击提交"按钮(您可能已经忘记了;)后,请使用 function doPost(e)函数检索用户输入.

You can do that with GAS. In your script, use function doPost(e) to retrieve user inputs when the submit button (that you might have forgotten ;) is cliked.

在doPost函数中,您可以使用其名称"属性访问输入,例如: e.parameter.color e.parameter.flavor .

In the doPost function, you can access inputs with their 'name' attribute like that : e.parameter.color and e.parameter.flavor.

然后,您可以使用Spreadsheet服务在电子表格中进行编写.该服务的文档位于此处.因此,您打开电子表格,然后在正确的表格中添加一行,如下所示: SpreadsheetApp.openById('电子表格的ID').getSheetByName('colors_and_flavors').appendRow([e.parameter.color,e.parameter.flavor]); .

Then, you can use Spreadsheet service to write in your spreadsheet. Documentation for this service is here. So you open your spreadsheet, and add a row in the correct sheet like that : SpreadsheetApp.openById('id of your spreadsheet').getSheetByName('colors_and_flavors').appendRow([e.parameter.color, e.parameter.flavor]);.

让我们回顾一下:

function doPost(e) {
  SpreadsheetApp.openById('id of your spreadsheet').getSheetByName('colors_and_flavors').appendRow([e.parameter.color, e.parameter.flavor]);
}

希望有帮助!;)

这篇关于您如何使用Google App脚本将表单提交的内容发布到Google云端硬盘电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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