Google apps script 使用google应用程序脚本从google drive获取OCR图像
我已经实现了以下脚本,使用图像URL对单个和多个图像执行OCRGoogle apps script 使用google应用程序脚本从google drive获取OCR图像,google-apps-script,google-sheets,ocr,google-docs,Google Apps Script,Google Sheets,Ocr,Google Docs,我已经实现了以下脚本,使用图像URL对单个和多个图像执行OCR function doOCRALL() { var selected = SpreadsheetApp.getActiveSheet().getActiveRange().getValues().length; for (var i = 0; i < selected; i++) { var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().
function doOCRALL() {
var selected = SpreadsheetApp.getActiveSheet().getActiveRange().getValues().length;
for (var i = 0; i < selected; i++) {
var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol).getValue();
var image = UrlFetchApp.fetch(valueURL).getBlob();
var file = {
title: 'OCR File',
mimeType: 'image/png'
};
// OCR is supported for PDF and image formats
file = Drive.Files.insert(file, image, {ocr: true});
var doc = DocumentApp.openByUrl(file.embedLink);
var body = doc.getBody().getText();
//Get link Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 2).setValue(file.embedLink);
//Get Content of Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 1).setValue(body);
}
}
function doOCR() {
//
var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol).getValue();
var image = UrlFetchApp.fetch(valueURL).getBlob();
var file = {
title: 'OCR File',
mimeType: 'image/png'
};
// OCR is supported for PDF and image formats
file = Drive.Files.insert(file, image, {ocr: true});
var doc = DocumentApp.openByUrl(file.embedLink);
var body = doc.getBody().getText();
// Print the Google Document URL in the console
Logger.log("body: %s", body);
Logger.log("File URL: %s", file.embedLink);
//Get link Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 2).setValue(file.embedLink);
//Get Content of Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 1).setValue(body);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('OCR Tools')
.addItem('Extract Cell', 'doOCR')
.addItem('Extract All Cell', 'doOCRALL')
.addSeparator()
.addSubMenu(ui.createMenu('About US')
.addItem('Infomation', 'menuItem2'))
.addToUi();
}
function menuItem2() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('AIO Team');
}
当我为任何图像提供一个图像URL时,它都可以工作。但如果我在我的驱动器上上传相同的图像,然后从驱动器提供图像URL,它只会让我登录主菜单。对于其他驱动器图像,它提供相同的文本。
提前感谢。如果内容已经在驱动器中,您不需要获取指向该内容的链接,只需提供可以从链接中获取的文件id即可 一旦您有了文件ID,您就可以简单地复制它,并使用最佳参数来激活OCR。当然,完整的选项列表可以在Drive REST API页面上找到: 我鼓励您也阅读最佳实践,如字段规范,这是最新的驱动器API版本的要求 此函数获取从某处获得的输入驱动器文件ID,以及设置使用OCR选项的truth-y值。 显而易见的假设是您拥有权限、id有效、您已在云控制台中启用了高级服务和驱动器API等
function getIdOfCopyOfDriveFile(fileId, useOcr) {
const options = {
fields: "choose the metadata fields to return in the response e.g. 'id,title,parents'"
};
const existingMetaData = Drive.Files.get(fileId, options);
options.ocr = !!useOcr;
existingMetaData.title += " (copied with" + (options.ocr ? " " : "out ") + "ocr)";
// We could do other modifications of fields we requested before
// copying, like changing the parents array to move the new file.
const newFileMetaData = Drive.Files.copy(existingMetaData, fileId, options);
return newFileMetaData.id;
}
我能问一下你的网址吗?你是怎么得到的?您能提供示例URL吗?当然,请删除您的私人信息。谢谢,但我的问题解决了。使用fileId而不是URL并更改了访问权限。感谢您的回复。我很高兴你的问题解决了。答案很有帮助。我使用了fileID而不是url,我需要更改文件的访问权限。file.setSharedDriveApp.Access.any,DriveApp.Permission.EDIT;