Use OCR function of google docs on google apps scr

2019-04-15 16:50发布

问题:

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