有没有办法从 Google Sheets 电子表格下载位于单元格链接中的所有图像和 pdf 文件?

问题描述 投票:0回答:1

我有一个电子表格,其中有一列填充了多个 URL 链接,这些 URL 链接指向通过 Google 表单上传并位于我的 Google 云端硬盘中的文件夹中的一些图像文件和 pdf 文件。 由于这些文件与通过同一表单上传的其他文件混合在一起,因此在浏览文件夹时很难手动检索它们。
所以,我想知道是否有一种方法,使用 Google 应用程序脚本,将链接位于该电子表格单元格中的文件下载到单独的文件夹中。
太感谢了。
google-sheets google-apps-script
1个回答
0
投票
您可以首先打开 Google 表格创建 Google Apps 脚本,然后转到“扩展”创建新脚本。通过编写脚本,它将从电子表格中读取 URL,获取文件,并将它们保存到指定的文件夹中。

您可以尝试这个示例脚本

function downloadFilesFromFormResponses() { // Replace these placeholders with your actual values const SHEET_NAME = "Sheet1"; // Name of the spreadsheet containing URLs const URL_COLUMN = 1; // Column number (1-based) containing image/PDF URLs const DOWNLOAD_FOLDER_ID = "YOUR_DOWNLOAD_FOLDER_ID"; // ID of the destination folder // Get the spreadsheet and the URL column const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME); const urlColumn = sheet.getRange(1, URL_COLUMN, sheet.getLastRow()); const urls = urlColumn.getValues(); // Loop through each URL for (const url of urls) { const fileUrl = url[0]; // Assuming only one URL per cell (adjust if needed) // Check if it's an image or PDF URL (modify as needed) if (fileUrl.endsWith(".jpg") || fileUrl.endsWith(".png") || fileUrl.endsWith(".pdf")) { const filename = fileUrl.split("/").pop(); // Extract filename from URL const destinationFile = DriveApp.getFolderById(DOWNLOAD_FOLDER_ID).createFile(filename, UrlFetchApp.fetch(fileUrl).getBlob()); Logger.log("Downloaded file: " + filename); } else { Logger.log("Skipping non-image/PDF URL: " + fileUrl); } } }
授予脚本“管理文件”权限时要小心。它允许脚本创建和修改您的云端硬盘中的文件。在运行脚本之前,请确保您了解安全隐患。

© www.soinside.com 2019 - 2024. All rights reserved.