I have a small html form with some options. Based on the user's selection, currently the button click will result in the downloading of a csv file with the relevant information. What I ideally like to do is to be able to generate programatically a Google Spreadsheet from that csv file when the user requests it via the form.
I couldn't find this info. Will I need Spreadsheet's APIs for this or is there a simpler way?
Many thanks!
Yes, you need to use the Google Documents List Data API. Which will require that you make and authenticated POST call to this API with the appropriate parameters (described on the link). For Java and .NET Google developed some libraries that makes this job much easier, but you can call the API from any app/lang, just take a look at the "protocol" explanations on the documentation.
A think a simpler way would be to use Google Apps Script, a server-side javascript built into Google Spreadsheets and Sites. It's possible to write an interface to look like your html form. You'll have the advantage of it being highly integrated with Google spreadsheets. With 2 lines of code (literally) you can create a new spreadsheet with you csv values.
Related
Im use this Google ApiClient https://developers.google.cn/apps-script/api/quickstart/php
To pull Form & Responses, but same the api just add script to script.google
Please show me example to get the list form and Responses
Thanks
As of this writing there are no explicit REST APIs to pull Google Form data, so you won't be able to pull form responses directly from the Google API PHP client.
It you want to access form responses from PHP you'll need to use App Script to create a custom intermediary service/API that exposes the information you want.
You're already aware of the Apps Script API, so you can try creating a series of custom Apps Script functions to pull form responses and then call the Apps Script API from PHP to execute those functions to return the requisite data.
None of this is trivial to implement, and I don't have any code that I can share. But I'd start with reading the Apps Script API documentation and work out a solution from there.
There are plenty of examples on how to use an apps script web app to connect HTML forms to google sheets. However all these refer to a scenario where the owner of the spreadsheet/form is the developer, such as this method. The owner/creator of the spreadsheet/form is not dynamic in these cases as far as I can see.
In my use case I want users to create their own HTML Forms based off data from a spreadsheet in their drive and receive responses in that sheet. So we are talking about multiple users creating their own spreadsheet in their own google account/drive instance and connecting that to my external web app to display the form to the users who have the link.
The flow is like this:
User1 creates Google SpreadSheet in their drive
User1 uses our add on to create the form/link
User1 sends the links out
Receiving Users go to the link which leads to my external web app to fill out the form and submit a response
User1 receives responses in another sheet in the same spreadsheet in their drive.
This is repeated for x amount of users.
I'm thinking this will be done through a sheet add on, but I'm not sure how to connect the add on to the external web app. In the link above it is done by creating an apps script web app, but I can't ask the user to do that/programmatically do it for them.
What options do I have?
There needs to be a url to send data to, with a process listening for said data that I can then run a function on.
Hopefully there is some api for this, maybe for drive?
P.S In case anyone is suggesting that I use google forms for this, google forms is great but it is limited in UI options and to a question and answer format. I'm looking to include search/filter functionality for possibly hundreds of list items with a quantity field. So it can't be done in forms.
This should be possible given the following constraints: 1. The form "owner" would need to pass the Sheet IDs of the source and destination to you and, 2. both of those Sheets would need to be publicly readable and writeable.
AppScript is great for manipulating Docs and Sheets you own, but in your case you'll want to use the core Sheets API instead.
Your application which handles responses is probably best implemented using one of the SDKs depending on which language you write it in.
I was searching for an SDK or API to create and/or edit Google Docs files, but (correct me if I'm wrong) it seems Google Apps Script is the way to integrate your app with G Suite. I'm now trying to grasp the basic workflow for a basic document editing operation.
For instance, say we have a Quote Template document that serves as the template for our business to create quotations for customers. From within our web app system, we would press a button that would duplicate that file with a new name and maybe replace some variables.
Now, here's where I am in a gray area. I'd think we would have to install some sort of SDK (PHP/Js/Ruby library) in our project, that would let us create a new file based on the template, and call some replacePattern(regex, value) method.
However, the more I read, it looks like the approach is different, where you would create a Google Apps Script and, from our web app, we make HTTP calls to a REST API that will run said script. Then, this script would do the creating document and replacing values.
Am I correct in assuming this is the basic workflow to integrate our web app with G Suite products, or am I missing something essential?
Yes, that would be the basic workflow. There is no public Google Docs REST API, so apps script at the moment is the only way to automate docs. If you want to integrate it with a current tool you can expose the script using the Execution API. There are client libraries, though being a REST API they can be accessed from anywhere you can make a http request. Links to the client libraries can be found in their respective quick starts found below:
https://developers.google.com/apps-script/guides/rest/
All of the business logic can be handled by the script such as copying a template, replacing values, and setting permissions to the file.
The DocumentApp service docs can be found at:
https://developers.google.com/apps-script/reference/document/
I want to create an app which helps newbie employers to take some simple decisions. So, using their answers to a form, the app asks new questions based on previous responses, something similar to a decision tree. Is Google Form a good alternative to implement this? (something like this suggested here, but with more questions:
Are there alternatives based on Google Apps Script?
What you need is a Google Form with a lot of questions, whose answers redirect to different pages in the Form.
You can also use Google Apps Script Forms Service to create Forms through code (and you can use previous responses in old Forms to generate new content for new Forms dynamically).
A typical example of this would be:
Create a Google Form with several questions with redirects based on answers (decision trees).
Have the responses to that form go to a response Spreadsheet.
Run a script on the response Spreadsheet to generate new content using the Forms Service within Google Apps Script (or do this manually; both work).
Send out the new Form to employees by email (you can use Gmail Service to accomplish this) or however you prefer.
--> and repeat the process as needed.
At my work, I use Google Forms with redirects and scripts on the response Spreadsheets in order to process faculty requests using Google Apps Script, so this is definitely possible!
However, it should be noted that Google Apps Script cannot be used to dynamically generate content for Forms while the user is filling it out. See my question here.
I don't feel like typing a mass of code here, but there are two ways of accomplishing this (both of which would require a lot of script code):
You could potentially accomplish this by making a web app that automatically dumps the responses into a spreadsheet (limited space) or a document (which doesn't have to be human-readable). This won't give you the same UI, but it would work, assuming you are familiar with making web apps in the first place.
You could make a multi-page form that you could write some code to direct the person upon selection of whatever choice they make to go to the correct page. This other question may serve of use if you want to go this route (it likely will be a bit easier).
After reading up a lot on the Google Spreadsheet API I have come to the conclusion that formatting (such as merging cells, changing fonts etc) is only available throught the Apps scripts.
Since we need to create and fill the spreadsheets with data programatically using Java on the back-end I guess I need to somehow either;
link the new sheet to a Apps script that trigger on-load or
create a Apps script that creates the spreadsheet for me.
Anyone knows?
If you want to just "create" the spreadsheet, you don't need a script to load whenever it spreadsheet is opened. It's probably easier to develop a script that runs once and create the spreadsheet for you.
Another tip is to have a template file that you can copy with most of the formatting (if not all) already there. Possibly pending just little things that are related to the real data the new spreadsheet will have.
Edit to answer the question in the title.
No, you can not add a script to an existing spreadsheet programatically, only manually. What you can do is previously set up a template spreadsheet with a script in it and create new spreadsheets by copying this template.
(answering the comment)
You can run a script programatically, but not upload it. To run a script you can deploy it as a web-app and call its url with either a http get or post (will call its doGet or doPost functions, that you must have declared). Also, you could set this script to run on form submit of any spreadsheet-form and just submit a set of answers to this form. At last (that I can think of now) you could just add the script as a library in another Apps Script and call it directly.
(Aug 2016) There is no way programmatic way to link a Google Sheet and Apps Script code other than manually. Based on what it seems you want ("create and fill the spreadsheets with data programatically using Java"), you can now do it without Apps Script.
TL;DR: Above, #Henrique has answered multiple questions and even questions that weren't asked! The good news is that today, we have more answers representing alternate possible solutions to what you're seeking.
It's now possible to "upload" Apps Script code programmatically with the
import/export system, say with Eclipse since you're a Java developer (2013 announcement).
I agree with Henrique's suggestion that if you create a spreadsheet
template, i.e., Excel file, you can use the Google Drive API to
programmatically import/create identical Google Sheets with all your
desired formatting.
"Formatting (such as merging cells, changing
fonts etc)" can now be done outside of Apps Script, as there is a
"new" Google Sheets API v4 (not GData).
In order to use the new API, you need to get the Google APIs Client Library for Java and use the latest Sheets API, which is much more powerful and flexible than any previous API. Here's one code sample to help get you started. If you're not "allergic" to Python, I also made a video with a different, slightly longer example introducing the new API and gave a deeper dive into its code via a blogpost that you can learn from.
Note the v4 API allows you to create spreadsheets & sheets, upload & download data, as well as, in the general sense, programmatically access a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.), but to perform file-level access such as uploads & downloads, imports & exports (same as uploads & downloads but conversion to/from Google Apps formats), you would use the Drive API instead.