在Google Apps脚本上通过网址获取文件ID的最简单方法 [英] Easiest way to get file ID from URL on Google Apps Script

查看:140
本文介绍了在Google Apps脚本上通过网址获取文件ID的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我想要做的:给定一个Google文档网址,我想获取文档ID以在Google云端硬盘上创建副本。我知道我可以通过一些正则表达式或替换URL来实现,但由于在URL中有多种不同的表示方式来表示同一个文档,所以我想找到一个通用的解决方案。



目前,我认为这是最好的:

  function getFileIdFromUrl(url){
try {
return getDocIdFromUrl(url);
} catch(e){
return getSpreadsheetIdFromUrl(url);
}
}

函数getDocIdFromUrl(url){
var doc = null;
尝试{
doc = DocumentApp.openByUrl(url);
} catch(e){
doc = DocumentApp.openByUrl(url +/ edit);
}
返回doc.getId();
}

函数getSpreadsheetIdFromUrl(url){
var spreadsheet = null;
尝试{
spreadsheet = SpreadsheetApp.openByUrl(url);
} catch(e){
spreadsheet = SpreadsheetApp.openByUrl(url +/ edit);
}
返回spreadsheet.getId();
}

function copy(url){//如果URL无效或私有,则可能会引发异常
var id = getFileIdFromUrl(url);
var file = DriveApp.getFileById(id);
file.makeCopy()。setSharing(DriveApp.Access.ANYONE_WITH_LINK,DriveApp.Permission.VIEW);
}

问题是我的解决方案只包含文档和电子表格,我想对任何上传的文件都做同样的事情,例如:

https://docs.google.com/file/d/0B-FYu_D7D7x4REdtRVEzVH0eU0/编辑



总之,我想要这样的东西:

  DriveApp.getFileByUrl(url).makeCopy(); 

有人知道这可能吗?

任何从文件URL中提取文件ID的安全解决方案都适合我。



谢谢

解决方案DriveApp确实缺少 getFileByUrl (以及文件夹)。您可能希望在 Apps脚本问题跟踪器 上打开增强请求。 a>。



但是我在脚本上做了什么(因为这些 openByUrl 函数有点新)使用正则表达式获取ID。


$ b

function getIdFromUrl(url){return url.match(/ [ - \ W] {25,} /); }

这个正则表达式适用于我试过的任何谷歌网址:Drive url for folders and files,Fusion表格,电子表格,文档,演示文稿等。它只是查找字符串中的任何内容,看起来像一个Google密钥。也就是说,任何足够大的字符串,只有(谷歌键)有效的字符。

此外,即使它直接接收ID,而不是URL 。当你询问用户的链接时,这很有用,因为有些人可能会直接粘贴id而不是url,它仍然有效。


Here is what I'm trying to do: given a Google document URL, I want to get the document ID to create a copy on Google Drive. I know I can achieve that by some regex or replacing on the URL, but as there are several different forms to represent the same document in a URL, I wanted to find a generic solution.

Currently, that's the best I could think:

function getFileIdFromUrl(url) {
  try {
    return getDocIdFromUrl(url);
  } catch (e) {
    return getSpreadsheetIdFromUrl(url);
  }
}

function getDocIdFromUrl(url) {
  var doc = null;
  try {
    doc = DocumentApp.openByUrl(url);
  } catch (e) {
    doc = DocumentApp.openByUrl(url + "/edit");
  }
  return doc.getId();
}

function getSpreadsheetIdFromUrl(url) {
  var spreadsheet = null;
  try {
    spreadsheet = SpreadsheetApp.openByUrl(url);
  } catch (e) {
    spreadsheet = SpreadsheetApp.openByUrl(url + "/edit");
  }
  return spreadsheet.getId();
}

function copy(url) { // may throw an exception if the URL is invalid or private
   var id = getFileIdFromUrl(url);
   var file = DriveApp.getFileById(id);
   file.makeCopy().setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
}

The problem is that my solution only covers documents and spreadsheets, I would like to do the same with any uploaded file, for example:

https://docs.google.com/file/d/0B-FYu_D7D7x4REdtRVEzVH0eU0/edit

In short, I wanted something like that:

DriveApp.getFileByUrl(url).makeCopy();

Does anyone know if it's possible?

Any safe solution to extract the file ID from the file URL would fit as well for me.

Thanks

解决方案

DriveApp is indeed missing a getFileByUrl (and also folder for that matter). You may want to open an enhancement request on Apps Script issue tracker.

But what I do on my scripts (since these openByUrl functions are somewhat new), is to get the id using a regex. Like this.

function getIdFromUrl(url) { return url.match(/[-\w]{25,}/); }

This regex works for any google url I've tried: Drive url for folders and files, Fusion Tables, Spreadsheets, Docs, Presentations, etc. It just looks for anything in a string that "looks like" a Google key. That is, any big enough string that has only (google key) valid characters in it.

Also, it works even if it receives the ID directly, instead of the URL. Which is useful when you're asking the link from the user, as some may paste the id directly instead of the url and it still works.

这篇关于在Google Apps脚本上通过网址获取文件ID的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆