Google App Script Make text within a document readonly? - google-apps-script

I have a library that is adding data to a shared document. Is it possible to keep the document editable save for specific word patterns? For example every text between two * * can't be modified by users who aren't the owner?

I Google Document in no way to lock part of document. It is possible in SpreadSheets only (known as protected ranges).
Bus is here different way to get similar effect. You can share the Document as read-only (view only) and write Google Script Web App what provide custom editor interface to User. The script will have set Execute the app as permission to You. That mean, only You and your Script can edit the Document and in Script you can write write custom protection.

Related

Script to detect user copying - event?

Can you detect a user copying in the google sheet script? If so how? I want to create an event that will check something if the user copies the cell.
Yes Cooper is right, there is no onCopy event in Apps Script. Not to mention, that Apps Script and Drive environment are separated so this means that you are unable to add/inject an eventListener using Javascript in order to prevent the selection (document doesn't exist in Apps Script side). Also, the copy method for drive cells doesn't rely on the selection (In order to use .preventDefault()) method. I have tested these paths on my end.
As workaround:
You can get the data from the sheet.
Display it in a HTML page "within" the same Apps Script project. https://developers.google.com/apps-script/guides/html/
Within this HTML page you can use JavaScript (and import a library using a CDN) and CSS in order to prevent the copy/selection of the data.

How to run a google script from a google doc?

I have copied a script to remove empty paragraphs from a google doc & I can run it from the script editor. It says in the script it will operate on the active document but I don't know what that is and don't want to just run it without knowing what doc it will execute on if any. How can I run this from the currently selected document? Is this possible?
There are 2 ways you can use a script (follow the links to read up on these things):
If you open the script editor from the document it means that your script is attached to that document and it's called "container-bound"
If you create a standalone script file in https://script.google.com/home (or just your Google Drive) then there is no such thing as ActiveDocument as it's not container-bound.
You can use openById() or openByURL() to specify a document that you have access to, however, you need to specify an ID or URL (the ID is the last string in the URL) so the script cannot take a random document and edit it unless it accesses your Google Drive data and looks for a file by name. If it works just with the Google Doc it will not need access to your Google Drive and before you run the script it will request access to different things just like in Android.

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.

Creating a new document on google drive through the API

Is it possible to create a new document (either using a template or completely dynamic) using the Google Drive APIs? I am working with a client that requires generation of word documents. I tired looking up but I couldn't find sufficient documentation on how to "create and format" documents using the APIs.
For example, can I include the client's company logo on the top of the doc programmatically? I'd like whatever I am doing for this client to be generic (i.e including logo at the top etc) to be dynamic, so I can re-purpose this for other clients as well.
I also am having difficulty finding documentation on any kind of formatting we could do on the documents (bold, italic, new paragraph) etc
The Google Drive API only deals with whole file operations. It has no understanding of the content of the files, including formatting.
You have two options:-
Use Apps Script (ie. not the Drive API) which has document manipulation features. See https://developers.google.com/apps-script/reference/document/
Create your templates in HTML (or Word, OpenOffice), then upload them to a new file, setting the option to 'convert to google docs'
It is possible to create new documents as well as make a copy of already existing documents (templates)
Some time ago I've created simple example how to work with templates:
On your Google drive create folder Templates
Inside this folder create formated document with images, texts etc... Replace dynamic values with {A},{B},{C} representing spreadsheet column names in this document
Than create spreadsheet and fill some data in it. Inside this spreadsheet navigate to Tools->Script gallery and search for fast template generator -> Install it
Close document and re-open it. Script will automatically execute onOpen function which will add Template generator to spreadsheet menu. Now just choose row from which you would like to populate values in template and launch Template generator-> Generate from template.
A new document with filled values will be generated from chosen template.
You can find source code and customize it in according your need in Tools->Script editor in this spreadsheet, so result should be in PDF or sent via email etc...
EDIT: Seems this approach does not work, as Google Drive API only allows copying files across current user's Drive.
I found for me suitable approach was to create a document in my own Google Drive. Style it accordingly, add base text etc. and give it read-only permission to everyone. Then from the Google Drive API use copy file functionality and copy it into the current users Drive.

Security issue concerning apps scripts inserted in google site

I inserted a google apps script as a gadget in a google Site. This GAS implements a page on html service and is intended to capture user data and store it in the ScriptDb of another script. Playing around, I noticed that viewing the source code of the google site any user could access directly to the GAS via an url displayed on the source of the Google site. I followed this link, and there was the GAS! From this link, I tried to save new data to ScriptDb, and I found that fortunately this was not possible. Doesn't this behaviour represent a security issue? Can I be sure that It is not possible to modify the ScriptDb data from this embedded link?
The ScritpDb your script creates can only be accessed accessed by that script's code - just because you have the URL to the published script or even if you have code in some form DOES NOT give you access to the ScriptDb.
However, lets say if in your doGet if you are blindly dumping out all the data to HTML output if a certain predictable parameter is passed in, then that is bad. But that would be considered a programming error.
So in short - if your ScriptDb is properly wrapped with the appropriate Apps Script, then access to that ScriptDb is secure.
I can clarify more if you share some code around what you worry might be insecure.