apps script getBody doesn't give full html contents - google-apps-script

I get emails from a system that contains paragraphs and tables.
When I try to get full html format using getBody from email it gives me just CSS contents and not the whole html contents.
However when I copy full email body and send it to myself in a new email then getBody function gives me accurately full body in html format including all tags and contents.
Kindly guide what I am missing here?
var label = GmailApp.getUserLabelByName("INBOX/reports0");
var threads = label.getThreads();
var tempbody = threads[i].getMessages()[0].getBody();
Regards

As the logged output in the console is too large and it's being truncated you're not seeing the whole output in neither of the cases, you're getting the entire HTML you just don't see it there. You can use Stackdriver logging instead to show the whole output.

Related

Google Apps Script Gmail getPlainBody Line Breaks

With the Google Apps Script Gmail library, when I use the function GmailMessage.getPlainBody(), the API seems to take what used to be one paragraph and break it up into multiple, potentially by using a character limit. For instance, a paragraph of my email reads:
From the link you sent me, I gleaned that Gmail refers to their secure email as confidential.
But when I call this function on the email, it becomes:
From the link you sent me, I gleaned that Gmail refers to their
secure email as confidential.
And, when I split the email text on a new line delimitor and do a bit of cleanup to create an array with my output, I end up with:
['From the link you sent me, I gleaned that Gmail refers to their', 'secure email as confidential.']
I viewed this Reddit post, which seemed to deal with the similar problem. But, I tried the resolution suggested by the person who posed the question:
body = message.getPlainBody().replace(/\r\n\r\n/gm,'aaaLINEBREAKERaaa').replace(/\r\n/gm,' ').replace(/aaaLINEBREAKERaaa/gm, '\r\r').replace(/ /gm,' ')
And it didn't quite give me what I need. Has anyone else encountered this problem, and if so, do you have a suggested workaround? Thanks!
I had the same issue. In that case, I used a workaround.
When I checked the email, I noticed that the HTML body is included in the message body and the HTML body has the original paragraph, and I used this situation. So, in this workaround, the original text is retrieved from the HTML body and the HTML is converted to a text. By this, the original paragraph is obtained. The sample script is as follows.
Sample script:
This script uses Drive API for converting HTML to text. So pelase enable Drive API at Advanced Google services.
var message = // Here, please use your "message".
var html = message.getBody();
var id = Drive.Files.insert({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, Utilities.newBlob(html, MimeType.HTML)).id;
var text = DocumentApp.openById(id).getBody().getText(); // or DocumentApp.openById(id).getBody().getText().trim();
DriveApp.getFileById(id).setTrashed(true);
console.log(text)
References:
getBody()
Files: insert

How can I set the page size for a PDF created (from HTML) and emailed using AppMaker?

I have a function in my AppMaker app that works like a charm -- when you click on a button, it takes the data from the current record, applies some logic to it, and mashes it up and styles it as HTML. I want to deliver the results to the user in two ways:
Print -- the function has a return that sends the HTML back to the browser, which opens up a print dialog as expected.
PDF by Email -- the function also generates a PDF which is attached to an email sent to the user.
I heard feedback from users that the PDF version cuts off the bottom two lines, so I'm trying to find out why.
To send the PDF as an email attachment, I use Utilities.newBlob() to first blobify my HTML. Then I get the blob as a PDF, and then I send the PDF as an email attachment. The code looks like this:
var pdfContent = Utilities.newBlob(html, 'text/html', 'PE-' + name + '.html')
var pdf = pdfContent.getAs("application/pdf");
GmailApp.sendEmail(to, subject, body, {attachments: pdf});
The email is delivered correctly with a PDF attached to it, and when you open it up, it looks pretty much like the HTML that gets sent to the print dialog. When you go to print it, though, the last lines clearly get cut off. My advice to users (until I could dig into the problem) was to use the "Fit to Page" function in the print dialog. Looking at it more closely, though, I had an inkling that it might be a page size issue -- there's something off about the proportions when you view the PDF. Sure enough, upon changing the paper size from letter to A4 in the print dialog, the page lays out perfectly.
So now I can't figure out where the default to A4 is happening.
I tried setting the #page attribute for size in the <style> tag, with no effect -- it doesn't seem to survive the transition from HTML to blob to PDF:
html += '<style>#page {size: 215.9mm 279.4mm; margin: 15mm;}</style> etc...';
For size I've used millimeters (as above) and px as units, and I've tried just using the word 'letter'. No version of the above works.
Is there some other place where I can specify page size to make this stop happening?

Google sheets error: Resource at url contents exceeded maximum size

My IMPORTXML functions sometimes work, but sometimes don't. When they don't, I get the following error:
Resource at url contents exceeded maximum size.
I tried downloading the website, and then hosting only the HTML at a domain that I own. When I do this, I am able to import using IMPORTXML, but my data obviously won't update automatically.
Is there a way to overcome this by somehow forcing the IMPORTXML function to only look at the HTML?
I recommend using a custom function that technically grabs all of it. If it is really a ton of content then wrap the (.*) with the content that surrounds the real content you want... such as <head>(.*)<\/head> (just for example sake)
function importWebsite(url) {
var found, html, content = '';
var response = UrlFetchApp.fetch(url);
if (response) {
html = response.getContentText();
if (html) content = html.match(/^(.*)/gi)[0];
}
return content;
}

Using HTML file to output a PDF

So I've got a HTML file, that I am using to send emails, but in some instances I want it simply to use that file to create a PDF of the same template.
I've got it functioning for the most part - it creates the file, runs the evaluations and gets the content, but it doesn't actually render the html. It simply leaves all the html notation in place.
For example, it outputs a pdf but it reads:
Dear Martin, <br />
instead of:
Dear Martin
How do I make sure it renders the HTML so that the PDF is laid out correctly, and doesn't have the html code noted in the text?
Here's the code:
var docName = "test";
var htmlBody = HtmlService.createHtmlOutput(template.evaluate().getContent()).getContent()
var doc = DocumentApp.create(docName);
doc.appendParagraph(htmlBody);
doc.saveAndClose();
DocsList.createFile(doc.getAs('application/pdf')).rename(docName);
You can use your existing HTML as a blob, and convert it to PDF like this:
var htmlBody = HtmlService.createHtmlOutputFromFile('my_file_within_script_project.html').getContent();
var blob = Utilities.newBlob(htmlBody, 'text/html').getAs('application/pdf').setName('my_output_in_drive.pdf');
DriveApp.createFile(blob);
The best option when you want to create a PDF from a template is to use Google Docs directly so that no formatting is lost and also avoid the problem you are facing.
Why don't you just create your template directly in Google Docs. Have some placeholders such as {name} instead of the actual name. Instead of using template.evaluate(), you can do a find and replace in the doc.

Is there an 'easy' way to split an enormous HTML file into multiple pages

I was given an html log file that has ~306K lines. I know there are better formats for this but would like to be able to view the file online. I think breaking the file up into smaller bits and "paging" is probably the way to go of any way other than manually doing the following:
take header of initial page and copy to every new file
copy 5-10k lines of the initial file and paste into a new body
copy the footer of the initial file and copy to every new file
Then give basic naming conventions of 1.html, 2.html, 3.html and create sublinks on every page in the new footer. Is there an automated way to do this?
You can use something like this
text = document.getElementById('text').value; var pieces = new Array(); var total = Math.ceil(text.length/10000); for(i=0;i<total;i++){
pieces[i] = text.substr(([i]*10000),10000)); }
then send it to a file(the way you want, ajax, or some write-txt)
if your html file is properly formatted (i.e with line-breaks) you can take out the header and footer and split the content every let's say 1000 lines (or any amount that guarantees meaningful data). that sound's doable with a shell script.