这是代码:
function dataAutofill() {
var doc = DocumentApp.openByUrl('inserted drive link here');
console.log(doc);
}
GAS错误:“参数无效(第3行,“代码”)
我打算阅读一个驱动器链接,该链接会打开PDF视图,从中提取信息,然后将该信息写入电子表格中。但是,我一直停留在函数读取数据的位置。
编辑1:已经指出,我无法在DocumentApp中使用openbyURl
答案 0 :(得分:0)
您要将PDF文件的内容(作为文本)导入到Google电子表格中。
如@Jescanellas所指示,此内容涵盖在主题Get text from PDF in Google中。 Mogsdad的答案特别有用,以下答案只不过是将各个组成部分组合到与您的情况相关的单个答案中。
作为概念证明-我使用Google文档创建了一个简单表,将文档保存为PDF文件,将PDF文件加载到Google Drive文件夹中,然后运行了该脚本(绑定到电子表格)。 PDF文件将插入电子表格的单元格“ A1”中。这种方法不会假装反映您的数据-可以想象,为此,您将有一种方法可以填充电子表格的单元格。
资源>先进的Google服务-云端硬盘API
该脚本要求在脚本编辑器下启用Drive API:
资源>高级Google服务> Drive API。
注意:
Mogsdad指的是要作为gist(pdfToText.js)找到的函数pdfToText()
。今后的读者应该注意这一点,因为要点可能会随着时间而改变。
function so5838796101() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "58387961";
var sheet = ss.getSheetByName(sheetname);
// identify the PDF file
var pdfFile = DriveApp.getFilesByName("so_58387961_export_from_Google_Docs.pdf").next();
var blob = pdfFile.getBlob();
// Get the text from pdf
// this uses Mogsdad's gist
var filetext = pdfToText( blob, {keepTextfile: false} );
// define destination and set value
var sheetdata =sheet.getRange("A1");
sheetdata.setValue(filetext);
}
// Mogsdad's gist start
/**
* Convert pdf file (blob) to a text file on Drive, using built-in OCR.
* By default, the text file will be placed in the root folder, with the same
* name as source pdf (but extension 'txt'). Options:
* keepPdf (boolean, default false) Keep a copy of the original PDF file.
* keepGdoc (boolean, default false) Keep a copy of the OCR Google Doc file.
* keepTextfile (boolean, default true) Keep a copy of the text file.
* path (string, default blank) Folder path to store file(s) in.
* ocrLanguage (ISO 639-1 code) Default 'en'.
* textResult (boolean, default false) If true and keepTextfile true, return
* string of text content. If keepTextfile
* is false, text content is returned without
* regard to this option. Otherwise, return
* id of textfile.
*
* @param {blob} pdfFile Blob containing pdf file
* @param {object} options (Optional) Object specifying handling details
*
* @returns {string} id of text file (default) or text content
*/
function pdfToText ( pdfFile, options ) {
// Ensure Advanced Drive Service is enabled
try {
Drive.Files.list();
}
catch (e) {
throw new Error( "To use pdfToText(), first enable 'Drive API' in Resources > Advanced Google Services." );
}
// Set default options
options = options || {};
options.keepTextfile = options.hasOwnProperty("keepTextfile") ? options.keepTextfile : true;
// Prepare resource object for file creation
var parents = [];
if (options.path) {
parents.push( getDriveFolderFromPath (options.path) );
}
var pdfName = pdfFile.getName();
var resource = {
title: pdfName,
mimeType: pdfFile.getContentType(),
parents: parents
};
// Save PDF to Drive, if requested
if (options.keepPdf) {
var file = Drive.Files.insert(resource, pdfFile);
}
// Save PDF as GDOC
resource.title = pdfName.replace(/pdf$/, 'gdoc');
var insertOpts = {
ocr: true,
ocrLanguage: options.ocrLanguage || 'en'
}
var gdocFile = Drive.Files.insert(resource, pdfFile, insertOpts);
// Get text from GDOC
var gdocDoc = DocumentApp.openById(gdocFile.id);
var text = gdocDoc.getBody().getText();
// We're done using the Gdoc. Unless requested to keepGdoc, delete it.
if (!options.keepGdoc) {
Drive.Files.remove(gdocFile.id);
}
// Save text file, if requested
if (options.keepTextfile) {
resource.title = pdfName.replace(/pdf$/, 'txt');
resource.mimeType = MimeType.PLAIN_TEXT;
var textBlob = Utilities.newBlob(text, MimeType.PLAIN_TEXT, resource.title);
var textFile = Drive.Files.insert(resource, textBlob);
}
// Return result of conversion
if (!options.keepTextfile || options.textResult) {
return text;
}
else {
return textFile.id
}
}
// Helper utility from http://ramblings.mcpher.com/Home/excelquirks/gooscript/driveapppathfolder
function getDriveFolderFromPath (path) {
return (path || "/").split("/").reduce ( function(prev,current) {
if (prev && current) {
var fldrs = prev.getFoldersByName(current);
return fldrs.hasNext() ? fldrs.next() : null;
}
else {
return current ? null : prev;
}
},DriveApp.getRootFolder());
}
// Mogsdad's gist end