How can i get the picture filename from a google drive url? - image

I have a lot of picture links in my Google Sheet:
https://drive.google.com/file/d/1BDj24_6uz-ZQlGWLy0o1_DES6tJI3NMu/view?usp=sharing
I want to get the filename behind the link. When i open the link and save the image the image filename is: "FILENAME.jpg" or something similar
I want to get this information in Sheets.
How can i do that?

An Apps Script Solution
Thanks to #Tanaike for obtaining clarifications in the comments.
First enter your links in the format shown below in a Google Sheet.
The name of the titles is not important, but this particular script starts getting names from the 2nd row.
If you don't know how to create a script, there are tutorials here offered by Google. You should create the script from the same Sheet. That is, from the menu in the Sheet:
In the script editor, copy and paste the following code:
function getNames() {
var activeRange = SpreadsheetApp.getActiveSheet().getDataRange();
var height = activeRange.getHeight();
var links = SpreadsheetApp.getActiveSheet()
.getRange("A2:A" + height)
.getValues();
var nameValues = [];
links.forEach((row) => {
try {
var link = row[0];
var fileID = getIdFromLink(link);
var name = DriveApp.getFileById(fileID).getName();
nameValues.push([name]);
} catch (e) {
nameValues.push(["NO NAME FOUND"]);
}
});
var nameRange = SpreadsheetApp.getActiveSheet().getRange("B2:B" + height);
nameRange.setValues(nameValues);
}
function getIdFromLink(link) {
var regex = new RegExp(
/(?<=https:\/\/drive\.google\.com\/file\/d\/)(.+)(?=\/)/
);
return regex.exec(link)[0];
}
Run the getNames function. The first time it will ask you to grant some permissions, grant them. Then it should fill in the files names.
References
Apps Script Overview
Spreadsheet service in Apps Script
Drive service in Apps Script

Related

Insert Image into a Google Sheet with Apps Script using base64 encoded data

Given some base64 encoded data for a png file, as in the example below from the image tag.
<img src="data:image/png;base64,R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7">
I need to create an image blob from the base64 encoded data for the insertImage() method.
sheetClass.insertImage(imageBlob, column, row)
Documentation:
Sheets Class - insertImage method
I've tried using the code below, but it throws the error:
Execution failed: Error retrieving image from URL or bad URL
The documentation states that the method needs a blob, not a URL, but it seems like it's expecting a link to an image file.
function insertImageFromBase64Src() {
var data = 'R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7';
var imageBlob = Utilities.newBlob(Utilities.base64Decode(data), 'image/png').getBytes();
var ss = SpreadsheetApp.getActiveSpreadsheet();//This code is bound to a Sheet
var po = {
shName:'Update File',
column:1,
row:37
}
var sh = ss.getSheetByName(po.shName);
var image = sh.insertImage(imageBlob, po.column, po.row);//Insert an image and return the image
}
How about this modification?
Modification points:
insertImage can use the blob and URL which is the direct link of the image file.
In your script, imageBlob is an byte array which is "number[]". By this, I think that an error occurs.
Please add the name to the blob.
When the name is not given, an error occurs.
Modified script:
When your script is modified, it becomes as follows.
function insertImageFromBase64Src2() {
var data = 'R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7';
var imageBlob = Utilities.newBlob(Utilities.base64Decode(data), 'image/png', 'sample'); // Modified
var ss = SpreadsheetApp.getActiveSpreadsheet(); //This code is bound to a Sheet
var po = {
shName:'Update File',
column:1,
row:37
}
var sh = ss.getSheetByName(po.shName);
var image = sh.insertImage(imageBlob, po.column, po.row);//Insert an image and return the image
}
In this modification, it supposes that data can be correctly decoded.
References:
insertImage(blobSource, column, row)
insertImage(url, column, row)
If this was not the direct solution of your issue, I apologize.

How to get app name programatically in native script

I'm trying to save some files on my local File storage So, I'm doing Something like below
var folder_name = "abcde/" + viewModel.dir_path;
const documents = fileSystemModule.knownFolders.documents();
documents._path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
const folder = documents.getFolder(folder_name);
var file = fileSystemModule.path.join(folder._path, this.pdf_url.split("/").pop());
var url = this.pdf_url;
httpModule.getFile(url, file).then(function(result) {
console.log(result);
Toast.makeText(`${result._name} is succesfully downloaded in ${folder_name}`).show();
}, function(e) {
console.log(e);
});
the only problem is the hardcoded value abcde/ I want it to be app name. whatever the app name is it should take that name.
I don't find any ways to read app name programatically. I need this to Android I'm not interested in IOS.
may be this is one could be the answer. but still this is not a proper way
const documents = fileSystemModule.knownFolders.currentApp();
console.log(documents); --> "/data/data/org.nativescript.app_name/files/app"
var str = documents;
var arr = str.split("/")[3];
console.log(arr.split(".")[2]); ---> app_name
In Android, you can set the app name for the application in strings.xml, for example:
<string name="app_name">"App_Name"</string>
Then whenever you want to reuse the app name, you can use the getString method with its name in the application.

Can you call an external API to shorten links based on google sheets input?

I have a spreadsheet that contains links created after a form entry. I'd like to call the external API of a link shortening service (not google's link shorten-er) to take the link created in a given cell, shorten it, return the value and have Google Apps Script insert that shortened link into a new cell.
Is this possible? Specifically, can I use jQuery's AJAX methods? Where should I start.
By checking the internet, I found this stackExchange question, you can try to follow the solution in this post. What you need here is URL shortener API under the resources in the script editor (Tools > Script editor) select the Advanced Google Services and activate the UrlShortener.
Here is the sample code that you need to use.
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("Shorten")
.addItem("Go !!","rangeShort")
.addToUi()
}
function rangeShort() {
var range = SpreadsheetApp.getActiveRange(), data = range.getValues();
var output = [];
for(var i = 0, iLen = data.length; i < iLen; i++) {
var url = UrlShortener.Url.insert({longUrl: data[i][0]});
output.push([url.id]);
}
range.offset(0,1).setValues(output);
}
For more information, explanation and some sample pictures, just check the link above.

Precache / Preload directory

Been looking around for ways to preload a directory of images, found this to be the best script provided here: http://perishablepress.com/3-ways-preload-images-css-javascript-ajax/
I like this best as it waits for current document to load before preloading, but I'd like to get this guy to load a directory of images instead of having to specify each file, and am pretty new to javascript, any help appreciated.
function preloader() {
if (document.images) {
var img1 = new Image();
var img2 = new Image();
var img3 = new Image();
img1.src = "http://domain.tld/path/to/image-001.gif";
img2.src = "http://domain.tld/path/to/image-002.gif";
img3.src = "http://domain.tld/path/to/image-003.gif";
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(preloader);
There is no general way to accomplish this. You need some way to retrieve the contents of a directory from the server, and there is no guarantee that your web server will provide a listing. In general, most web servers do not allow a client to list the contents of a server side directory.
If you control the Web Server, you can configure it so that a directory listing is shown, for example, when you ask for "http://domain.tld/path/to/". But even then, the format of the returned response will likely be different, depending on the web server.
However, if you are able to get a listing of the images in a given directory from the server, you will still need to use some technique such as AJAX, or a hidden IFRAME, to load those image names. Then you could use JavaScript to iterate over the file names, construct the URL, and make an Image object for each.

Unable to set firefox bookmark description for in Add-On Kit API

I found the nsINavBookmarksService, however since Firefox 3, it does not seem to have any API methods for getting/setting the Bookmark description. (API doc)
I've seen other Add-Ons modify the description as a method of synchronized data storage (which is exactly what I'm trying to do). I'm guessing perhaps the description is a non-gecko standard, and that's why it is not directly supported, but then there must be a completely different interface for manipulating Bookmarks that I haven't discovered.
Can anyone help with this newbie problem?
Starting with Firefox 3 the bookmarks have been merged into the Places database containing all of your browsing history. So to get the bookmarks you do a history query, like this (first line is specific to the Add-on SDK which you appear to be using):
var {Cc, Ci} = require("chrome");
var historyService = Cc["#mozilla.org/browser/nav-history-service;1"]
.getService(Ci.nsINavHistoryService);
var options = historyService.getNewQueryOptions();
var query = historyService.getNewQuery();
query.onlyBookmarked = true;
var result = historyService.executeQuery(query, options);
result.root.containerOpen = true;
for (var i = 0; i < result.root.childCount; i++)
{
var node = result.root.getChild(i);
console.log(node.title);
}
That's mostly identical to the code example here. Your problem is of course that nsINavHistoryResultNode has no way of storing data like a description. You can set annotations however, see Using the Places annotation service. So if you already have a node variable for your bookmark:
var annotationName = "my.extension.example.com/bookmarkDescription";
var ioService = Cc["#mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService);
var uri = ioService.newURI(node.uri, null, null);
var annotationService = Cc["#mozilla.org/browser/annotation-service;1"]
.getService(Ci.nsIAnnotationService);
annotationService.setPageAnnotation(uri, annotationName,
"Some description", 0, Ci.nsIAnnotationService.EXPIRE_NEVER);
For reference: nsIAnnotationService.setPageAnnotation()

Resources