問題 - 我有一個包含大約 200 個 Google 檔案 URL 的 Google 表格。是否可以有一個腳本將 URLS 轉換為單個 PDF 檔案并將其保存到我的桌面?
我已經在互聯網上搜索了高低,我找不到解決方案。如果有人有任何見解或可以指出我正確的方向,那將非常有幫助。
uj5u.com熱心網友回復:
一種解決方案是,在 Drive 中創建一個檔案夾,將檔案轉換為 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()) */
}
步驟如下:
- 檢索
A列內的所有鏈接 - 用于
DriveApp為每個鏈接創建 PDF(需要決議鏈接以檢索 ID) - 將 PDF 放在驅動器檔案夾中
- 從這里開始,您有兩種可能性:
- 使用 UI 下載檔案夾
- 使用此功能(由@Tanaike提供)直接獲取下載鏈接。在我的腳本里面被參考為
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);
}
檔案
getAs(contentType)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438800.html
