How to run a google script from a google doc? - google-apps-script

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.

Related

Do Google Apps Scripts have a Copy URL?

I'm wondering if there is a way to share a link to a Google Apps Script file that forces users to make a copy of it? Using Google Docs, you can append /copy to the url. For example:
https://docs.google.com/document/d/19yoQ71RcqNsYgPmZHzw96G_QH05hC20sWxk_ktahpXY/copy
This will force users to make a copy of the original document. I've been trying to do something similiar with a Google Apps Script file.
I've tried appending copy to the URL like this but it doesn't seem to work. As a potential work around, I could just use embedded scripts and share the Sheet/Doc/Form.
Anyone know of a URL parameter to force copying of a Google Apps Script file?
You can use this format to quickly make a copy of any public Google Script.
https://script.google.com/d/SCRIPT_ID/edit?newcopy=true
See example.
There is no way to do this since the new editor has been rolled, have reported it on this issue:
https://issuetracker.google.com/issues/198266388
You can manually make a copy on the new editor by going to the "Overview" tab, and on the top-right is the button to make a copy.

GAS - How to copy a Docs file with a bound Script and execute that script

I need to programmatically create a file based on a template Google Docs, and then process the document with a bound Script.
My initial approach was to create a bound script to the template file; then, using the google client library (for php, ruby, or w/e), copying the template into the actual document, and finally call the Script function on the newly copied document.
The problem is it seems you have to "publish" your bound App Script, and even after copying a document with a published bound script, the resulting file's script is not published.
Is there any way to copy the document and publish it's bound script directly from my PHP/Ruby/etc app?
PS: The reason I'm using bound script rather than just a script, it's because I need to store data using the PropertiesService.getDocumentProperties() in order to replace some variables with a "preview" function
I think there's a misconception about boundscripts. You call them boundscripts because
" it was created from that document " Tools -> Script Editor unlike standalone scripts which are created from script.google.com directly.
You don't have to publish boundscripts in order to use them. Those are called add-ons.
Now with regard to copying documents in Apps Script, try checking out copy() from DocumentApp. There's also a code snippet available in this SO thread with that demonstrates copyong which might give you insight.
Hope that helps.

Creating a Google Apps Script which Automatically Executes on Upload

I have an interesting fairly simple problem I am trying to solve but I am at a loss on the proper code to get this to working correctly.
Basically, I have an very old local application running which allow me to print a receipt of a transaction. I have the ability to enable this receipt to be printed to a specific file within a specific directory on my computer or I can set it up to print out automatically to a printer on my computer. Currently I print the data out to a file, then copy the contents of that file and paste it into a word document template and then manually go through and format the data so it looks clean (I should note that the data prints out in fixed field format requiring a specific font and size so its pasts correctly. After all this is done I then email the word or pdf document out.
Essentially what I am trying to do here is automate this method and I thought this should be possible using a Google Apps Script.
My thought here was that it should be possible to:
save all the original files from my application to a specific
directory on my computer
sync that folder with google drive
have a google apps script automatically execute upon upload
have the google apps script automatically use a specific document
template I create with a header/footer and then paste all the
contents from the new file which was uploaded
have the google apps script pattern match certain elements within the contents of
the document and properly format it.
save the document to a specific location with a specific file naming convention
automatically email the document as a doc or pdf based upon a
specific regex matched field within the source document.
Could anyone provide a specific code example which demonstrates how Google apps scripts can be utilized to monitor the contents of a specific Google drive folder to trigger an event immediately after a new file is uploaded?
Thanks in advance!

How to separate script editor from document (google apps script)

I'm writing some script for my Documents, Spreadsheets and Gmail. I would like to know if there is a way to save all my script to separate files in a specific google drive folder. I don't like having to go to the backend of my Documents and Spreadsheets in order to edit my script.
Would I have to call the document from my script using "getByID" or is there another way?
Yes, when you create a new document in Drive you can select Script. If you don't see the script option, try going here.
You will, however, need to call your document using getByID. There my be other complexities if you are using onEdit type triggers.
A lot more information can be found under the Types of Scripts heading at Google Apps Scripts Documentation.
You could go advanced and pull your script in behind the Document as a Library and then using the script behind the Doc to make the calls to the library, but that's a bigger discussion.

How do I access my app scripts?

I work for a university which uses Google App for EDU. Recently, I decided to begin testing app script to see how it could be of use to our organization. I have been able to create new app scripts and run them without issue. However, I have found that once I close the browser editor tab, I am not able to retrieve any script I've wrote unless I have the editor url saved (ie. I dont see any of my script in my google drive). Is there a particular app script site where my scripts are listed?
Thanks,
Bryan
The long method i use to get to see all my App Scripts is:
open an existing spreadsheet, or simply create a new spreadsheet
go to Extensions > Apps Script
click on the "Apps Script" logo in the top left corner
This will take you to: https://script.google.com/home
The short method is just to have that link (above) saved on browser toolbar and click it whenever i need to.
to be able to starts new script from the drive you have to connect script app to your drive using the Browser interface like below :
from the connect more apps at the bottom search for "script" and add it.
If you created scripts from inside a spreadsheet, they are tied to that sheet and will not show up in Drive; you can find them via the "Script Manager" menu in that spreadsheet.
All standalone scripts will be visible in your Google Drive. Did you write your script inside a spreadsheet or a Google Site ? If so, you have to open the spreadsheet or site to see your script.
If you indeed have a standalone script, make sure you click the 'All Items' on the left hand side of Google Drive or use search.
If you've created a new script from script.google.com and have saved it, it'll appear in Drive under the root My Drive folder.
If you created one from inside of Drive by clicking the red Create button then Script, it'll be in the folder which you last clicked on before clicking Create. If you don't remember what folder you created the script file under, click the Recent folder name to find it.
If you've started one inside a Form , Sheet, or Doc you'll have to remember where the Form, Sheet or Doc was located in Drive.
You can search all your scripts in Google Drive using app:"Google Apps Script" or by selecting Google Apps Script under Opens With from the drop down menu.
As pointed out by #Mandy, this assumes you have added Apps Script as a connect app. See answer from #Serge on how to do that.
Try the handy dashboard/central location for scripts:
https://script.google.com