Created
August 10, 2017 01:02
-
-
Save dan23njguy/c0f832cc2e0c327c54876d9e8174a589 to your computer and use it in GitHub Desktop.
TypeError: Cannot find function getSubject in object GmailThread. (line 28, file "dl")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GLOBALS | |
//Array of file extension which you would like to extract to Drive | |
var fileTypesToExtract = ['pdf']; | |
//Name of the folder in google drive in which files will be put | |
var folderName = 'New Order pdfs'; | |
//Name of the label which will be applied after processing the mail message | |
var labelName = 'Orderdownloaded'; | |
function GmailToDrive(){ | |
//build query to search emails | |
var query = ''; | |
// filename:jpg OR filename:tif OR filename:png OR fileName:gif OR filename:bmp OR filename:pdf'; //'after:'+formattedDate+ | |
for(var i in fileTypesToExtract){ | |
query += (query == '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i])); | |
} | |
query = 'label:inbox label:exam label:New_order -label:Orderdownloaded -label:ordertotext' + query; | |
var threads = GmailApp.search(query); | |
var label = getGmailLabel_(labelName); | |
var deletelabel = getGmailLabel_('Exam'); | |
var parentFolder; | |
if(threads.length > 0){ | |
parentFolder = getFolder_(folderName); | |
} | |
var root = DriveApp.getRootFolder(); | |
for(var i in threads){ | |
var mesgs = threads[i].getMessages(); | |
//dhf | |
var messageSubject = threads[i].getSubject(); | |
var messageDate = threads[i].getDate(); | |
//dhf | |
for(var j in mesgs){ | |
//get attachments | |
var attachments = mesgs[j].getAttachments(); | |
for(var k in attachments){ | |
var attachment = attachments[k]; | |
var isImageType = checkIfImage_(attachment); | |
if(!isImageType) continue; | |
var attachmentBlob = attachment.copyBlob(); | |
//******************************************************************************************************************************************** | |
function pdfToText ( pdfFile, options ) { | |
// Start with a Blob object | |
// var blob = gmailAttachment.getAs(MimeType.PDF); | |
var blob = attachment.copyBlob(); | |
// fileId will be the ID of a saved text file (default behavior): | |
var fileId = pdfToText( blob ); | |
// filetext will contain text from pdf file, no residual files are saved: | |
var filetext = pdfToText( blob, {keepTextfile: false} ); | |
// we can save other converted file types, too: | |
var options = { | |
keepPdf : false, // Keep a copy of the original PDF file. | |
keepGdoc : true, // Keep a copy of the OCR Google Doc file. | |
keepTextfile : true, // Keep a copy of the text file. (default) | |
path : "New Order pdfs" // Folder path to store file(s) in. | |
} | |
filetext = pdfToText( blob, options ); | |
/** | |
* 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 | |
*/ | |
// 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()); | |
} | |
//******************************************************************************************************************************************** | |
var file = DriveApp.createFile(attachmentBlob); | |
parentFolder.addFile(file); | |
root.removeFile(file); | |
} | |
} | |
threads[i].addLabel(label); | |
threads[i].removeLabel(deletelabel); | |
//dhf | |
var ss = SpreadsheetApp.getActiveSheet(); | |
//This appends the text of the pdf doc to the gsheet | |
ss.appendRow([messageDate, messageSubject, filetext]) | |
//dhf | |
} | |
//This function will get the parent folder in Google drive | |
function getFolder_(folderName){ | |
var folder; | |
var fi = DriveApp.getFoldersByName(folderName); | |
if(fi.hasNext()){ | |
folder = fi.next(); | |
} | |
else{ | |
folder = DriveApp.createFolder(folderName); | |
} | |
return folder; | |
} | |
//getDate n days back | |
// n must be integer | |
function getDateNDaysBack_(n){ | |
n = parseInt(n); | |
var today = new Date(); | |
var dateNDaysBack = new Date(today.valueOf() - n*24*60*60*1000); | |
return dateNDaysBack; | |
} | |
function getGmailLabel_(name){ | |
var label = GmailApp.getUserLabelByName(name); | |
if(label == null){ | |
label = GmailApp.createLabel(name); | |
} | |
return label; | |
} | |
//this function will check for filextension type. | |
// and return boolean | |
function checkIfImage_(attachment){ | |
var fileName = attachment.getName(); | |
var temp = fileName.split('.'); | |
var fileExtension = temp[temp.length-1].toLowerCase(); | |
if(fileTypesToExtract.indexOf(fileExtension) != -1) return true; | |
else return false; | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looking for some help on a script that I can't seem to figure out. It is a combination of this script:
https://stackoverflow.com/questions/26613809/get-pdf-attachments-from-gmail-as-text (thanks to the orginal author)
AND this one:
https://gist.github.com/Carolusian/9021828f284b31e1440c (thanks to the orginal author)
However, when I tried to combine the two I couldn't figure out how to get the subject of the email to pass through to the google sheet. Any help is much appreciated.