我有一个脚本,据称用于使用 Google Apps 脚本将多个 pdf 文件合并为一个,以下是代码片段:
async function newMain() {
// Retrieve PDF data.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Please set your sheet name.
const ids = sheet.getRange("D1:D2").getDisplayValues().filter(([d]) => d);
const data = ids.map(([id]) => new Uint8Array(DriveApp.getFileById(id).getBlob().getBytes()));
// Merge PDFs.
const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
eval(UrlFetchApp.fetch(cdnjs).getContentText()); // Load pdf-lib
const pdfDoc = await PDFLib.PDFDocument.create();
for (let i = 0; i < data.length; i++) {
const pdfData = await PDFLib.PDFDocument.load(data[i]);
for (let j = 0; j < pdfData.getPageCount(); j++) {
const [page] = await pdfDoc.copyPages(pdfData, [j]);
pdfDoc.addPage(page);
}
}
const bytes = await pdfDoc.save();
// Create a PDF file.
DriveApp.createFile(Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, "sample2.pdf"));
}
但是当我运行它时,我收到以下错误:
ReferenceError:setTimeout 未定义 评估
X
评估
评估
评估
一个
非常感谢任何解决此问题的指导。
在你的脚本中,做如下修改怎么样?
async function newMain() {
// Retrieve PDF data.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Please set your sheet name.
const ids = sheet.getRange("D1:D2").getDisplayValues().filter(([d]) => d);
const data = ids.map(([id]) => new Uint8Array(DriveApp.getFileById(id).getBlob().getBytes()));
// Load pdf-lib
const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
eval(UrlFetchApp.fetch(cdnjs).getContentText().replace(/setTimeout\(.*?,.*?(\d*?)\)/g, "Utilities.sleep($1);return t();"));
// Merge PDFs.
const pdfDoc = await PDFLib.PDFDocument.create();
for (let i = 0; i < data.length; i++) {
const pdfData = await PDFLib.PDFDocument.load(data[i]);
for (let j = 0; j < pdfData.getPageCount(); j++) {
const [page] = await pdfDoc.copyPages(pdfData, [j]);
pdfDoc.addPage(page);
}
}
const bytes = await pdfDoc.save();
// Create a PDF file.
DriveApp.createFile(Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, "sample2.pdf"));
}
setTimeout
修改为Utilities.sleep
。