0

I already done a convert to CSV using one of the threads I found in Stack Overflow but as I have one report that is sent to my gmail account in .html I was wondering if someone as some piece of code that can share so I can get it converted to google sheets.

Many thanks,

Community
  • 1
  • 1
loonix
  • 430
  • 7
  • 14
  • You may want to check [Class GmailAttachment](https://developers.google.com/apps-script/reference/gmail/gmail-attachment) and [`getAs(contentType)`](https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getAs(String)) method. As mentioned in the documentation [`getAs(contentType)`](https://developers.google.com/apps-script/reference/gmail/gmail-attachment#getAs(String)) method returns the data inside this object as a blob converted to the specified content type. – Teyam Feb 08 '17 at 16:01
  • Many thanks for your input. Are you able to show me an example? – loonix Feb 09 '17 at 12:16

1 Answers1

2

Really late to the party here, but I just wrote a google script that might be helpful. It takes an html table file and converts it to csv.

I'm replacing all the table tags with commas where necessary or removing them altogether. You may have to massage it a bit for your specific use, but it's working for me.

There's probably a more elegant way to do this... but here you go!

  var blob = DriveApp.getFileById(id).getBlob();
  
  var string = blob.getDataAsString();
  var newString = string.replace(/\r?\n|\r/g,"").replace(/<\/td>/g,",").replace(/<td[^<>]*>/g,"").replace(/<tr[^<>]*>/g,"").replace(/<\/tr>/g,'\n').replace(/<br>/g," ").replace(/&nbsp;/g,"");
  Logger.log(newString);
  
  var csv = Utilities.parseCsv(newString);
KatieBev
  • 46
  • 4
  • I managed to get it working a long time ago, but always good to have other opinions for other members that stumble on this post. Here is my code https://github.com/loonix/Google-Apps-Script/blob/master/Fetch-.CSV-Attachment-From-Gmail.gs – loonix Nov 09 '18 at 15:30