Use OCR function of google docs on google apps script

心已入冬 提交于 2019-12-12 09:14:56

问题


How can i use this on the google apps script? i want to upload attachments from gmail. Also, i want to use the ocr function of google docs since all of the attatchments will be images.

function uploadFile() {
  var image = UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();
  var file = {
    title: 'google_logo.png',
    mimeType: 'image/png'
  };
  file = Drive.Files.insert(file, image);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}

回答1:


Change your file varaible to Drive.Files.insert(file, image, {ocr: true});

Please note, OCR didn't work for me when I used the image you supplied. Test it out with this image and should work fine for you.

function uploadFile() {
  var image = UrlFetchApp.fetch('https://i.imgur.com/ahDXEPo.png').getBlob();
  var file = {
    title: 'imagehastext.png',
    mimeType: 'image/png'
  };
  file = Drive.Files.insert(file, image, {ocr: true});
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
}

Here's a rough example of how to get an email attachment.

GmailApp.getInboxThreads()[0].getMessages()[0].getAttachments();

And some documentation on how to save files to Google Drive



来源:https://stackoverflow.com/questions/25431795/use-ocr-function-of-google-docs-on-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!