One question survey app Google Apps Script and Google Sheets with user management - google-apps-script

I have to do a little bit of "crowdsourcing" for my work and it would consist in a very simple web app where a user can register/log in, and then be taken to a page where a picture is shown to a user and submit a number.
I'd like to be able to set up a few rules to choose which picture the user will see (so he won't have to answer twice the same question and also to allow some overlap between users to compare their answers).
We can assume that I have a google sheet with a list of images URLs that can be accessed and that I would like the answers to be populated there.
I figured that this tutorial would be a good start since it handles user creation and management and user can submit ideas :
Creating a CRUD Web App with Google Sheets
That being said I'm a bit clueless, I've tried to look for sample scripts deployed as web app but it's hard to know where to start.
I'll appreciate any help !

Google Apps Script is, basically, a javascript environment with a set of libraries that interact with the Google Apps. For instance, to read or store information from/to Google Sheets or Google Docs. If you know javascript, you can create simple applications there. In addition, you can create plugins for Google Applications if you are interested.
A very simple example
Suppose you wanna display an HTML page. You can create an stand-alone script (an script not-bound to a G suite application) and use the content or the HTML services.
You can create a function that sends a simple message to the browser using the ContentService:
function doGet() {
return ContentService.createTextOutput('Hello, world!');
}
Or you can create a function that sends an HTML file in the same project using the HtmlService:
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
Once you have created the function, you must deploy the script as a web application.
Save the script as a new version doing File > Manage Versions and Save new version.
Then, publish the app using Publish > Deploy as web app and provinding information about the permissions for the application.
After Google publishes the application, it gives you an URL to access the application,
You may check more in a simple tutorial on producing content with Google Apps Script. In addition, you may check the google guide to use the HtmlService to provide templated content, i.e. where the HTML are templates which data is provided by javascript functions and variables.
Getting started with Google Apps Script
To start, you may check some tutorials in the internet:
Google has some tutorials and a series of Youtube videos.
In addition, you may find multiple resources for Google App Scripts that may help you:
Google has a list of sample projects
Tanakeich has a list of resources for taking advantage of Google Apps Script.
Oshliaer has another list of resources.
There are Google Codelab (tutorials) for Apps Scripts. They include a lab for using Google Sheets as a reporting tool and for creating a Hangouts Chat bot with Apps Script
Developing Google Apps Script projects locally
Although Google Apps Script provides a web interface to create your applications (your scripts), a better idea may be to create the software locally, using more-traditional tools for Javascript.
Clasp is a command-line tool to develop locally Google Apps Script projects.
Google has a codelab that teach you how to use clasp.

Related

Where can i see the analytics of an Google App Script Deployment?

I created a small app on top of a spreadsheet (with GAS and HTML, CSS) and I deployed it.
Users can access to it without having to enter in the spreadsheet.
It works really well but i'm not able to see even the basic analytics (for eg. the number of viewers)
Thanks
Go to the project overview page. In the Google Apps Script web IDE, on the lefmost sidepanel click on Overview.
Also, if you have starred your project, go to https://script.google.com/home/starred
Rather than "viewers" you will see "users". If you have set your web-app as execute as you by anyone even anonymous, you will see only one user, you, as this page show the users that exectuted the scripts like the doGet function and the server-side functions called through google.script.run.
Note: https://script.google.com keep execution logs for the last 7 days. If you need to keep the logs longer you have to use another place to keep these logs, i.e. Cloud Logging (requires a Google Cloud standard project), Google Sheets spreadsheet.
Resources
https://developers.google.com/apps-script/guides/logging
Related
Effective way to debug a Google Apps Script Web App

Workflow for integrating Google Docs with my web app through Google Apps Script

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/

HTMLService or Google Apps script

I've use Google Apps Script before, but haven't bene programming for oabout a year now.
For a new project I would like to know if I should continue using Google Apps script or switch to HTMLService.
So first question is if Google Apps Script still is and well be supported by Google.
Second question is what are the main differences between HTMLService and Google Apps Script that make it worth switching.
If you mean should you use HTMLService or UIService within Google Apps Script, it would probably be best to use HTMLService as UIService is deprecated.
UIService will appear in auto-complete until 30th June, but not after.
https://developers.google.com/apps-script/sunset?utm_campaign=htmlser-1214&utm_source=gadbc&utm_medium=blog
http://googleappsdeveloper.blogspot.co.uk/2014/12/speeding-up-htmlservice.html
https://developers.google.com/apps-script/guides/html/
For first Question: Yes, The Google app script used to make google products as custom like google spread sheet , google docs and we can insert the script into google sites so that we can share output through website.
For second Question: In Google app script the ClassUIApp is deprecated in that place we can using HTML services
https://developers.google.com/apps-script/reference/ui/ui-app
In the beginning Google App Script, all the code was written in one file one with .gs extension which included the code for User Interface, styling and business logic which made it complex as the application moved from small scale to enterprise level.
Also there was a restriction that you have to use the UI components from App Script (UiApp) which was not welcomed by the existing community of web developers as it was limited and needed to be learned and could not offer all the features a programmer may require.
Therefore in order to make it popular among existing community of developers App Script gave the ability of writing HTML pages as part of the project which was very helpful as it is widely used for designing all kinds of UI.
So, as you can see here that Google has made a shift, and hence HTMLservice is the future.
Google Apps Script was design for interact with google apps, so, if you are working with google drive, calendar, maps. You must use apps script, It's is easy and fast to implement.
But if you don't need use Google Apps may be is better use another software because how I told you before, Apps Script was created for interact with Google Apps and works awesome.
Google say: "The HTML service lets you serve web pages that can interact with server-side Apps Script functions. It is particularly useful for building web apps or adding custom user interfaces in Google Docs, Sheets, and Forms. You can even use it to generate the body of an email."
https://developers.google.com/apps-script/guides/html/
The purpose is different, what do you want to do?
Been following updates from Google in regard to GAS for over 6 months now. Google is putting a lot of resource into GAS and I don't think there should be any concern about it being deprecated.
And HTMLService is a class that can be called from inside the script editor so I don't know why one would need to choose one or the other.
Just go into the script editor and press control + space bar at the same time and you'll see all the classes available to you in alphabetical order. Scroll down to H and you'll see HTMLService. I haven't used it yet but it's there to work with.
HTMLService is a service to serve HTML pages through Google Apps Script. You can use HtmlService as a normal web page.

How to add a Google Apps Script web app as a client in Google API console

I have an app that consists of two parts
an installed application (more specifically: a browser add-on) that runs on the user's machine, and
a web app implemented as a stand-alone Google Apps Script app using HtmlService.
The two parts talk to each other and both access some Google APIs on behalf of the user via Oauth. To do this, I set up an API project in the Google API console. In the section "API Access", I created a client of type "Installed Application" for the browser add-on. For the Apps Script part, Google takes care of the authorization flow automatically.
My problem is that Google does not know that these two clients are part of the same project. Therefore, the user has to go through two authorization steps in a row when executing my app for the first time (first, the Oauth process for the browser add-on and then the automatically generated authorization process from Google Apps Script). This is very confusing to the user, especially, since the splitting between the browser add-on and the web app is rather an implementational detail about which the user shouldn't really have to bother.
Is there a way to unify the two authorization flows? I know that one can add several distinct clients to a single project in the Google API console. It seems to me like this feature is explicitly intended for scenarios similar to mine, where more than one application make up what is perceived as a single service by the user. Unfortunately, web apps that are implemented in Google Apps script don't seem to be compatible with the Google API console because of the way Apps Script generates the authorization flow automatically. Or am I missing an option in the API console or in the Apps Script editor?
For future reference: I found out that Google automatically creates an API project in the Google API console for each Google Apps Script project. This seems to be a fairly recent feature. One difficulty for me was that these API projects don't show up when one goes to the API console, clicks on the name of whatever project is currently opened in the upper left corner and chooses "open". Apparently, the only way to reach the auto-generated API project is as follows:
Go to drive.google.com and open the Google Apps Script project.
Chose "Resources --> Use Google APIs...".
In the popup, click the link to the Google API console.
In principle, this should allow to add additional clients to the project, as long as none of the other clients is also a Google Apps Script project. I haven't been able to test this since it still doesn't solve my specific problem: Sending e-mails from the user's Gmail account seems to be a feature unique to Google Apps Script and I couldn't find an Oauth2 scope for this operation. Therefore, I still cannot ask for all required permissions in a single authorization flow. But if anyone who's reading this has a similar problem with different Oauth2 scopes, the above instructions should fix it for you.
Anyway, I solved my problem in the meantime by no longer using Google Drive to upload files.

How to use Google Apps Script on any/all Spreadsheets?

I am trying to implement a spreadsheet app using Google Apps Script. But I have several issue that does not mach my requirements. I have looked in to these issues and was unsuccessful to find answers or better alternative ways to implement this functionality.
Requirements:
Write manageable testable code
Not to download the script to each spreadsheet but to somehow when you install once you can use it on any spreadsheet in the drive.
1,2 are the problems I faced.
The Google Apps Script IDE seems to have difficulty in handling a large code base (I mean not to keep the code its about managing and handle the growth of the code)
This is the major problem (Not matching the requirement)
Let's say I create a script for a spreadsheet and then I deploy it and then I can then install the script on any other script I like and then execute it. But this has to be done for each and every spreadsheet. The installed script is not in the script manager to be used in all the spreadsheets without installing the script form the gallery every time.
And also When I create a project of Spreadsheets directly on the Google Drive. Since this is not allocated to a particular spreadsheet it does not allow me to select it from the dive (From script manager) and load this script to a spreadsheet I wish, and run/debug it.
I am not hoping to get a better solution to above issue 1.
But issue No. 2 is is the burning one,
Is this can be addressed to match the requirement?
If the requirement can not be achieved form Google Apps Script, I have to go for an alternative.
Alternatives I thought so far:
I thought about desktop application that can give the spreadsheet URL and then give the authentication information and by using the spreadsheet API do the necessary manipulations form the desktop application. This is the dummy est approach which is not so user friendly.
Go for a Google app, where you can log in using the G mail authentication and then after giving the spreadsheet URL using the Spreadsheet API do the necessary manipulations. But I am not sure about the authentication process for a Google app and the possibility of access permission to the drive.
Final is the Crome app, Where Crome is provided with the necessary authentication to access a given spreadsheet URL and do the necessary manipulations to the spreadsheet.
Which of those options is preferred, or is there any better solution that matches the requirement I stated above?
The definition of "large code base" is subjective, but even a project with several hundred lines of code can benefit from organization. In the IDE you have two primary tools available to help organize your code; Files and Libraries.
This answer is mainly a collection of links to other answers, within a number of topic areas that should address the concerns you've expressed.
Code Organization & Libraries
A script project can have multiple files, of type "gs" (script) or "html". The namespace extends across all script files in the project. Separating methods into files is a simple way to organize your code within a project (for instance, place all tests in one file separate from production code), although it does very little for reusability across projects.
Libraries are projects that are published to enable other projects to incorporate their capabilities without requiring them to copy the actual code. Have a look at the Notable Script Libraries for a start. Libraries take their name space from the library name - for instance, I have a library of utilities related to input & output of CSV files, which I (cleverly) named CSVUtils. The methods in that library are accessed as CSVUtils.example(), and are supported by the auto-complete feature. Libraries support a limited subset of jsdoc, and when published provide a link to the generated documentation.
Organizing Spreadsheet Code in several *.gs files - even possible?
Is it possible to have one script for multiple spreadsheets?
How, or where, do I define a function for use in more than one spreadsheet?
google-apps-script have multiple scripts in one spreadsheet
Creating a namespace-like organization in a google apps script library
Publishing a Google Apps Script Library
Design patterns for libraries in GAS?
ScriptDb in Library accessed by WebApp
Project and multiple doGet() script files published as web apps -- Need clarification
... every SO question about Apps Script Libraries.
Using external JavaScript libraries:
Start by reading through Understanding Caja Sanitization, especially the section regarding External JavaScript Libraries.
Google Apps Script: How can use js library in HtmlService
Authorization & Ownership
Authorization - Google apps script grant user permissions access services on spreadsheet in ther first run without open script editor
https://stackoverflow.com/questions/15096379/google-app-scripts-how-to-let-shared-users-run-scripts-as-themselves
When does Google require re-authentication?
OAuth in Google Apps Scripts Libraries
Code Transparency
Source-code of Libraries used by WebApps visible to everybody?
How to hide library source code in Google way?