Creating a Google Apps Script which Automatically Executes on Upload - google-apps-script

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!

Related

How to access and extract data in App Script that is not bound to the code

I am working on a script that is standalone. It collects its baseline data from a google sheet bound to a form. In the form submission, excel documents are uploaded. While I can access the link for the excel document(google downloads the excel into your drive upon submission and inserts the drive link into the google response sheet), I am having difficulty looking into and extracting anything (exact cell values, ranges, indexes, etc) from the excel doc.
I have tried using various functions from the sheets and spreadsheets classes and continue to get errors. Some suggestions I've found say the excel document needs to be converted into a google sheet before the app script can access it, or that app script won't allow you to work with such a document if it is not bound to your script (since that allows you to activate it)(I can't bind the excel doc because it changes upon every new submission of the form).
Has anyone ever compiled a similar code and figured out how to access an unbound, non-google doc?
Let me know if you need error codes or script snippets. I just wasn't sure if this was a syntax problem or a google suite trick spot that needed extra code that I probably haven't found cause I'm new to this platform.
From the question
Has anyone ever compiled a similar code and figured out how to access an unbound, non-google doc?
Scripts in Google Apps Script aren't "compiled" by the script writer in the sense that it's done when developing something in other platforms.
To get data from a file hosted in Google Drive, first the script should get that file by using the Drive Service (Class DriveApp) or the Drive Advanced Service.
The next part depends pretty much on the Excel file format. If it's an xlsx file, then usually the most convenient is to convert the file into an Google spreadsheet as this will make possible to use the Spreadsheet Service (Class SpreadsheetApp) to read the data from it.
If you don't want to convert it to a Google spreadsheet file, or it can't be converted the the "basic" means because it's using an incompatible format (like a xls file format), then you will need to use an library or an external service to parse the Excel file content.
function getalldataonsheet() {
const ss=SpreadsheetApp.openById('ssid');//you provide id
const sh=ss.getSheetByName('sheetname');//you provide sheet namme
const rg=sh.getDataRange();
const vs=rg.getValues();//2d array
return vs;//this return 2d array
}
There are restrictions for passing parameters in client to server communication

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.

How to create Google Sheets file when CSV file is downloaded

When a user downloads a CSV file from an arbitrary site, I would like to be informed of this event and automatically upload the same file as a Google Sheets file. Is this possible or would this be violating a fundamental browser security concept?
The CSV file in question does not have a URL, but is created on the fly by the arbitrary web site, when the user clicks a button. An example would be the user's list of financial transactions at a bank web site.
I am not new to Google Apps/Drive but I am new to Google Apps Script.
There is no way to detect that kind of events.
I can't even imagine a function that would allow for automatic file upload of a local file... don't forget Google Apps Script is a server based environment.

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.

Loading Web Pages into Panels + reading the raw source code of another script

Ok, so this questions has two parts, and it's a dozy.
What I'm trying to do is create a web app that splits the screen in half, where one half is the Google Apps Script editor for another script, and the other half contains the result of that script executing.
Here's what I need help with:
1) Assuming you already know the URL you want to load, how do you load another website into a panel in an active web app?
2) Assuming you already know the key for the file, how do you retrieve that raw source code from a file saved onto the user's drive?