I need to fetch a simple document template file (.doc) from google docs (or drive), then fill in the missing details and save the document as new file into the cloud.
Could someone advise on how to achieve this using the Google Docs API so this could be done within bespoke environment?
thanks in advance
If I understand you correctly, you want to programmatically:
Retrieve a document from Drive.
Edit this document while keeping the original one unmodified.
Save this new document.
You have to follow these steps:
Use Files.copy from Drive API to copy the file you want to edit. To achieve this, you have to provide the id of the original file as a request parameter. As a response you will get a File resource corresponding to the copy you created. Retrieve the file id from this response.
Update this new file using batchUpdate from Docs API. For that you will need to (1) provide the file id you retrieved in the previous step as a parameter and (2) provide the changes you want to make using the requests parameter (an array of all the changes you want to make) in the request body (check the links I attach to know exactly how should your requests parameter be like - I would like to be more specific here, but I don't know how exactly do you want to modify the document).
Obviously, there is no need to save the modified document, this is done automatically when the file is modified.
I hope this is of any help to you.
Related
I tried to use Get(). I was able to read/update the file metadata but don't know how to get the file contents. From my googling I know I have to identify alt=media but I don't know how. Could anybody give a full example.
To retrieve the contents of a Gogle document like Google Sheets, Google Docs etc. you can use the Sheets API, Docs API etc. respectively
To retrieve the contents of a non-Google file on your Google Drive, like e.g. a text file - you cannot do it directly
Instead, you need to donwload the file to your local Drive
For this, when using the method Files: get, specify alt=media as you already know
However, this will not directly give you the file contents, but rather request a download of the contents from the server
In the next step you need to write this downloaded content to a file on your local machine
The Guide for downloading files stored on Google Drive provide samples of how to do so in Java, Python and Node.js
I have a Google Docs document which contains one image. I found images's objectId as stated here https://developers.google.com/docs/api/reference/rest/v1/InlineObject but I can't understand how to get an url to that image.
I tried searching for this objectId in Drive but it returns File not found.
Any ideas ?
Update
As noted by #Tanaike the image info is contained in the result.inlineObjects and
not directly in the paragraph.
You want to retrieve the URL of the inserted image in Google Document using Google Docs API.
If my understanding is correct, how about this answer?
I think that the property of inlineObjectElement that you are checking is in the paragraph. The information of the inserted images can be seen at the property of inlineObjects. And the URL can be seen at the property of ImageProperties.
The endpoint is as follows.
Endpoint:
GET https://docs.googleapis.com/v1/documents/{documentId}?fields=inlineObjects
Here, as a sample, inlineObjects is used as fields. You can also use * as fields.
Result:
The URL can be retrieved as follows. It supposes that response is the returned value from above endpoint.
url = response.inlineObjects["kix.###"].inlineObjectProperties.embeddedObject.imageProperties.sourceUri
kix.### is inlineObjectId in your question.
If the image is inserted from the URL of outside, the URL is the same with the URL which was used when the image was inserted.
If the image is inserted from Google Drive, the URL is like below.
https://lh3.google.com/u/0/d/{fileId}=w###-h###
In this case, {fileId} is the file ID of the image. You can retrieve the original file using this file ID.
References:
Method: documents.get
InlineObject
ImageProperties
If I misunderstood your question and this was not the result you want, I apologize.
Step #1: Right-click and Copy the image from the Google Doc.
Step #2: Paste into dynalist.io.
(Note: Dynalist doesn't allow you to paste images, but you can paste images from Google Docs because it just adds the link.)
I understand that this may not help if you are trying to do something with the API, but for finding the link to a single image, it is the simplest method I've found.
use the drive and doc api
use the gdoc api retrieve the ContentUri:
(Document.InlineObjects[imageid].InlineObjectProperties.EmbeddedObject.ImageProperties.ContentUri)
imageid has the form "kix.###..."
The imageid can be retrieved frm the InlineObjects map, the key is the imageid
After obtaining the ContentUri you must strip the ending "=wxx-hyy" to obtain the the image file id for the drive api
use the drive api to obtain the file pointer:
*fil, err = service.Files.Get(fileid).Do()
After obtaining the file pointer, you can retrieve the image file characteristics (see File)
Alternatively the image file can be downloaded via:
httpResp, err =service.Files.Export(fileid, mime).Download()
mime types are listed here
I wish to firstly turn a Google Spreadsheet into a JSON feed, there are plenty of posts online for this but none seem to work, it is fully shared and public. I think the structure of the URL may have changed that is causing this not to work.
This is the file I wish to read as JSON
https://docs.google.com/spreadsheets/d/195boU7gqGLh_q2RXgnwfFVidfEn-YXakBhC4A--lej0/edit?usp=sharing
This is what I have based on https://ctrlq.org/code/20004-google-spreadsheets-json
https://spreadsheets.google.com/feeds/list/195boU7gqGLh_q2RXgnwfFVidfEn-YXakBhC4A--lej0/od6/public/basic?alt=json
Secondly, I would like this Google spreadsheet to be able to be updated from a 3rd party CSV or XML file located an another URL. Is this possible?
I found another tutorial about your problem.
Just make sure you follow the step 1 here, that in your Google Spreadsheet,click the "share" button and choose the "Anyone on the internet can find and view". Also you need to publish the entire sheet in order to this. Go to "File", "Publish to the web…" and publish the entire spreadsheet.
You can also try the solution in this SO question.
https://spreadsheets.google.com/feeds/worksheets/195boU7gqGLh_q2RXgnwfFVidfEn-YXakBhC4A--lej0/private/full?alt=json
Another workaround is by using Google AppScript. Here is the tutorial on how to do it by using AppScript.
I am wondering if anyone can give me an example on how to gather data from a JSON file, and import it into a single cell in google sheets? It does not need to be formatted or copied to multiple cells, it simply needs to take the entire contents of the JSON and copy it into a single cell. The file I am working with is also a local file. Can anyone shed some light? It does not necessarily need to use google apps script, if a python script or anything similar could do the same thing that would be ok
First of all, Google Sheets cannot access your local files. It's a web application, so any external data it gets must be accessible from the Internet.
If your goal is simply to put the contents of a web-accessible JSON file in a single cell, the following custom function will do the job:
function import(url) {
return UrlFetchApp.fetch(url).getContentText();
}
This simply grabs whatever page you point at, and crams its contents in a cell. Example: =import("http://xkcd.com/info.0.json")
If you do decide to parse JSON, be advised there isn't anything suitable built into Google Sheets at present. The project importJSON by Trevor Lohrbeer may be helpful.
You can also use an extension of "API CONNECTOR" for free in the google sheets and just paste the url of the json file you need to import.
here is the link to the API Connector extension
https://workspace.google.com/marketplace/app/api_connector/95804724197
here is how to perform it.
https://www.benlcollins.com/apps-script/api-tutorial-for-beginners/
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!