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

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

问题描述

这是我想要做的:给定一个 Google 文档 URL,我想获取文档 ID 以在 Google Drive 上创建一个副本.我知道我可以通过一些正则表达式或在 URL 上替换来实现这一点,但是由于有几种不同的形式可以在 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?

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

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

谢谢

推荐答案

DriveApp 确实缺少 getFileByUrl (以及相关的文件夹).您可能希望在 Apps 脚本问题跟踪器.

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.

但我在我的脚本中所做的(因为这些 openByUrl 函数有些新)是使用正则表达式获取 id.像这样.

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,}/); }

此正则表达式适用于我尝试过的任何 google url:文件夹和文件的 Drive url、Fusion Tables、Spreadsheets、Docs、Presentation 等.它只是在字符串中查找看起来像"的任何内容.谷歌密钥.也就是说,任何足够大的字符串,其中只有(谷歌键)有效字符.

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.

此外,即使它直接接收 ID 而不是 URL,它也可以工作.当您向用户询问链接时,这很有用,因为有些人可能会直接粘贴 id 而不是 url,但它仍然有效.

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.

--编辑

还有一些其他答案和评论可以解决我自己从未遇到过但可能会发生的一些边缘情况,例如尝试在嵌套文件夹 URL 上获取文件夹 ID,或者当您拥有 25 岁以上的 G-Suite 域时长字符.对于这些情况,您可能需要使用更严格的正则表达式.

There are some other answers and comments that address some edge cases that I never encountered myself but might happen, like trying to get a folder-id on a nested folder URL, or when you have G-Suite domain that is 25+ characters long. For those cases, you might want to use a more strict regex.

通过快速浏览下面的建议,我推荐以下 /[-w]{25,}(?!.*[-w]{25,})/,因为它仍然很简单,应该解决这些情况.

From a quick look at the suggestions below I recommend the following /[-w]{25,}(?!.*[-w]{25,})/ because it is still very simple and should address these cases.

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

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