0
votes

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,

1
You may want to check Class GmailAttachment and getAs(contentType) method. As mentioned in the documentation getAs(contentType) method returns the data inside this object as a blob converted to the specified content type. - Teyam
Many thanks for your input. Are you able to show me an example? - loonix

1 Answers

2
votes

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);