這是我上一個問題的后續問題。當我想使用
或者您可以使用此示例電子表格。
有沒有我錯過的步驟或我做錯了什么?
之前謝謝你回答問題
uj5u.com熱心網友回復:
在你的情況下,如何修改toStringFromHtml如下?
修改后的腳本:
function toStringFromHtml(html) {
html = '<div>' html '</div>';
html = html.replace(/<br>/g, "").replace(/<p><\/p><p><\/p>/g, "<p></p>").replace(/<span>|<\/span>/g, "");
var document = XmlService.parse(html);
var strText = XmlService.getPrettyFormat().setIndent("").format(document);
strText = strText.replace(/<[^>]*>/g, "");
return strText.trim();
}
在此修改后的腳本中,您的以下示例 HTML 將按如下方式轉換。
從
<p><span>Hi Katy</span></p> <p></p> <p><span>The illustration (examples) paragraph is useful when we want to explain or clarify something, such as an object, a person, a concept, or a situation. Sample Illustration Topics:</span></p> <p></p> <p></p> <p><span>1. Examples of annoying habits people have on the Skytrain.</span></p> <p><span>2. Positive habits that you admire in other people. </span></p> <p><span>3. Endangered animals in Asia. </span></p>到
<div> <p>Hi Katy</p> <p></p> <p>The illustration (examples) paragraph is useful when we want to explain or clarify something, such as an object, a person, a concept, or a situation. Sample Illustration Topics:</p> <p></p> <p>1. Examples of annoying habits people have on the Skytrain.</p> <p>2. Positive habits that you admire in other people. </p> <p>3. Endangered animals in Asia. </p> </div>通過這種轉換,得到以下結果。
Hi Katy The illustration (examples) paragraph is useful when we want to explain or clarify something, such as an object, a person, a concept, or a situation. Sample Illustration Topics: 1. Examples of annoying habits people have on the Skytrain. 2. Positive habits that you admire in other people. 3. Endangered animals in Asia.
筆記:
- 使用問題中顯示的示例 HTML 時,修改后的腳本可以實作您的目標。但是,我不確定您的其他 HTML 資料。所以我不確定這個修改后的腳本是否可以用于您的實際 HTML 資料。請注意這一點。
uj5u.com熱心網友回復:
我想你可以使用這個庫:[cheerio for Google Apps Script][1]
function htmltotext() {
let html = `<p><span>Hi Katy</span></p><p></p><p><span>The illustration (examples) paragraph is useful when we want to explain or clarify something, such as an object, a person, a concept, or a situation. Sample Illustration Topics:</span></p><p></p><p></p><p><span>1. Examples of annoying habits people have on the Skytrain.</span></p><p><span>2. Positive habits that you admire in other people. </span></p><p><span>3. Endangered animals in Asia. </span></p>`
const $ = Cheerio.load(html)
let paragraph = []
let lines = $('p')
for(let i = 0; i < lines.length;i ) {
let line = lines.get((i))
let line_text = $(line).text();
if(line_text) {
paragraph.push(line_text)
}
}
Logger.log(paragraph.join('\n'))
return paragraph.join('\n')
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/358825.html
