从超链接列表批量复制 google 驱动器上的文件
Bulk copy files on google drive from a list of hyperlinks
我在电子表格中保存了 200 个超链接的列表。这些链接适用于所有保存在 Google 云端硬盘中的文件(特别是 Google 幻灯片文件)。它们分散在具有 ~1500 个文件的同一根文件夹下的子文件夹中
Link 1
Link 2
Link 3
...
Link200
我只想复制这 200 个文件。在 Google 驱动器搜索中没有常见的搜索词或过滤器可以将它们拉出来。所以我需要完成该列表
想这样做吗?提前致谢!
假设链接看起来与此类似,并且它们存储在 A
列中:
https://docs.google.com/presentation/d/SLIDE_ID/edit
您可以使用此公式(将其拖到整个 A
列)从对应于 fileId
的超链接中轻松提取 slideId
:
=REGEXEXTRACT(A1,"[-\w]{25,}")
最后,为了复制每个文件,您可以使用 Apps Script 的 DriveApp
,类似这样的东西:
DriveApp.getFileById(“fileId”).makeCopy(“destination”);
但是,由于 fileId
对应于 sheet 中的一个范围,您可以直接传递该范围 - 因此您可以使用以下内容代替“fileId”:
let sheet = SpreadsheetApp.openById(“spreadsheetId”).getSheetByName(“sheetName”);
let fileId = sheet.getRange(1,2).getValue();
上面的代码片段正在检索存储链接的 sheet,然后通过使用 getRange
和 getValue
方法从 B1
单元格(假设文件的 ID 将在 REGEXEXTRACT
之后的 B
列中)。
备注
请记住,您也可以直接在脚本中提取 fileId
,具体取决于变通方法和编程语言。
参考
我相信你的现状和你的目标如下。
- 您的电子表格包含 Google 幻灯片的 200 个超链接,例如
https://docs.google.com/presentation/d/FILE_ID
。
- 您想将 Google 幻灯片复制到 Google 驱动器中的特定文件夹。
- 您想使用 Google Apps 脚本实现此目的。
从超链接的数量来看,我认为在这种情况下,批量请求可能对您的情况有用。当使用批量请求时,由于批量请求是运行异步流程,因此流程成本会变低。因此,在这个答案中,我想建议使用批量请求复制 200 个超链接的文件。示例脚本如下
用法:
1。安装 Google Apps 脚本库。
在这个脚本中,为了实现批量请求,使用了一个Google Apps Script库。 Ref I thought that the request of the batch request might be a bit complecate. Ref 所以我创建了这个库,以便将批处理请求与 Google Apps 脚本结合使用。图书馆的项目密钥如下。
1HLv6tWz0oXFOJHerBTP8HsNmhpRqssijJatC92bv9Ym6HSN69_UuzcDk
库的安装方法见官方文档。 Ref
2。示例脚本。
请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中,其中包括 200 个超链接,例如 https://docs.google.com/presentation/d/FILE_ID
。该脚本使用了驱动器 API。所以,在你使用这个脚本之前,please enable Drive API at Advanced Google services。并且,运行 函数“myFunction”。
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const destinationFolderId = "###"; // Please set the destination folder ID.
// 1. Retrieve file IDs from Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const fileIds = sheet.getRange("A1:A" + sheet.getLastRow()).getValues().reduce((ar, [a]) => {
if ((/^https:\/\/docs.google.com\/presentation\/d/).test(a)) ar.push(a.split("/")[5]);
return ar;
}, []);
console.log(fileIds) // You can check the retrieved file ID.
// 2. Retrieve the filenames form the file IDs using the "Files: get" method with the batch request.
const requests1 = fileIds.map(id => ({
method: "GET",
endpoint: `https://www.googleapis.com/drive/v3/files/${id}?supportsAllDrives=true`,
}));
const res1 = BatchRequest.EDo({batchPath: "batch/drive/v3", requests: requests1});
console.log(res1) // You can check the retrieved file metadata.
// 3. Copy the files using the file IDs and filenames using the "Files: copy" method with the batch request.
const requests2 = res1 .map(({id, name}) => ({
method: "POST",
endpoint: `https://www.googleapis.com/drive/v3/files/${id}/copy?supportsAllDrives=true`,
requestBody: {parents: [destinationFolderId || "root"], name},
}));
const res2 = BatchRequest.EDo({batchPath: "batch/drive/v3", requests: requests2});
console.log(res2);
}
注:
- 在此示例脚本中,假设将 200 个超链接放在“Sheet1”的“A”列中。所以,请根据您的实际情况进行修改。请注意这一点。
参考文献:
我在电子表格中保存了 200 个超链接的列表。这些链接适用于所有保存在 Google 云端硬盘中的文件(特别是 Google 幻灯片文件)。它们分散在具有 ~1500 个文件的同一根文件夹下的子文件夹中
Link 1 Link 2 Link 3 ... Link200
我只想复制这 200 个文件。在 Google 驱动器搜索中没有常见的搜索词或过滤器可以将它们拉出来。所以我需要完成该列表
想这样做吗?提前致谢!
假设链接看起来与此类似,并且它们存储在 A
列中:
https://docs.google.com/presentation/d/SLIDE_ID/edit
您可以使用此公式(将其拖到整个 A
列)从对应于 fileId
的超链接中轻松提取 slideId
:
=REGEXEXTRACT(A1,"[-\w]{25,}")
最后,为了复制每个文件,您可以使用 Apps Script 的 DriveApp
,类似这样的东西:
DriveApp.getFileById(“fileId”).makeCopy(“destination”);
但是,由于 fileId
对应于 sheet 中的一个范围,您可以直接传递该范围 - 因此您可以使用以下内容代替“fileId”:
let sheet = SpreadsheetApp.openById(“spreadsheetId”).getSheetByName(“sheetName”);
let fileId = sheet.getRange(1,2).getValue();
上面的代码片段正在检索存储链接的 sheet,然后通过使用 getRange
和 getValue
方法从 B1
单元格(假设文件的 ID 将在 REGEXEXTRACT
之后的 B
列中)。
备注
请记住,您也可以直接在脚本中提取 fileId
,具体取决于变通方法和编程语言。
参考
我相信你的现状和你的目标如下。
- 您的电子表格包含 Google 幻灯片的 200 个超链接,例如
https://docs.google.com/presentation/d/FILE_ID
。 - 您想将 Google 幻灯片复制到 Google 驱动器中的特定文件夹。
- 您想使用 Google Apps 脚本实现此目的。
从超链接的数量来看,我认为在这种情况下,批量请求可能对您的情况有用。当使用批量请求时,由于批量请求是运行异步流程,因此流程成本会变低。因此,在这个答案中,我想建议使用批量请求复制 200 个超链接的文件。示例脚本如下
用法:
1。安装 Google Apps 脚本库。
在这个脚本中,为了实现批量请求,使用了一个Google Apps Script库。 Ref I thought that the request of the batch request might be a bit complecate. Ref 所以我创建了这个库,以便将批处理请求与 Google Apps 脚本结合使用。图书馆的项目密钥如下。
1HLv6tWz0oXFOJHerBTP8HsNmhpRqssijJatC92bv9Ym6HSN69_UuzcDk
库的安装方法见官方文档。 Ref
2。示例脚本。
请将以下脚本复制并粘贴到 Google 电子表格的脚本编辑器中,其中包括 200 个超链接,例如 https://docs.google.com/presentation/d/FILE_ID
。该脚本使用了驱动器 API。所以,在你使用这个脚本之前,please enable Drive API at Advanced Google services。并且,运行 函数“myFunction”。
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const destinationFolderId = "###"; // Please set the destination folder ID.
// 1. Retrieve file IDs from Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const fileIds = sheet.getRange("A1:A" + sheet.getLastRow()).getValues().reduce((ar, [a]) => {
if ((/^https:\/\/docs.google.com\/presentation\/d/).test(a)) ar.push(a.split("/")[5]);
return ar;
}, []);
console.log(fileIds) // You can check the retrieved file ID.
// 2. Retrieve the filenames form the file IDs using the "Files: get" method with the batch request.
const requests1 = fileIds.map(id => ({
method: "GET",
endpoint: `https://www.googleapis.com/drive/v3/files/${id}?supportsAllDrives=true`,
}));
const res1 = BatchRequest.EDo({batchPath: "batch/drive/v3", requests: requests1});
console.log(res1) // You can check the retrieved file metadata.
// 3. Copy the files using the file IDs and filenames using the "Files: copy" method with the batch request.
const requests2 = res1 .map(({id, name}) => ({
method: "POST",
endpoint: `https://www.googleapis.com/drive/v3/files/${id}/copy?supportsAllDrives=true`,
requestBody: {parents: [destinationFolderId || "root"], name},
}));
const res2 = BatchRequest.EDo({batchPath: "batch/drive/v3", requests: requests2});
console.log(res2);
}
注:
- 在此示例脚本中,假设将 200 个超链接放在“Sheet1”的“A”列中。所以,请根据您的实际情况进行修改。请注意这一点。