简体   繁体   English

如何将链接转换为文件名?

[英]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.

相关问题 我可以在 Google 云端硬盘中创建图像列表(包括文件名和共享链接)吗? - Can I create a list of images in my Google Drive (including file name and share link)? 如何将名称修改为上传的谷歌表单文件? - How can i modify the name to an uploaded google form file? 如何使用 Google App Script 中的“垃圾箱”将一个文件替换为另一个文件(如果它们的名称相同)? - How can i REPLACE a file with another file (if their name is the same) using 'Trash' ing in Google App Script? 如何使用文件名从谷歌电子表格复制数据,并将其粘贴到另一个文件中? - How can I copy data from a google spreasheet using the file name, and paste it into another file? 如何将单元格的值链接到工作表? - How can I link the value of a cell to a sheet? 我如何知道是否点击了锚链接? - how can i know if an anchor link is clicked or not? 如何在电子邮件中创建确认链接? - How can I create a confirmation link in email? 如何使用 setName function 从谷歌应用程序脚本中的单元格设置文件名? - How can I use the setName function to set the name of a file from a cell in google app script? 我怎样才能获得当前的工作表名称? - How can I get current sheet name? 如何将“ onEdit”名称更改为其他名称? - How can I change the name “onEdit” to other name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM