I am trying to insert an image that is stored on my google drive that I want to insert into my google sheet, using google script
Bearing in mind that i have a a whole load of images stored in my drive that i don't really want to publish to the web, but do want an automated way of doing it.
I know this question was asked about 2/3 years ago and google didn't have a method of doing it, there were workarounds like export links which have now been detracted.
insertimage() comes to mind but it doesn't seem to work for me.
This is the code I am using to get PDF and Tiff in, it is basically the code by Tainik
function insertDrawing(folderID, file_name) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var file = DriveApp.getFolderById(folderID).searchFiles('title contains "' + file_name + '"').next();
if (!~file.getMimeType().indexOf("pdf") || !~file.getMimeType().indexOf("tiff") || file == 'undefined') {
ss.toast('No image found');
return
}
var res = (JSON.parse(UrlFetchApp.fetch("https://www.googleapis.com/drive/v3/files/" + file.getId() + "?fields=thumbnailLink", {
method: "GET",
headers: {
"Authorization": "Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
}).getContentText()).thumbnailLink);
var r = res.split("=");
Logger.log(res)
var url = res.replace(r[r.length - 1], "s5000");
Logger.log([res, url])
return UrlFetchApp.fetch(url, {
headers: {
Authorization: "Bearer " + ScriptApp.getOAuthToken()
}
}).getBlob()
}
if (!~file.getMimeType().indexOf("pdf") || !~file.getMimeType().indexOf("tiff") || file == 'undefined') {,ss.toast()inifis run for all files included pdf and tiff. When you want to use only pdf and tiff, please modify fromif (!~file.getMimeType().indexOf("pdf") || !~file.getMimeType().indexOf("tiff") || file == 'undefined') {toif (!~file.getMimeType().indexOf("pdf") && !~file.getMimeType().indexOf("tiff") || file == 'undefined') {. - Tanaikes1100. Whensis large, an error occurs. - Tanaikesis different between mimeType. I have reported about it at github.com/tanaikech/ImgApp#doresize_principle Please check it. - Tanaike