将多个 URL 转换为单个 PDF

Convert multiple URL to individual PDFs

问题 - 我有一个 Google Sheet,其中包含大约 200 个 Google 文档 URL。是否可以有一个脚本将 URLS 转换为单独的 PDF 文件并将其保存到我的桌面?

我在网上到处搜索都找不到解决办法。如果有人有任何见解或可以指出正确的方向,那将非常有帮助。

一个解决方案是,在云端硬盘中创建一个文件夹,将文档转换为 PDF,然后将文件夹下载为 .zip

function convertDocuments() {
  /* Select the Spreadsheet */
  const SS_ID = "SPREADSHEET_ID"
  const SS = SpreadsheetApp.openById(SS_ID)
  const PDF_MIME = "application/pdf"
  const newFolder = DriveApp.createFolder('PDFs')
  /* Get the links */
  const getLinks = SS.getRange('A2:A').getValues()
  getLinks.forEach((cells)=>{
    const link = cells[0]
    if(link==="") return
    /* Getting the ID from the URL */
    const parseID = link.toString().split("/")[5]
    /* CREATE THE PDF */
    const document = DriveApp.getFileById(parseID).getAs(PDF_MIME).copyBlob()
    /* Inserting the PDF into the file */
    newFolder.createFile(document)
  })
  Logger.log(newFolder.getUrl())
  /* downloadFolder(newFolder.getId()) */
}

步骤如下:

  1. 检索 A 列内的所有 links
  2. 使用 DriveApp 为每个 link 创建 PDF(link 需要解析以检索 ID)
  3. 将 PDF 放入驱动器文件夹
  4. 从这里开始,您有两种可能性:
    • 使用UI下载文件夹
    • 使用 function (provided by @Tanaike)直接获取下载link。在我的脚本中引用为 downloadFolder
function downloadFolder(folderId) {
  const folder = DriveApp.getFolderById(folderId);
  const files = folder.getFiles();
  let blobs = [];
  while (files.hasNext()) {
    blobs.push(files.next().getBlob());
  }
  const zipBlob = Utilities.zip(blobs, folder.getName() + ".zip");
  const fileId = DriveApp.createFile(zipBlob).getId();
  const url = "https://drive.google.com/uc?export=download&id=" + fileId;
  Logger.log(url);
}
文档