0

I'm new to google app script.

I was trying to convert html string to plain text without html tags in google app script using the reference in this question.

However, when I try to apply it to my script, it is not working as expected.

This is the script that I use:

function toStringFromHtml(html)
{
  
html = '<div>' + html + '</div>';
html = html.replace(/<br>/g,"");
var document = XmlService.parse(html);
var strText = XmlService.getPrettyFormat().format(document);
strText = strText.replace(/<[^>]*>/g,"");
return strText;
}

and this is the output: enter image description here

While my actual expectation is in the third column.

Can someone tell me what is wrong with my previous script?

Thank you!

2 Answers 2

1

In your situation, for example, how about the following modification?

From:

return strText;

To:

return strText.trim();

Reference:

Sign up to request clarification or add additional context in comments.

Comments

1

If you're running in a browser, then the easiest way is just to let the browser do it for you

function stripHtml(html)
{
   let tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}

3 Comments

Hi, thank you for the response. I tried using this and I got the error. Then I modify it a bit, and it's still error. Is this expected? The function that I use: function stripHtml(html) { html = '<div>' + html + '</div>'; html = html.replace(/<br>/g,""); var document = XmlService.parse(html); let tmp = document.createElement("DIV"); tmp.innerHTML = html; return tmp.textContent || tmp.innerText || ""; }
what was your error ? could you share it for me pls, you got a error? i guess you should've added full code of your problem , if you can post the code of the part of the problem i could help you better
@Ninja -- is there a way to create a dummy HTML document for this purpose, so I can debug and run it on my desktop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.