我有这段代码,它正在做我想要它做的事情。它从 Gmail 中提取 PDF,创建一个文件夹来存储它们,然后遍历这些 pdf 并将它们存储在主 google 工作表内的相应工作表中。
运行代码时,它成功地完成了上述所有操作,但最后它抛出
Exception: Invalid argument: id
错误并引用了代码的第 3 行。我似乎无法弄清楚为什么会发生此错误。此代码使用 Google App Script 中的 Drive V2 API 服务。
代码似乎运行良好。任何帮助寻找解决方案或解决方法的帮助将不胜感激。
function convertPDFsToSheets(folderId, language) {
language = language || 'en'; // English
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFiles();
// Create a new Google Sheet and add the lines to it
const newSpreadsheet = SpreadsheetApp.create('Invoices');
let fileIndex = 0;
// Loop through each PDF file in the folder
while (files.hasNext()) {
const file = files.next();
if (file.getMimeType() !== 'application/pdf') {
continue; // Skip non-PDF files
}
// Read the PDF file in Google Drive
const pdfDocument = DriveApp.getFileById(file.getId());
// Use OCR to convert PDF to a temporary Google Document
// Restrict the response to include file Id and Title fields only
const fileResource = Drive.Files.insert(
{
title: pdfDocument.getName().replace(/\.pdf$/, ''),
mimeType: pdfDocument.getMimeType() || 'application/pdf',
},
pdfDocument.getBlob(),
{
ocr: true,
ocrLanguage: language,
fields: 'id,title',
}
);
const id = fileResource.id;
const title = fileResource.title;
// Use the Document API to extract text from the Google Document
const textContent = DocumentApp.openById(id).getBody().getText();
// Delete the temporary Google Document since it is no longer needed
DriveApp.getFileById(id).setTrashed(true);
// Split the text content into an array of lines
const lines = textContent.split('\n');
// Create a new sheet inside the main sheet for the current PDF
const newSheet = newSpreadsheet.insertSheet();
const sheetName = 'Sheet ' + newSheet.getIndex(); // Generate sheet name as "Sheet 1", "Sheet 2", etc.
newSheet.setName(sheetName);
// Loop through each line and add it to a new row in the sheet
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Split the line into an array of cells using a comma delimiter
const cells = line.split(',');
// Set the values in the sheet for the current row
newSheet.getRange(i + 1, 1, 1, cells.length).setValues([cells]);
}
fileIndex++;
}
//Return ID of a new spreadsheet
const newSpreadsheetId = newSpreadsheet.getId();
Logger.log(newSpreadsheetId);
return newSpreadsheetId;
}
function makeFolderDrive(){
const currentDate = new Date();
const afterDate = new Date();
const folderDate = new Date();
currentDate.setDate(currentDate.getDate()+1);
afterDate.setDate(currentDate.getDate() - 1);
folderDate.setDate(currentDate.getDate()-2);
const folderName = 'Invoices for: ' + Utilities.formatDate(folderDate, 'EST', 'yyyyMMdd');
const folder = DriveApp.createFolder(folderName);
const folderID = folder.getId();
const formattedCurrentDate = Utilities.formatDate(currentDate, Session.getScriptTimeZone(), 'yyyy/MM/dd');
const formattedAfterDate = Utilities.formatDate(afterDate, Session.getScriptTimeZone(), 'yyyy/MM/dd');
const threads = GmailApp.search('label:invoices has:attachment from:[email protected] after:' + formattedAfterDate + ' before:' + formattedCurrentDate);
threads.forEach(thread => {
thread.getMessages().forEach(message => {
message.getAttachments().forEach(attachment => {
Logger.log(attachment.getName()); // Log the PDF file name
folder.createFile(attachment.copyBlob().setName(attachment.getName()));
});
});
});
Logger.log('Created folder with ID: ' + folderID);
return folderID;
}
const folderId = makeFolderDrive(); // Retrieve the folder ID from the `makeFolderDrive` function
Logger.log('Received folder ID: ' + folderId);
const newSpreadsheetId = convertPDFsToSheets(folderId, 'en');
Logger.log('New Spreadsheet ID: ' + newSpreadsheetId);
我做了不同的 try/catch 方法以及 Logger 语句来尝试找到问题所在。我仍然不知道这个错误可能发生在哪里。