Scripting Google documents (not spreadsheets) with Google Apps Script - google-apps-script

So I know that it's possible to create scripts for use on spreadsheets using Google Apps Script, but is there support for using created scripts on documents? I know that the API provides services for creating and editing documents, though they bear the experimental tag, but I can't seem to find a way to create and install a script on a document.
And when I say document services, I refer to the following link: https://developers.google.com/apps-script/service_document

Update: you can do this now. Just in case someone stumbles on this answer. To clarify the above answer, too: you can create scripts that are attached to documents or standalone scripts. Attached scripts can be published as add-ons, but are always associated with a document (doc, sheet, or form). Standalone scripts can be published as web apps.

You can't find it because you cannot do it. Scripts can indeed manipulate documents using Document services (as you referred to) but the script itself should be linked to a spreadsheet or a Google site. Linked is probably not the best word since the apps script can be published to work as a standalone web application - in this case the link to the spreadsheet is used only during the development of your application.
I hope this makes things clear enough.

Related

One question survey app Google Apps Script and Google Sheets with user management

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.

Can I have captcha in google apps script?

I'm deploying an aplicattion in Google Apps Script, and I need the implementation of a captcha on it, how can I include captcha in Google App Script?
Know it's an old question, but had the same issue recently.
In short, Yes, and you can add reCaptcha.
See the readme and sample files and then adapt to your needs.
Code.gs has the basics you need to only have to use javascript as oppose to php for responses.
Yes, you can.
Here is a tutorial I posted today on my website.
reCAPCHA with Google Apps Script
The tricky thing is the domain name when you register for a Website to use reCAPTCHA. Since Apps Script webapps are hosted in an iFrame, you need to use domain of iFrame. If you do an analysis of Apps Script Webapp, you will find that all the apps have an iFrame on a temporary domain like xsdsdfjsdfks.googleusercontent.com. So you need to resgister domain googleusercontent.com in order to get a site-key and secret-key pair.

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.

Sharing Google Docs content-bounded scripts

I have created some content bounded scripts (Tools->Script Editor) for private use. The scripts add a sidebar, and use a timer to copy the content from the sidebar to a Google document at the cursor position. It is important that the insertion happens at the cursor.
I want to share these scripts with others, as well as add them to old documents. All suggestions I have seen so far involves creating a template document with these scripts and then sharing the template document. This option is not feasible for me. One of the concerns is, these scripts need to be added to existing documents. Creating a new document, and merging it with the old one is not working (the original documents are complex, and migrated documents loose formatting).
Is there an easy way to share/insert these scripts? So far, I have failed to implement any of the below obvious options:
Export Script Editor project to somewhere, and then import it in another document.
Create an app script in drive, and then import it in an existing document.
Publish this as add-on (N/A since this is not a public project, and not complete yet either).
I'm not sure if it is feasible to publish my scripts as a webapp, then write a simple container-bound application to call my webapp with document id as suggested at Deploying container-bound Google Apps Script as Web App
However, this requires webapp to insert the text at cursor position of the active window. I doubt that is possible.
I'm hoping that somebody found an easier way, and willing to share it.
I appreciate any help.
Sincerely,
Converting your script to an add-on is the recommended way to distribute across multiple users and documents. We understand this solution doesn't always work when the script is not meant for general consumption. Add-ons can be published only to a single domain however, so if all of your users are within a Google Apps domain then add-ons may still be an option.

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?