How to get the url to Google doc image - google-drive-api

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

Related

Creating an inlineImage blob for Google Apps Script MailApp from a base64 image URL?

I have a sign-in form built in Google Apps Script that sends data to a row in a Google Sheet. But if a user tries to sign in that's not in a preset list, it sends an email to the clerk. The sign in computer has a webcam, and the GAS form can activate the webcam and take a picture of the person; it returns a base64 encoded string of the image data (jpeg format).
What I'm wrestling with is getting GAS to create the proper Blob to add as an inlineImage into the email.
var userImgBlob = Utilities.newBlob()
.setContentType("image/jpeg")
.setDataFromString(Utilities.base64Decode(imgTxt.slice(23)))
.setName("userImgBlob");
imgTxt contains the data: URI, coming in like:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgM....
Slicing the first 24 (0 to 23) characters removes the header so that it's just the image data. Something about this causes the script to fail, however. (Commenting this section out lets the rest of the script run fine.)
What's malformed about my definition?
NOTE: I've already looked at the How to include inline images in email using MailApp. My question's a little different, because in that post, the user was Fetching a URL from the web. This is a data: URI that I'm working with.
Well, $%&$#. Now I feel dumb. So a script I had seen elsewhere created a newBlob() and then set parameters after creation.
Turns out the Utilities.newBlob() function doesn't like creation with no parameters.
Put it in a single declaration:
var userImgBlob = Utilities.newBlob(Utilities.base64Decode(imgTxt.slice(23)),"image/jpeg","userImgBlob");
and boom - Bob's your uncle.
Reference: Class Utilities - Apps Script

Using Google Docs API to Insert Image From Google Drive

I'm wondering if it's possible to obtain the URI for an image located on one's Google Drive.
What I've Tried:
Drive API: GET https://www.googleapis.com/drive/v2/files/{fileId}
(Using the Google Docs batchUpdate ReplaceImageRequest to replace images)
uri = file["selfLink"] => "Invalid requests[0].replaceImage: Access to the provided image was forbidden."
uri = file["webContentLink"] => "Invalid requests[0].replaceImage: There was a problem retrieving the image. The provided image should be publicly accessible, within size limit, and in supported formats."
uri = file["alternateLink"] => (see above)
uri = file["embedLink"] => (see file["webContentLink"])
I was trying these properties– expecting the Docs API to automatically interpret drive links. But this was probably just wishful thinking; as it of course, did not work.
I believe your goal as follows.
You want to replace the image using the method of documents.batchUpdate in Google Docs API.
You want to use the image file on your Google Drive for this.
Issue and solution:
When the image is put to the Google Document using Docs API, it seems that the URL of the image is the external URL. So in your case, you can use the URL of webContentLink. But the file is required to be publicly shared as the viewer with "Anyone with the link". I think that the reason for your issue is this. So please do the following flow.
Please publicly share the image file as the viewer with "Anyone with the link".
Please request the batchUpdate using the URL of webContentLink of the shared image.
Note:
This answer supposes that you have already been able to get and put values for Google Document using Docs API. So please be careful about this.
Unfortunately, I couldn't understand your actual request body. When you modified like above, when another error occurs, can you provide the detail of your request body? By this, I would like to confirm it.
References:
Method: documents.batchUpdate
ReplaceImageRequest

how to replicate the document using Google Docs Api?

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.

Download Public Google Spreadsheet as CSV?

I have a spreadsheet, ex.:
https://docs.google.com/spreadsheets/d/1_qVnTw1Cwb2Ziwta_N0p-V4_ptD6-ZypDvCIrnryNFU/pubhtml
that I want to download as a CSV file. To do this, I believe I must download it from a link similar to this:
https://spreadsheets.google.com/pub?hl=en&hl=en&key={INSERT KEY HERE}&output=csv
But my question is, how do I go about getting the "key"?
Append the /export?format=csv just after the document id in the URL.
So in your case, the URL should look like this:
https://docs.google.com/spreadsheets/d/1_qVnTw1Cwb2Ziwta_N0p-V4_ptD6-ZypDvCIrnryNF/export?format=csv
Of course, this must link to an existing sheet that is publicly shared (for viewing)

Can I add a web page as a document to google drive

I would like to add a 'file' to my drive which is simply a link to a website/page. I have tried creating a file with alternateLink and webContentLink fields specified but they just get overwritten.
I want to be able to store the link in Drive, retrieve it and use it in my app at a later date.
You can but not like that. You can:
1) write your own custom file and store the link as file data
2) additionally if you want the link to open from drive you can (unlike a comment in this question that saus you cant) by writting a Drive app and handle the file-open. See https://support.google.com/drive/answer/2500820?hl=en
What I was looking for was the 'shortcut' mimetype : "application/vnd.google-apps.drive-sdk", as described in create_a_shortcut_to_a_file. When I create a file with that mimeType, the alternateLink does not get overwritten. A 'file' is created in my drive and I can use the alternateLink to navigate to tghe page.