How do you post form submissions to a Google Drive Spreadsheet using Google App Scripts

醉酒当歌 提交于 2021-02-18 19:01:06

问题


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?

sample form

<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>

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

| color | flavor |
   red    vanilla

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


UPDATE

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

This is the current script I've thrown together... and it works!!!

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;
}

回答1:


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.

In the doPost function, you can access inputs with their 'name' attribute like that : e.parameter.color and 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]);.

Let's recap :

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

Hope it helps ! ;)



来源:https://stackoverflow.com/questions/11976777/how-do-you-post-form-submissions-to-a-google-drive-spreadsheet-using-google-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!