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.
Related
I would like to allow users to use my spreadsheet but not copy it as it contains intellectual property. I tried going to sharing settings and disabling:
Editors can change permissions and share
Viewers and commenters can see the option to download, print, and copy
But the sheet can still be copied. Ideas?
Unfortunately, it is not possible to disable copy / download for editors.
You can only do that for commenters and viewers.
As a workaround, I would advice you to keep your sensitive information into one master file and then importrange or copy via a script the shareable information into another file. So even if they copy or download the latter your sensitive information won't be copied / downloaded.
Related questions:
How to disable copy/ download access for editors in google sheets
Prevent editors from downloading the file
Disable download & Copy to Option in Google Spreadsheet
I think the simplest solution would be to copy and paste from the master file the range of values you want to share with the other document. In this scenario the editors of the other document won't have access to neither the code nor the full data of the master file since the latter won't be shared with them.
The copy and paste part can be done automatically via a script and a trigger mechanism to update the data automatically so you won't have to do anything manually and the master file won't be exposed to any user.
There isn't any sure way to hide your data. Once something is published on the internet, you should consider it saved on many devices all over the world. Consider some ways to get hidden spreadsheet data
Attack scenarios:
By far the easiest way is CTRLC and CTRLV(Copy and Paste)
Editor menu options: File->Copy and File->Export
Once your file id is visible, any editor or even viewer with access to the file can easily copy the file itself through
Url manipulation: Adding /copy at the end instead of /edit
google-drive-api: File:get and File:copy
google-sheets-api: Useful to directly get data as json
google-vizualization-api: Can get data as html,csv or json(google query). See endpoints
Screenshot and use OCR(Optical character recognition)
View source code in the browser and directly copy the table
web-scraping Simulate browser using selenium
Hiding data:
Data may be hidden from naive users. Data cannot be hidden from users, who know the basics of how the web works.
Add ?rm=minimal to url, when sharing the sheets file. This hides all menu options.See here
Frame the edior in a iframe in your own website and use css to hide the top portion of the web page.
Hiding Logic:
You may still be able to hide logic of your code.
IMPORTRANGE: This is a very basic and easy way to hide your logic. But there are limitations and any editor can access any part of your master spreadsheet.
You can implement a IMPORTRANGE like logic using custom functions and webapps. This gives more control over the connector and secures your master spreadsheet much better than IMPORTRANGE. Here,
Two web apps are created, each associated with a spreadsheet(Master and client).
You use two KEYs to communicate between them. One for access and other for encryption.
Once access is verified, the data from master spreadsheet is encrypted and sent back to the custom function. Simultaneously the encryption key is posted to the client webapp.
The key here is the Master/Server webapp posts the encryption key only to the published client web app link. So, no other sheet or anything else can intercept the key or decrypt the data. Furthermore, a random key is generated for each access.
Another option is to let go off the spreadsheet completely and use a single webapp to show the data. This hides the logic in server scripts and linked spreadsheets.
Comment thoughts:
Create a script onOpen to kill sheets if the file is wrong?
onOpen cannot post data anywhere without the new copy owner permission. It's not possible to kill sheets. But data can be erased.
/**
* Deletes all sheets on the copy, if a copy is made
*/
const onOpen = () => {
const ss = SpreadsheetApp.getActive();
const id = ss.getId();
const sheets = ss.getSheets();
ss.insertSheet(sheets.length);//insert a blank sheet at the end
if (id !== '###Original ID###') sheets.forEach(s => ss.deleteSheet(s));//will fail at the last sheet(doesn't matter)
};
But editor can modify the original script before making a copy. And a revision of the original spreadsheet will still be available. The new owner can revert to the original version, use api endpoints mentioned above to get the data. Also mobile apps don't support onOpen. New owners can simply use mobile versions to access data.
Use formula web-app to notify file owner, ?
Possible, but data is already copied and there's no specific information that can be used to accurately identify the new owner. You maybe able to get locale information though.
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.
I'd like to open a PDF in Google Docs (not the previewer) without having Docs convert the file, 3rd Party PDF Viewers are not an option as I need to run a Google Script on the file also. PDF->Doc conversion is completely useless, as it removes all formatting and leaves a worthless document in it's wake.
No, there is not. Google Apps script DocumentApp can only work with Google Docs type files anyway.
Depending on what you are trying to accomplish you could look at the DriveApp Apis, but any modifications to the Document contents require it to be a google type file.
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!
After reading up a lot on the Google Spreadsheet API I have come to the conclusion that formatting (such as merging cells, changing fonts etc) is only available throught the Apps scripts.
Since we need to create and fill the spreadsheets with data programatically using Java on the back-end I guess I need to somehow either;
link the new sheet to a Apps script that trigger on-load or
create a Apps script that creates the spreadsheet for me.
Anyone knows?
If you want to just "create" the spreadsheet, you don't need a script to load whenever it spreadsheet is opened. It's probably easier to develop a script that runs once and create the spreadsheet for you.
Another tip is to have a template file that you can copy with most of the formatting (if not all) already there. Possibly pending just little things that are related to the real data the new spreadsheet will have.
Edit to answer the question in the title.
No, you can not add a script to an existing spreadsheet programatically, only manually. What you can do is previously set up a template spreadsheet with a script in it and create new spreadsheets by copying this template.
(answering the comment)
You can run a script programatically, but not upload it. To run a script you can deploy it as a web-app and call its url with either a http get or post (will call its doGet or doPost functions, that you must have declared). Also, you could set this script to run on form submit of any spreadsheet-form and just submit a set of answers to this form. At last (that I can think of now) you could just add the script as a library in another Apps Script and call it directly.
(Aug 2016) There is no way programmatic way to link a Google Sheet and Apps Script code other than manually. Based on what it seems you want ("create and fill the spreadsheets with data programatically using Java"), you can now do it without Apps Script.
TL;DR: Above, #Henrique has answered multiple questions and even questions that weren't asked! The good news is that today, we have more answers representing alternate possible solutions to what you're seeking.
It's now possible to "upload" Apps Script code programmatically with the
import/export system, say with Eclipse since you're a Java developer (2013 announcement).
I agree with Henrique's suggestion that if you create a spreadsheet
template, i.e., Excel file, you can use the Google Drive API to
programmatically import/create identical Google Sheets with all your
desired formatting.
"Formatting (such as merging cells, changing
fonts etc)" can now be done outside of Apps Script, as there is a
"new" Google Sheets API v4 (not GData).
In order to use the new API, you need to get the Google APIs Client Library for Java and use the latest Sheets API, which is much more powerful and flexible than any previous API. Here's one code sample to help get you started. If you're not "allergic" to Python, I also made a video with a different, slightly longer example introducing the new API and gave a deeper dive into its code via a blogpost that you can learn from.
Note the v4 API allows you to create spreadsheets & sheets, upload & download data, as well as, in the general sense, programmatically access a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.), but to perform file-level access such as uploads & downloads, imports & exports (same as uploads & downloads but conversion to/from Google Apps formats), you would use the Drive API instead.