1

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

2

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 ! ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the doPost explanation. The issue now is that I'm using the UI service to generate an app. Right now I have a very basic script. Maybe you can help me figure whats wrong. ill update my question -- second thought I just realized I needed to add a Form element.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.