[英]How can I transform a link to a file name?
I have a Google spreadsheet file with almost 2000 rows of URL such as this one:我有一个包含近 2000 行 URL 的 Google 电子表格文件,例如:
https://docs.google.com/uc?id=0B4ptELk-D3USblk1aWd1OWowRWs
Each URL contains an image, stored in Google Drive.每个 URL 包含一个图像,存储在 Google Drive 中。 My question is: How can I transform this link (
https://docs.google.com/uc?id=0B4ptELk-D3USblk1aWd1OWowRWs
) to a file name?我的问题是:如何将此链接(
https://docs.google.com/uc?id=0B4ptELk-D3USblk1aWd1OWowRWs
)转换为文件名? For instance, I would like to know if it could be a /something/folder/Photofile1.jpg
.例如,我想知道它是否可以是
/something/folder/Photofile1.jpg
。
Is there anyway of doing this?反正有这样做吗?
Try script:尝试脚本:
function TESTgetFileName() {
Logger.log(getFileName('0B4ptELk-D3USVi1NZ3ZGbkhqYXc'));
// ^^^^^^^^^^ file ID ^^^^^^^^^
}
function getFileName(id) {
var file = DriveApp.getFileById(id);
var fileName = file.getName();
var strFolders = getFolders(file);
return strFolders + '/' + fileName;
}
function getFolders(object) {
var folders = object.getParents();
if (!folders.hasNext()) { return 'My Drive'; }
var folder = folders.next();
var folderNames = [];
while (folder.getParents().hasNext()) {
var folderName = folder.getName();
folderNames.unshift(folderName);
folder = folder.getParents().next();
}
return folderNames.join('/');
}
Script function will return the result:脚本函数将返回结果:
0B4ptELk-D3USVi1NZ3ZGbkhqYXc → My Drive/something/folder/Photofile1.jpg
Tests测试
I opened script editor, selected function TESTgetFileName
and get the result我打开脚本编辑器,选择函数
TESTgetFileName
并得到结果
My Drive/Detalhes_Farmacia.Fotografia_Fachada_1.JPEG
. My Drive/Detalhes_Farmacia.Fotografia_Fachada_1.JPEG
。
This function can be run from script, not directly from spreadsheets.此函数可以从脚本运行,而不是直接从电子表格运行。 When you try using it as custom formula from sheet, it throws error:
当您尝试将其用作工作表中的自定义公式时,它会引发错误:
You do not have permission to call getFileById (line 8).
您无权调用 getFileById(第 8 行)。
So better use it from script to get data and then write the result to the sheet.所以最好从脚本中使用它来获取数据,然后将结果写入工作表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.